Back to blog

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

Prasandeep

Published 8 min readCI/CD
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:

Bash
adb shell getprop sys.boot_completed

returns 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:

  1. No KVM — the x86_64 emulator falls back to software virtualization. Frames and boot stretch into minutes.
  2. Cold AVD every job — SDK download + avdmanager create + first boot with no snapshot.
  3. 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:

Yaml
- 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

Sanity check in the same job:

Yaml
- name: Verify KVM run: | ls -l /dev/kvm # Expect a character device the runner can open

If /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:

Clike
-no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
FlagWhy it helps in CI
-no-windowNo display server dependency; true headless
-gpu swiftshader_indirectSoftware GPU path that works on cloud VMs without a real GPU
-noaudioSkips audio host setup
-no-boot-animCuts animated boot chrome
-camera-back noneAvoids 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):

Yaml
- 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 connectedDebugAndroidTest

On 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:

  1. Prefer ATD (Automated Test Device) or default / google_apis x86_64 images without Play Store when your tests do not need Play services.
  2. Pin one primary API (for example 30 or 34) on every PR; run a wider API matrix nightly.
  3. 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:

  1. Cache Gradle with gradle/actions/setup-gradle so connectedCheck is not a cold daemon + dependency download.
  2. Install only the SDK packages you need (platform, build-tools, emulator, one system image). Avoid “install everything” SDK scripts.
  3. Disable animations in the runner (disable-animations: true) so Espresso waits less.
  4. Pre-grant permissions in the test script when the suite always needs them, instead of clicking dialogs on every run.
  5. Shard long suites across jobs (matrix by package or class) so one emulator is not the single bottleneck.

Full starter workflow

Yaml
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 connectedDebugAndroidTest

Log 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:

Bash
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

SymptomLikely causeFix
Boot still 2–5+ minutesNo /dev/kvmEnable udev rule; confirm device node
Fast first minute, then hangWaiting on Play Services / wizardUse ATD/default; disable boot anim; snapshot after first setup
Cache hit but slow bootSnapshot not used / wrong optionsDrop -no-snapshot on create; use -no-snapshot-save only on test boot; bump cache key after option changes
Flaky “device offline”Emulator killed early / OOMRaise job timeout; lower emulator RAM if the runner is tight; one emulator per job
Good boot, slow suiteCold Gradle / huge connected suiteGradle cache + shard tests
Works locally, fails on GHAGPU flag / nested virt differenceStick to swiftshader_indirect on Linux GHA

What not to optimize first

  1. Buying a device farm before KVM + snapshot cache exist — you may not need it for PR smoke.
  2. Parallelizing five emulators on one small runner — they fight for CPU and make boot worse.
  3. Chasing API 21–35 on every PR — one API for PRs, wider matrix nightly.
  4. Turning off flakes by sleeping 60s after boot — fix readiness (boot_completed + package manager) instead.

Decision cheat sheet

GoalChoice
PR smoke under tight minutesLinux + KVM + cached snapshot + one API + ATD/default
Play Billing / MapsSeparate job with Play image; keep main shard lean
Nightly compatibilityMatrix of API levels; accept longer boots on cold cells
Need physical hardwareDevice farm or self-hosted; keep emulator path for the rest

Minimal checklist

  1. Confirm /dev/kvm on the job.
  2. Use ubuntu-latest + reactivecircus/android-emulator-runner (or equivalent) with headless options.
  3. Cache ~/.android/avd and seed a snapshot on cache miss.
  4. Boot tests with -no-snapshot-save and disable-animations: true.
  5. Prefer x86_64 ATD/default images; isolate Play-only needs.
  6. Cache Gradle; shard long suites.
  7. Log seconds to sys.boot_completed and 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.

ShareLinkedInPost on X

Get the next guide in your inbox

Weekly AI testing insights, templates, and tool notes — unsubscribe anytime.