GenError
GenError

Reputation: 1176

Android instrumentation test fails to install APK

I want to run instrumentation test as part of my CI pipeline but when I run the tests they fail because the APK could not be installed (as far as I understood):

> Task :app:connectedDebugAndroidTest FAILED
Mar 18, 2022 3:00:59 PM com.google.testing.platform.RunnerImpl run
SEVERE: Test execution failed with fatal error!
com.google.testing.platform.api.plugin.PluginException: Exception thrown during onBeforeAll invocation of plugin com.google.testing.platform.plugin.android.AndroidDevicePlugin.
        at com.google.testing.platform.plugin.PluginLifecycleKt.invokeOrThrow(PluginLifecycle.kt:216)
        at com.google.testing.platform.plugin.PluginLifecycleKt.invokeOrThrow$default(PluginLifecycle.kt:205)
        at com.google.testing.platform.plugin.PluginLifecycle$onBeforeAll$1.invoke(PluginLifecycle.kt:92)
        at com.google.testing.platform.plugin.PluginLifecycle$onBeforeAll$1.invoke(PluginLifecycle.kt:88)
        at com.google.testing.platform.core.telemetry.common.noop.NoopDiagnosticsScope.recordEvent(NoopDiagnosticsScope.kt:35)
        at com.google.testing.platform.core.telemetry.TelemetryKt.recordEvent(Telemetry.kt:105)
        at com.google.testing.platform.core.telemetry.TelemetryKt.recordEvent$default(Telemetry.kt:98)
        at com.google.testing.platform.plugin.PluginLifecycle.onBeforeAll(PluginLifecycle.kt:88)
        at com.google.testing.platform.executor.SingleDeviceExecutor$execute$4.invoke(SingleDeviceExecutor.kt:86)
        at com.google.testing.platform.executor.SingleDeviceExecutor$execute$4.invoke(SingleDeviceExecutor.kt:86)
        at com.google.testing.platform.executor.SingleDeviceExecutor.runUnlessCancelled(SingleDeviceExecutor.kt:105)
        at com.google.testing.platform.executor.SingleDeviceExecutor.execute(SingleDeviceExecutor.kt:86)
        at com.google.testing.platform.RunnerImpl.run(RunnerImpl.kt:108)
        at com.google.testing.platform.server.strategy.NonInteractiveServerStrategy$run$4.invoke(NonInteractiveServerStrategy.kt:80)
        at com.google.testing.platform.server.strategy.NonInteractiveServerStrategy$run$4.invoke(NonInteractiveServerStrategy.kt:79)
        at com.google.testing.platform.core.telemetry.common.noop.NoopDiagnosticsScope.recordEvent(NoopDiagnosticsScope.kt:35)
        at com.google.testing.platform.core.telemetry.TelemetryKt.recordEvent(Telemetry.kt:66)
        at com.google.testing.platform.server.strategy.NonInteractiveServerStrategy.run(NonInteractiveServerStrategy.kt:79)
        at com.google.testing.platform.main.MainKt$main$4.invokeSuspend(Main.kt:67)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
        at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
        at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
        at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
        at com.google.testing.platform.main.MainKt.main(Main.kt:66)
        at com.google.testing.platform.main.MainKt.main$default(Main.kt:34)
        at com.google.testing.platform.main.MainKt.main(Main.kt)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at com.google.testing.platform.launcher.Launcher.main(Launcher.java:149)
Caused by: com.google.testing.platform.core.error.UtpException: ErrorName: UNKNOWN
NameSpace: DdmlibAndroidDeviceController
ErrorCode: 1
Message: Failed to install APKs: UNKNOWN
        at com.android.tools.utp.plugins.deviceprovider.ddmlib.DdmlibAndroidDeviceController$executeAsync$deferred$1.invokeSuspend(DdmlibAndroidDeviceController.kt:197)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
