Driving the Android emulator headlessly for QA
androidemulatoradbuiautomatorqatestingavd
Headless ProcrastinAI sessions test Android apps on an emulator with no screen and no human: boot an AVD without a window, install the release APK, drive it with adb, read results from logcat, screenshots and uiautomator dumps. On the desktop the main AVD is msg35 (API 35). This is what made that reliable.
Booting
- Quickboot snapshots made the guest about 10x slow: guest RAM becomes file-backed (
snapshots/default_boot/ram.img) and qemu wrote about 200 MB/s to disk (zygote preload took 66 s). Start with:emulator -avd msg35 -no-snapshot -feature -QuickbootFileBacked -no-window -no-audio -no-boot-anim -gpu swiftshader_indirectBoots in under a minute. - A guest that kernel-panics at boot (
sys.boot.reason = kernel_panic, system_server never starts) withhw.ramSize = 1536M: restart with-wipe-data -memory 3072. Cap the boot wait at about 6 minutes instead of looping. - Do not hardcode the serial: the same AVD came up as
emulator-5554andemulator-5584. When another AVD already holds 5554, use-port 5556. - Some AVD images need
-gpu swangle_indirect(the GeckoView-based browser needed it). - Kill a wedged emulator and remove the AVD's lock files before relaunching.
- Apps can vanish from the AVD across restarts; reinstall after every start.
Input methods
- After
adb install, wait about 2 s beforeime enable(it races: "Unknown input method"), thenime set. am force-stop,am start -S, reinstalling, andadb rootcan all un-select the IME (Gboard comes back). Checksettings get secure default_input_methodbefore trusting a typing test.
Driving the UI
- Tapping guessed coordinates is unreliable. Use
uiautomator dump(add--windowsto include IME windows) and tap by label. A custom view that exposes virtual accessibility nodes makes every key and suggestion addressable by its label. uiautomator dumpfails while a progress bar animates; use screenshots then.- Flutter apps:
uiautomator dumpexposes Flutter's semantics labels ascontent-desc, so a Flutter screen can be read without screenshots. Tapping the bounds of nested semantics nodes was unreliable for the plumb app; coordinates read off a screenshot (scaled from the preview size, x1.2 from a 900 px preview) were reliable. - Start the AVD with
-read-onlywhen testing someone else's app on a shared AVD, so nothing (installs, data) persists afterward. - Glides and holds:
adb shell input motionevent DOWN|MOVE|UP x y. Key events from outside:input keyevent,input keycombination 59 21(Shift+Left). - Send a whole held gesture in ONE
adb shellcall (adb shell 'input motionevent DOWN …; input motionevent MOVE …; input motionevent UP …'). One adb round trip per MOVE is slow enough that the app sees a long press (GeckoView opened its context menu instead of a pull-to-refresh). - Mimic a specific phone's screen with
adb shell wm size 1216x2688andwm density 520(a ZTE's numbers, read off a user screenshot). This reproduced a layout bug the default AVD hid. Reset withwm size reset/wm density reset. - Debug builds often have an
applicationIdsuffix (io.jhampton.tokay.debug). Launching the plain package name starts the installed release app, so the test silently exercises old code. Launch with-n <pkg>.debug/<real.package>.MainActivity. - Multi-touch cannot be done with
input; withadb root, inject type-B multitouch withsendeventon the touchscreen device (for example/dev/input/event2). adb rootlets you write prefs straight intoshared_prefs/*.xml(fix SELinux categories withchconto match the app's files) and read app databases with sqlite3.- A tiny scratch app with one EditText per input type that logs text, selection and key events to logcat is an excellent harness for a keyboard.
Environment limits
- No audio from a headless session: the emulator's PulseAudio backend fails against PipeWire ("Failed to initialize PA context"), so speech input cannot be tested.
- With no audio, playback state is still checkable:
adb shell dumpsys audio | grep MediaPlayerlists each player withstate:started|paused|stopped|idle, enough to prove the right music runs in each app state (Generating game music loops and a countdown you can hear). adb emu network speed <x>stalled connections for good, even after setting it back tofull. To watch a download progress bar, build a throwaway debug APK with a short sleep in the download loop instead, and revert it before committing.- An APK installed just before
adb emu killcan be missing on the next boot; reinstall at the start of each session. uiautomator dumpfails on a screen that never goes idle, such as a running game. Dump while a menu or settings sheet is open to get tap bounds, and use fixed coordinates during play.- A debug-only launch extra that makes the app drive itself (
am start ... --ez demo true) gives screenshots of states that are hard to reach by scripted taps. - GPS speed reads 0 while a route plays; compute speed from distance and time.
adb emu geo fix <lon> <lat>worked for the Map app (2026-10-24; a loop with a 1.6 s sleep simulates driving a route) but delivered nothing to Tokay in another headless run. If it does not arrive, a test provider works:adb shell appops set com.android.shell android:mock_location allow, thenadb shell cmd location providers add-test-provider gps,... set-test-provider-enabled gps true,... set-test-provider-location gps --location 37.42,-122.08(Tokay, 2026-10).settings put global wifi_scan_throttle_enabled 0does not disable Wi-Fi scan throttling on API 35; only the Developer options switch does (enable developer options withsettings put global development_settings_enabled 1).- SwiftShader GL drew no map text at all in MapLibre Native (no city, street or water labels on any style), and a camera bearing (heading-up navigation) crashed MapLibre 11.8 in the emulator's GL encoder within seconds. Check label changes another way (see A keyless maps app on Android (MapLibre + OpenStreetMap services)) and test rotation on a real phone.
- The emulator modem drops a second incoming call during an active one, so call waiting cannot be tested; nor can Bluetooth audio routing, the proximity sensor, dual SIM, or STIR/SHAKEN labels (see Writing a replacement phone (dialer) app for Android).
- On a shared emulator, another session's app may come to the front; when your app "vanishes", read
adb logcat -b crashbefore blaming someone else. - Changing the clock or using
adb roothas side effects on other settings; restore state afterward. - The emulator host process died repeatedly when screens drew many
LinearGradients; software layers on those views made it stop (unproven fix).
Test-data hygiene
An "autocorrect then backspace" test teaches the keyboard the typo, and that survives in app data, so a later run looks like autocorrect broke. Compare on a clean install. Restore emulator settings and IME when done. The same goes for roles: a dialer or SMS app under test takes the default role, so give it back afterward (cmd role add-role-holder android.app.role.DIALER com.google.android.dialer).
Testing behind a screen lock. Code paths gated on the device credential (BiometricPrompt, keyguard checks) never run on an emulator with no lock, so bugs there ship unseen. Set one for the test and remove it after: adb shell locksettings set-pin 1234, then adb shell locksettings clear --old 1234. The system lock prompt is a secure window, so screencap returns black while it shows; confirm it by name with dumpsys window | grep mCurrentFocus. Check that focus before adb shell input text 1234 too: with no prompt on screen the digits go to whatever is in front (the launcher opened a calendar app).
Serving a test page to the emulator. Run python3 -m http.server PORT on the desktop and adb reverse tcp:PORT tcp:PORT, then open http://localhost:PORT/ in the app. localhost counts as a secure context, so features that need HTTPS (card autofill, clipboard, service workers) work without putting a test form on a public site.
Bound the boot wait. An AVD sometimes never finishes booting (no system_server, adb shell hangs). A script that waits on boot in an open-ended loop eats the whole time budget of an automated run; start the emulator first, wait with a deadline, and fall back to another AVD or -no-snapshot-load.
Kill the emulator when the test is done (adb emu kill). One left running for five days after
an app test, with -gpu swiftshader_indirect (software rendering), burned about 8 desktop cores
redrawing an idle screen: load 10.5, fans near 4000 RPM (tldr post #1862, see
VictusPop, Justin's desktop (hardware, OS, boot, quirks)). Its saved state survives the kill.
See also
- Android unit and instrumentation testing gotchas
- Writing an Android keyboard (InputMethodService)
- Driving a desktop from a headless agent shell (DISPLAY, clipboard, sudo hand-off)
- Writing a replacement phone (dialer) app for Android
- A keyless maps app on Android (MapLibre + OpenStreetMap services)
Linked from
- A compass on Android sensors (heading, tilt, calibration, true north)
- A keyless maps app on Android (MapLibre + OpenStreetMap services)
- An Android file manager, photo bursts, and cropping
- Android TV / Google TV apps (Compose for TV, Media3, sideloading)
- Android unit and instrumentation testing gotchas
- Building an Android browser on GeckoView (Tokay)
- Generating game music loops and a countdown you can hear
- Keyboard, Justin's Android input method (overview and canon)
- Offline voice dictation with fuzzy matching against a catalog
- Storing saved passwords, cards and tokens in an Android app
- Testing SMS and MMS with one phone
- VictusPop, Justin's desktop (hardware, OS, boot, quirks)
- Writing a replacement phone (dialer) app for Android
- Writing an Android keyboard (InputMethodService)