Search code examples
androidxamarinxamarin.uitest

Installed apk doesn't start


I have to write couple tests on Xamarin UI Test framework to check apk on emulator. I tried to install my apk in two ways:

1 adb.exe install path\com.company.mobiledemo.apk

2 through Xamarin UI Test

if (platform == Platform.Android)
{
    AndroidApp app = ConfigureApp.Android
        .ApkFile("path\\com.company.mobiledemo.apk")
        .Debug()
        .EnableLocalScreenshots()
        .DeviceSerial("emulator-5554")
        .StartApp();

    return app;
}

In first approach apk installed and working correctly. But when apk installed via code above apk will be install but wont launch, just stopped after splash screen.

enter image description here

I have no idea why because both use the same apk file.

NUnit: 3.13.3
NUnit3TestAdapter: 4.2.1
Xamarin.UITest: 2.2.6
Emulator OS: Android 6.0 Api 23 

Solution

  • Ok I found the solution. It's a bit confused and maybe there is a more easy fix. Problem as I understood is your apk and Instrumentation backend apk must be signed the same cert.

    My steps:

    1. I installed apk via adb command : >adb.exe install ~\..\com.company.mobiledemo.apk

    2. Then changed AppInitializer.cs like this :

      if (platform == Platform.Android)
      {
          string keystore = "~\\..\\..\\some.keystore";
          AndroidAppConfigurator appConfigurator = ConfigureApp.Android
              .KeyStore(
              keystore,
              "storePassword",
              "keyPassword",
              "\"keyAlias\""
              )
              .InstalledApp("com.company.mobiledemo.apk")
              .Debug()
              .EnableLocalScreenshots();
      
          AndroidApp app = appConfigurator.StartApp(AppDataMode.Clear);
          return app;
      }
      
    3. Then tried to launch test and got error :

    System.Exception : Failed to execute: C:\Program Files\Android\jdk\microsoft_dist_openjdk_1.8.0.25\bin\jarsigner.exe -sigalg SHA1withRSA -digestalg SHA1 -signedjar "C:\Users\{user}\AppData\Local\Temp\uitest\a-287A943C412ED6ED5DEB1675E7FDF91843FD0807\20344\SignedTestServer.apk" -storepass bla-bla -keypass bla-bla -keystore "~..\Mobile\Mobile.Android\Certificate\some.keystore" "C:\Users\{user}\AppData\Local\Temp\uitest\a-287A943C412ED6ED5DEB1675E7FDF91843FD0807\TestServer.apk" ""Key Alias"" - exit code: 1
    
    Please type jarsigner -help for usage
    Only one alias can be specified
    

    It's ok.

    1. Open folder from error message :

    C:\Users{user}\AppData\Local\Temp\uitest\

    You will see something like this :

    enter image description here

    1. Delete all and run tests once again. After launching new test data will appear:

    enter image description here

    1. Copy command from error message above, go to C:\Program Files\Android\jdk\microsoft_dist_openjdk_1.8.0.25\bin or directory where located jarsigner.exe

    2. Execute command after System.Exception : Failed to execute:

    enter image description here

    1. Find SignedTestServer.apk and copy it into folder containes dummy.apk

    enter image description here

    1. Run tests once again and see that FinalTestServer.apk appeared

    enter image description here

    PS Close folder C:\Users\{user}\AppData\Local\Temp\uitest before 9.