Caused by: com.android.ddmlib.InstallException: Unknown failure: Exception occurred while executing 'install':
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.pm.PackageManagerInternal.freeStorage(java.lang.String, long, int)' on a null object reference
at com.android.server.StorageManagerService.allocateBytes(StorageManagerService.java:4033)
at android.os.storage.StorageManager.allocateBytes(StorageManager.java:2340)
at android.os.storage.StorageManager.allocateBytes(StorageManager.java:2419)
at com.android.server.pm.PackageInstallerSession.doWriteInternal(PackageInstallerSession.java:1556)
at com.android.server.pm.PackageInstallerSession.write(PackageInstallerSession.java:1469)
at android.content.pm.PackageInstaller$Session.write(PackageInstaller.java:1045)
at com.android.server.pm.PackageManagerShellCommand.doWriteSplit(PackageManagerShellCommand.java:3338)
at com.android.server.pm.PackageManagerShellCommand.doWriteSplits(PackageManagerShellCommand.java:3300)
at com.android.server.pm.PackageManagerShellCommand.doRunInstall(PackageManagerShellCommand.java:1351)
at com.android.server.pm.PackageManagerShellCommand.runInstall(PackageManagerShellCommand.java:1303)
at com.android.server.pm.PackageManagerShellCommand.onCommand(PackageManagerShellCommand.java:193)
at com.android.modules.utils.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:97)
at android.os.ShellCommand.exec(ShellCommand.java:38)
at com.android.server.pm.PackageManagerService.onShellCommand(PackageManagerService.java:24742)
at android.os.Binder.shellCommand(Binder.java:950)
at android.os.Binder.onTransact(Binder.java:834)
at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:4818)
at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:8839)
at android.os.Binder.execTransactInternal(Binder.java:1184)
at android.os.Binder.execTransact(Binder.java:1143)
        at com.android.ddmlib.internal.DeviceImpl.installRemotePackage(DeviceImpl.java:1314)
        at com.android.ddmlib.internal.DeviceImpl.installPackage(DeviceImpl.java:1140)
        at com.android.tools.utp.plugins.deviceprovider.ddmlib.DdmlibAndroidDevice.installPackage(DdmlibAndroidDevice.kt)
        at com.android.tools.utp.plugins.deviceprovider.ddmlib.DdmlibAndroidDeviceController$executeAsync$deferred$1.invokeSuspend(DdmlibAndroidDeviceController.kt:166)
        ... 6 more

Mar 18, 2022 3:00:59 PM com.android.tools.utp.plugins.host.additionaltestoutput.AndroidAdditionalTestOutputPlugin deviceShellAndCheckSuccess
WARNING: Shell command failed (1): ls "/sdcard/Android/media/xxxxx/additional_test_output"
ls: /sdcard/Android/media/xxxxx/additional_test_output: No such file or directory

