#!/bin/bash # Boots the headless AVD (default: $EMULATOR_NAME, else `bench`) if it is not running, then prepares # the device. Idempotent: the preparation runs every time, also when the emulator was already up. export ANDROID_AVD_HOME=${ANDROID_AVD_HOME:-/opt/avd} AVD=${1:-${EMULATOR_NAME:-bench}} booted() { [ "$(adb shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')" = "1" ]; } if ! (adb get-state >/dev/null 2>&1 && booted); then ACCEL="-accel on"; [ -w /dev/kvm ] || { echo "WARNING: /dev/kvm not available, falling back to software emulation (very slow)"; ACCEL="-accel off"; } nohup emulator -avd "$AVD" -no-window -no-audio -no-boot-anim -no-snapshot -gpu swiftshader_indirect $ACCEL -memory 3072 > /tmp/emulator.log 2>&1 & adb wait-for-device for i in $(seq 1 180); do booted && break; sleep 2; done booted || { echo "emulator failed to boot"; tail -20 /tmp/emulator.log; exit 1; } fi # ATD system images boot with HWUI drawing off (debug.hwui.drawing_enabled=0): apps render nothing and # every screenshot is black. Turn it on and restart the framework so every process, SystemUI included, # draws; then hand adbd back unprivileged as it was. if [ "$(adb shell getprop debug.hwui.drawing_enabled 2>/dev/null | tr -d '\r')" = "0" ]; then adb root >/dev/null 2>&1; sleep 2; adb wait-for-device adb shell setprop debug.hwui.drawing_enabled 1 adb shell setprop sys.boot_completed 0 adb shell stop; adb shell start for i in $(seq 1 120); do booted && [ -n "$(adb shell pidof com.android.systemui 2>/dev/null)" ] && break; sleep 2; done booted || { echo "framework did not come back after enabling drawing"; exit 1; } adb unroot >/dev/null 2>&1; sleep 2; adb wait-for-device for i in $(seq 1 30); do booted && break; sleep 2; done sleep 10 fi # The sandbox cannot reach Google's connectivity probe; without this Android never marks the # network as validated and WorkManager jobs with a CONNECTED constraint never run. adb shell settings put global captive_portal_mode 0 adb shell svc wifi disable; adb shell svc data disable; sleep 2 adb shell svc wifi enable; adb shell svc data enable for i in $(seq 1 30); do adb shell dumpsys connectivity | grep -q "IS_VALIDATED" && break; sleep 2; done adb shell settings put global window_animation_scale 0 adb shell settings put global transition_animation_scale 0 adb shell settings put global animator_duration_scale 0 adb shell input keyevent KEYCODE_WAKEUP; adb shell wm dismiss-keyguard echo "emulator ready"