Search code examples
androidandroid-emulatoradbboot

Detecting that android has booted up over adb


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?


Solution

  • 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.