Search code examples
androidshellandroid-studioandroid-emulatoradb

How to accept debugging dialog for Android Emulator via command line


I'm writing an automated emulator script that will create and launch an Android emulator so I can run my UI tests from any machine and guarantee that it'll run on a device.

My current script creates an android-27;google_apis;x86 device which works fine, but lacks google services so the maps in my app do not show.

I tried creating an emulator using google_apis_playstore, but when the device boots, it prompts with an ADB debugging prompt. Normally tapping this would be fine, but Im expecting to be able to run this on a headless server and wont always be able to.

Is there anyway to create the emulator that will have google apis + maps without having to accept an ADB dialog?

Here's my current shell script

#!/bin/sh
# Run this script in root project dir

# Kill existing emulator
$ANDROID_HOME/platform-tools/adb devices | grep emulator | cut -f1 | while read line; do $ANDROID_HOME/platform-tools/adb -s $line emu kill; done

# Install system image
$ANDROID_HOME/tools/bin/sdkmanager "system-images;android-27;google_apis;x86"
yes | $ANDROID_HOME/tools/bin/sdkmanager --licenses

# Create emulator
echo "no" | $ANDROID_HOME/tools/bin/avdmanager create avd -f \
    -n "tester" \
    -k 'system-images;android-27;google_apis;x86' \
    -b x86 \
    -d "Nexus 5X"

# Start emulator
$ANDROID_HOME/emulator/emulator -avd tester &

# Wait for emulator to start
$ANDROID_HOME/platform-tools/adb wait-for-device shell input keyevent 82
while [ "`$ANDROID_HOME/platform-tools/adb shell getprop sys.boot_completed | tr -d '\r' `" != "1" ] ; do sleep 1; done
sleep 5;

I get an error when using google_apis_playstore because it can't adb in to check if the emulator has started because of the dialog.

error: device unauthorized.
This adb server's $ADB_VENDOR_KEYS is not set
Try 'adb kill-server' if that seems wrong.
Otherwise check for a confirmation dialog on your device.

Seems silly that you would need to accept debugging permissions for an emulator?

Edit: I have submitted this as a bug https://issuetracker.google.com/issues/128271326


Solution

  • Unlike default or google_apis, google_apis_playstore is enforcing authentication. It means that both adb server on host and adb daemon at emulator should share same RSA adbkey for communication. Documentation is here: https://developer.android.com/studio/command-line/adb

    Typically it works automatically. adb start-server command will create adbkey files and then emulator/emulator will copy them into image filesystem. But since it doesn't work in your case you would have to carefully verify where things got misconfigured:

    • Running adb keygen adbkey generates 2 files - adbkey and adbkey.pub
    • These 2 files needs to be copied to $HOME/.android folder (alternatively $ANDROID_VENDOR_KEYS) for adb server on your HOST
    • Same 2 files needs to be copied to $HOME/.android folder (alternatively $ANDROID_SDK_HOME/.android) for emulator/emulator on your GUEST. Typically HOST==GUEST but if you are running adb on desktop and emulator inside docker container they are different.
    • Filesystem for emulator is cached so remove any previously created images. They wouldn't have key copied over.
    • Optionally, it is suggested here https://developer.android.com/studio/command-line/adb#notlisted to call adb start-server explicitly before issuing any adb commands
    $ adb kill-server
    $ emulator -avd Nexus_6_API_25 -port 5557
    $ adb start-server
    $ adb devices
    
    List of devices attached
    emulator-5557 device