[FIXED] Android instrumentation test fails to install APK

Issue

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?

Solution

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;'

Answered By – GenError

Answer Checked By – Terry (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published