2

I'm trying to detect the successful bootup of an emulated android OS (Google's Android Emulator, sys version 12.0, running on Linux inside Docker) over adb in order to install an app right after boot. I tried the methods mentioned in this thread, but none of them seem to work. I tried checking for all these props in a loop:

  • sys.boot_completed
  • dev.bootcomplete
  • init.svc.bootanim
  • service.bootanim.exit

They continue returning 0 even after the device is fully booted up and never return 1.

I also tried adb wait-for-device, but that finished immediately, even though I'm using the "-delay-adb" flag on my emulator (and if I don't use it, the behavior is still the same). Attempting to install the APK before the boot is finished (right after wait-for-device exits) results in an installation error:

adb: failed to install app.apk: Exception occurred while executing 'install': java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.pm.PackageManagerInternal.freeStorage(..) on a null object reference'

It I wait for the device to boot and then manually trigger the installation, or if I add a long enough wait time before the installation is attempted, it works without any problems. But I don't want to add an arbitrarily long sleep because this may run on different devices which take different amounts of time to boot up.

Did anybody encounter this behavior? Do you have any suggestions on how to detect bootup without having to write my own app with a BOOT_COMPLETE listener? More abstractly, does anybody have a different suggestion on how to install an app in the Android emulator right after boot?

Dex
  • 164
  • 1
  • 2
  • 18
  • Maybe `adb wait-for-device` ? – TDG Oct 29 '22 at 08:17
  • If a device is available via adb you should be able to perform an app installation via adb. A device may become visible via adb when the boot process is not yet finished but that shouldn't make a difference for the installation process. – Robert Oct 29 '22 at 11:46
  • Unfortunately wait-for-device completes as soon as the boot process starts, too early for installation to be possible – Dex Oct 30 '22 at 12:29
  • `adb wait-for-device` finished immediately, even though I'm using the "-delay-adb" flag on my emulator. – Dex Nov 13 '22 at 07:18

1 Answers1

1

It's a quite an ugly solution, but you could always just attempt to install the application in a loop with some small delay and wait until it succeeds. It's not pretty, but should do the job.

You said that you receive a specific exception when you attempt to install an app during booting. Assuming the same exception is unlikely to happen after booting the phone for some unrelated reason, it should be mostly safe approach to use.

Another thing that comes up to my mind is looking at network traffic, I'd expect android to call home right after booting the system, but that seems much more complicated, and maybe even less reliable, than the "install in a loop" idea.

SoptikHa
  • 437
  • 4
  • 19
  • Thanks! It never occurred to me that I can just repeatedly attempt to install it until it succeeds. I decided to go with this solution because it seems to be most reliable of all things I tried so far. – Dex Nov 14 '22 at 01:49