Headless Android Emulator Optimization in GitHub Actions: Cutting Boot Times Under 30 Seconds

Your instrumentation job is not “slow because Espresso is slow.” It is slow because the emulator boots cold, animates a home screen nobody watches, and burns five to fifteen minutes before connectedCheck starts. On GitHub Actions that waste shows up as queue time, billable minutes, and flakes when the suite races a half-ready system.
This guide shows how to get emulator boot under ~30 seconds on GitHub-hosted Linux runners: KVM, headless flags, AVD snapshot cache, lean system images, and a timing check you can put in the log. It pairs with GitHub Actions + Playwright CI/CD for pipeline hygiene and Fix Flaky Tests: 2026 Masterclass when the remaining flakes are timing, not boot. For Flutter suites that still need an emulator, see Patrol vs Maestro.
What “under 30 seconds” means
Measure one thing: wall clock from emulator -avd … (or the action’s launch) until:
adb shell getprop sys.boot_completedreturns 1, and adb shell pm path android succeeds. That is boot ready, not “tests finished.” Package install, Gradle, and Espresso still add time after that.
Community measurements on GitHub Linux after KVM landed often land around ~15–30s for a warm / accelerated boot versus 2+ minutes without hardware accel, and much worse on a cold AVD with no snapshot. Treat “under 30s” as a boot target, not a promise that every matrix cell will hit it on day one.
Why CI emulators used to crawl
Three separate costs stack:
- No KVM — the x86_64 emulator falls back to software virtualization. Frames and boot stretch into minutes.
- Cold AVD every job — SDK download +
avdmanager create+ first boot with no snapshot. - Headful defaults — window, boot animation, audio, and heavy Google APIs images do work you do not need in CI.
GitHub enabled hardware-accelerated Android virtualization on hosted Linux runners (including smaller 2-vCPU runners as of 2024). You still have to open /dev/kvm for the job and design the AVD for reuse. The action that most teams use is reactivecircus/android-emulator-runner.
Step 1: Enable KVM on ubuntu-latest
Add this before any emulator step:
- name: Enable KVM group perms
run: |
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvmSanity check in the same job:
- name: Verify KVM
run: |
ls -l /dev/kvm
# Expect a character device the runner can openIf /dev/kvm is missing, you are on a runner without nested virt. Stop optimizing flags and switch runner class (larger GitHub Linux, or a self-hosted/KVM host). Flags alone will not hit 30s in pure software mode.
Prefer ubuntu-latest (Linux) over macOS for Android emulator CI: Linux + KVM is usually faster and cheaper than macOS runners for the same Espresso / Compose UI work.
Step 2: Run headless with lean emulator options
Headless means no GUI window and no decorative boot work. Typical options used with android-emulator-runner:
-no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none| Flag | Why it helps in CI |
|---|---|
-no-window | No display server dependency; true headless |
-gpu swiftshader_indirect | Software GPU path that works on cloud VMs without a real GPU |
-noaudio | Skips audio host setup |
-no-boot-anim | Cuts animated boot chrome |
-camera-back none | Avoids camera host plumbing you do not use |
When you load a cached snapshot for tests, add -no-snapshot-save so the job does not rewrite the snapshot every run (faster teardown, stable cache). When you create the snapshot once, omit -no-snapshot-save so a clean boot is written.
Do not copy macOS/-gpu host recipes blindly onto GitHub Linux VMs. swiftshader_indirect is the usual safe default there.
Step 3: Cache the AVD snapshot (the big win)
Downloading the system image and doing a first boot every PR is what keeps you above a minute. Cache:
~/.android/avd~/.android/adb*(optional but common)
Pattern with actions/cache + android-emulator-runner (API level in the cache key):
- name: AVD cache
uses: actions/cache@v4
id: avd-cache
with:
path: |
~/.android/avd/*
~/.android/adb*
key: avd-${{ runner.os }}-${{ matrix.api-level }}-x86_64-atd-v1
- name: Create AVD and generate snapshot
if: steps.avd-cache.outputs.cache-hit != 'true'
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: ${{ matrix.api-level }}
arch: x86_64
target: default
force-avd-creation: false
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
disable-animations: true
script: echo "Snapshot generated"
- name: Run tests from snapshot
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: ${{ matrix.api-level }}
arch: x86_64
target: default
force-avd-creation: false
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none -no-snapshot-save
disable-animations: true
script: ./gradlew connectedDebugAndroidTestOn a cache hit, the create step is skipped and the test step boots from the warm snapshot. On a miss, you pay one slow job to seed the cache; later jobs should land in the boot budget if KVM is on.
Bump the cache key (-v1 → -v2) when you change API level, arch, target image, or emulator options that affect the snapshot.
Step 4: Prefer ATD / default images over Play Store skins
Google Play system images are heavier and often slower to boot. For most instrumentation:
- Prefer ATD (Automated Test Device) or default / google_apis x86_64 images without Play Store when your tests do not need Play services.
- Pin one primary API (for example 30 or 34) on every PR; run a wider API matrix nightly.
- Use x86_64, not ARM images, on x86 GitHub runners.
If a test needs Play Billing or Maps, isolate that shard on a Play image instead of forcing Play onto every job.
Step 5: Shrink everything around the emulator
Boot is only half the wall clock. Also:
- Cache Gradle with
gradle/actions/setup-gradlesoconnectedCheckis not a cold daemon + dependency download. - Install only the SDK packages you need (platform, build-tools, emulator, one system image). Avoid “install everything” SDK scripts.
- Disable animations in the runner (
disable-animations: true) so Espresso waits less. - Pre-grant permissions in the test script when the suite always needs them, instead of clicking dialogs on every run.
- Shard long suites across jobs (matrix by package or class) so one emulator is not the single bottleneck.
Full starter workflow
name: Android instrumented
on:
pull_request:
push:
branches: [main]
jobs:
android-test:
runs-on: ubuntu-latest
timeout-minutes: 45
strategy:
fail-fast: false
matrix:
api-level: [30]
steps:
- uses: actions/checkout@v4
- name: Enable KVM group perms
run: |
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm
ls -l /dev/kvm
- name: Set up JDK
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4
- name: AVD cache
uses: actions/cache@v4
id: avd-cache
with:
path: |
~/.android/avd/*
~/.android/adb*
key: avd-${{ runner.os }}-${{ matrix.api-level }}-x86_64-default-v1
- name: Create AVD and generate snapshot
if: steps.avd-cache.outputs.cache-hit != 'true'
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: ${{ matrix.api-level }}
arch: x86_64
force-avd-creation: false
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
disable-animations: true
script: echo "AVD snapshot ready"
- name: Boot timing + tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: ${{ matrix.api-level }}
arch: x86_64
force-avd-creation: false
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none -no-snapshot-save
disable-animations: true
script: |
echo "boot_completed=$(adb shell getprop sys.boot_completed | tr -d '\r')"
./gradlew connectedDebugAndroidTestLog boot_completed (and optionally timestamps around the action) so you can prove the boot budget instead of guessing from total job duration.
Timing the boot yourself (manual / self-hosted)
If you launch the emulator without the action wrapper:
START=$(date +%s)
emulator -avd ci_api30 -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -no-snapshot-save &
adb wait-for-device
until [[ "$(adb shell getprop sys.boot_completed | tr -d '\r')" == "1" ]]; do
sleep 1
done
END=$(date +%s)
echo "Boot seconds: $((END - START))"Use that number in PRs when someone claims “the emulator is fine.” Separating boot seconds from Gradle + test seconds stops wrong optimizations (for example rewriting Espresso when the real cost is a cold AVD).
Failure modes that keep you above 30 seconds
| Symptom | Likely cause | Fix |
|---|---|---|
| Boot still 2–5+ minutes | No /dev/kvm | Enable udev rule; confirm device node |
| Fast first minute, then hang | Waiting on Play Services / wizard | Use ATD/default; disable boot anim; snapshot after first setup |
| Cache hit but slow boot | Snapshot not used / wrong options | Drop -no-snapshot on create; use -no-snapshot-save only on test boot; bump cache key after option changes |
| Flaky “device offline” | Emulator killed early / OOM | Raise job timeout; lower emulator RAM if the runner is tight; one emulator per job |
| Good boot, slow suite | Cold Gradle / huge connected suite | Gradle cache + shard tests |
| Works locally, fails on GHA | GPU flag / nested virt difference | Stick to swiftshader_indirect on Linux GHA |
What not to optimize first
- Buying a device farm before KVM + snapshot cache exist — you may not need it for PR smoke.
- Parallelizing five emulators on one small runner — they fight for CPU and make boot worse.
- Chasing API 21–35 on every PR — one API for PRs, wider matrix nightly.
- Turning off flakes by sleeping 60s after boot — fix readiness (
boot_completed+ package manager) instead.
Decision cheat sheet
| Goal | Choice |
|---|---|
| PR smoke under tight minutes | Linux + KVM + cached snapshot + one API + ATD/default |
| Play Billing / Maps | Separate job with Play image; keep main shard lean |
| Nightly compatibility | Matrix of API levels; accept longer boots on cold cells |
| Need physical hardware | Device farm or self-hosted; keep emulator path for the rest |
Minimal checklist
- Confirm
/dev/kvmon the job. - Use
ubuntu-latest+reactivecircus/android-emulator-runner(or equivalent) with headless options. - Cache
~/.android/avdand seed a snapshot on cache miss. - Boot tests with
-no-snapshot-saveanddisable-animations: true. - Prefer x86_64 ATD/default images; isolate Play-only needs.
- Cache Gradle; shard long suites.
- Log seconds to
sys.boot_completedand treat that as the boot SLA.
Get KVM and the snapshot cache right first. Most teams that miss “under 30 seconds” are still paying for a cold AVD or software virtualization—not for Espresso itself.
Get the next guide in your inbox
Weekly AI testing insights, templates, and tool notes — unsubscribe anytime.