Mar 18, 2022 3:00:59 PM com.android.tools.utp.plugins.host.additionaltestoutput.AndroidAdditionalTestOutputPlugin afterAll
WARNING: Failed to retrieve additional test outputs from device.
com.android.ddmlib.SyncException: Remote object doesn't exist!
        at com.android.ddmlib.SyncService.pullFile(SyncService.java:341)
        at com.android.ddmlib.internal.DeviceImpl.pullFile(DeviceImpl.java:1083)
        at com.android.tools.utp.plugins.deviceprovider.ddmlib.DdmlibAndroidDevice.pullFile(DdmlibAndroidDevice.kt)
        at com.android.tools.utp.plugins.deviceprovider.ddmlib.DdmlibAndroidDeviceController.pull(DdmlibAndroidDeviceController.kt:254)
        at com.google.testing.platform.executor.DeviceControllerProxy$pull$2.invoke(DeviceControllerProxy.kt:82)
        at com.google.testing.platform.executor.DeviceControllerProxy$pull$2.invoke(DeviceControllerProxy.kt:82)
        at com.google.testing.platform.core.telemetry.common.noop.NoopDiagnosticsScope.recordEvent(NoopDiagnosticsScope.kt:35)
        at com.google.testing.platform.core.telemetry.TelemetryKt.recordEvent(Telemetry.kt:66)
        at com.google.testing.platform.executor.DeviceControllerProxy.pull(DeviceControllerProxy.kt:79)
        at com.android.tools.utp.plugins.host.additionaltestoutput.AndroidAdditionalTestOutputPlugin.copyAdditionalTestOutputsFromDeviceToHost(AndroidAdditionalTestOutputPlugin.kt:222)
        at com.android.tools.utp.plugins.host.additionaltestoutput.AndroidAdditionalTestOutputPlugin.afterAll(AndroidAdditionalTestOutputPlugin.kt:206)
        at com.google.testing.platform.plugin.PluginLifecycle$onAfterAll$1$2$1.invoke(PluginLifecycle.kt:180)
        at com.google.testing.platform.plugin.PluginLifecycle$onAfterAll$1$2$1.invoke(PluginLifecycle.kt:179)
        at com.google.testing.platform.plugin.PluginLifecycleKt$invokeOrThrow$1.invoke(PluginLifecycle.kt:213)
        at com.google.testing.platform.plugin.PluginLifecycleKt$invokeOrThrow$1.invoke(PluginLifecycle.kt:212)
        at com.google.testing.platform.core.telemetry.common.noop.NoopDiagnosticsScope.recordEvent(NoopDiagnosticsScope.kt:35)
        at com.google.testing.platform.core.telemetry.TelemetryKt.recordEvent(Telemetry.kt:105)
        at com.google.testing.platform.core.telemetry.TelemetryKt.recordEvent$default(Telemetry.kt:98)
        at com.google.testing.platform.plugin.PluginLifecycleKt.invokeOrThrow(PluginLifecycle.kt:212)
        at com.google.testing.platform.plugin.PluginLifecycleKt.invokeOrThrow$default(PluginLifecycle.kt:205)
        at com.google.testing.platform.plugin.PluginLifecycle$onAfterAll$1.invoke(PluginLifecycle.kt:179)
        at com.google.testing.platform.plugin.PluginLifecycle$onAfterAll$1.invoke(PluginLifecycle.kt:173)
        at com.google.testing.platform.core.telemetry.common.noop.NoopDiagnosticsScope.recordEvent(NoopDiagnosticsScope.kt:35)
        at com.google.testing.platform.core.telemetry.TelemetryKt.recordEvent(Telemetry.kt:105)
        at com.google.testing.platform.core.telemetry.TelemetryKt.recordEvent$default(Telemetry.kt:98)
        at com.google.testing.platform.plugin.PluginLifecycle.onAfterAll(PluginLifecycle.kt:173)
        at com.google.testing.platform.executor.SingleDeviceExecutor$execute$3.invoke(SingleDeviceExecutor.kt:83)
        at com.google.testing.platform.executor.SingleDeviceExecutor$execute$3.invoke(SingleDeviceExecutor.kt:82)
        at com.google.testing.platform.result.TestResultListenerManager.afterTestSuite(TestResultListenerManager.kt:140)
        at com.google.testing.platform.RunnerImpl.run(RunnerImpl.kt:118)
        at com.google.testing.platform.server.strategy.NonInteractiveServerStrategy$run$4.invoke(NonInteractiveServerStrategy.kt:80)
        at com.google.testing.platform.server.strategy.NonInteractiveServerStrategy$run$4.invoke(NonInteractiveServerStrategy.kt:79)
        at com.google.testing.platform.core.telemetry.common.noop.NoopDiagnosticsScope.recordEvent(NoopDiagnosticsScope.kt:35)
        at com.google.testing.platform.core.telemetry.TelemetryKt.recordEvent(Telemetry.kt:66)
        at com.google.testing.platform.server.strategy.NonInteractiveServerStrategy.run(NonInteractiveServerStrategy.kt:79)
        at com.google.testing.platform.main.MainKt$main$4.invokeSuspend(Main.kt:67)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
        at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
        at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
        at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
        at com.google.testing.platform.main.MainKt.main(Main.kt:66)
        at com.google.testing.platform.main.MainKt.main$default(Main.kt:34)
        at com.google.testing.platform.main.MainKt.main(Main.kt)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at com.google.testing.platform.launcher.Launcher.main(Launcher.java:149)



Test results saved as file:/var/jenkins_home/workspace/xxxx/app/build/outputs/androidTest-results/connected/S21(AVD)%20-%2012/test-result.pb. Inspect these results in Android Studio by selecting Run > Import Tests From File from the menu bar and importing test-result.pb.
Platform error occurred when running the UTP test suite

FAILURE: Build failed with an exception.

The error message looks like it's a storage issue, but the emulator should have enough storage. This is how I started the emulator:

emulator -avd S21 -memory 2048 -wipe-data -no-boot-anim -cache-size 1000 -noaudio -no-window -partition-size 8192 &

and this is the device creation

avdmanager create avd --force -c 2G -k "system-images;android-32;google_apis;x86_64" -n S21

Then the gradle script is called like this:

./gradlew connectedDebugAndroidTest

When I manually build the universal apk, install it via adb and then run the instrumentation tests it works. But to my understanding it's supposed to work without having to manually install the app.

What is the problem?

Upvotes: 19

Views: 15660

Answers (6)

Mr-IDE
Mr-IDE

Reputation: 7641

I had a similar error on Android 10 when switching between Git branches, then trying to run some Instrumentation tests:

Unknown platform error occurred when running the UTP test suite. Please check logs for details.

The solution was to uninstall the test APK and then re-install it by running a UI test. The steps are as follows:

With the phone UI:

  1. On the Home screen, press the "Apps" button, or swipe up. Then launch the Settings app.
  2. Click on "Apps and Notifications" ➔ "See All apps" or "Applications List"
  3. Scroll down the list to locate your test APK, which will normally have a title of something like "com.company.app.test" and it will have a generic green Android icon.
  4. Click this test APK ➔ Uninstall ➔ OK.
  5. Now run your UI test. Android Studio will reinstall a fresh version of the test APK.

Using adb on the command line:

# Replace "com.company.app.test" with your APK test package ID
adb uninstall com.company.app.test

Using Gradle command-line:

./gradlew :app:uninstallAll

Upvotes: 22

Saeed.re
Saeed.re

Reputation: 775

In my scenario, there was a large file (700MB) in the resources directory, causing the size of the APK to be around 700MB. Consequently, the installation time of the APK was prolonged, leading to timeout exceptions when running the tests.

Failed to install APK(s): tests-debug.apk
Unknown platform error occurred when running the UTP test suite. Please check logs for details.
com.android.ddmlib.TimeoutException
    at com.android.ddmlib.AdbHelper.read(AdbHelper.java:1201)
    at com.android.ddmlib.SyncService.doPushFile(SyncService.java:745)
    at com.android.ddmlib.SyncService.pushFile(SyncService.java:439)
    at com.android.ddmlib.internal.DeviceImpl.syncPackageToDevice(DeviceImpl.java:1333)
    at com.android.ddmlib.internal.DeviceImpl.installPackage(DeviceImpl.java:1233)
    at com.android.tools.utp.plugins.deviceprovider.ddmlib.DdmlibAndroidDevice.installPackage(DdmlibAndroidDevice.kt)
    at com.android.tools.utp.plugins.deviceprovider.ddmlib.DdmlibAndroidDeviceController$executeAsync$deferred$1.invokeSuspend(DdmlibAndroidDeviceController.kt:192)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)

Removing the file from the resources solved my issue.

Upvotes: 0

Daniil
Daniil

Reputation: 1600

For me running adb kill-server solved the problem

Upvotes: 0

maged sabrelgamil
maged sabrelgamil

Reputation: 41

There may be some reasons for this error:

  1. The Android Gradle plugin version is not compatible with Gradle version.

  2. If you use wireless debugging try using USB debugging.

Upvotes: 4

César Muñoz
César Muñoz

Reputation: 645

In my case my emulator ran out of space, apparently the "testing apk" files are placed in the emulator before installing them, and they stay there after the test finishes. So I had to remove those files and then it worked again.

Upvotes: 3

GenError
GenError

Reputation: 1176

I found a solution. The problem is that the emulator takes a lot of time to start, so the instrumentation test fails because it cannot install the APK while the emulator is booting

Message: Failed to install APKs: UNKNOWN

I adapted the solution from a comment here Detect when Android emulator is fully booted to delay the next steps until the emulator is fully booted:

emulator -avd S21 -memory 2048 -wipe-data -no-boot-anim -cache-size 1000 -noaudio -no-window -partition-size 8192 &

adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;'

Upvotes: 3

Related Questions