In a CI job that boots Android emulators headless, how do you create the AVD, start it, and make each test run begin from a clean state?
answer
- image, then device, then boot
- a package path in quotes
- no window, no snapshot
- connected is not booted
- even console ports for parallel runs
basics
~20 sInstall the system image, create the AVD with avdmanager create avd -k, start it with emulator -no-window plus -no-snapshot or -wipe-data, and wait for boot completion rather than adb's device state before installing. Give parallel emulators distinct even console ports.
solid answer
~40 sScript it in three steps. First install the image with `sdkmanager "system-images;android-37;google_apis;x86_64"` and accept licenses with `sdkmanager --licenses`. Then create the AVD with `avdmanager create avd -n ci_api37 -k "system-images;android-37;google_apis;x86_64" -f`. Finally boot it with `emulator -avd ci_api37 -no-window -no-snapshot -no-boot-anim -accel on`. `-no-snapshot` gives a full boot that neither loads nor saves state, and `-wipe-data` resets the user-data image to factory state. The `device` state in `adb devices` appears while Android is still booting, so poll for boot completion before `adb install`. For parallel jobs, start each emulator with an even `-port` such as 5560 and address it as `emulator-5560`. All three SDK tools are now deprecated in favour of the Android CLI (`android sdk`, `android emulator`), although most existing scripts still call them.
code
console · 5 lines$ sdkmanager --licenses
$ sdkmanager "platform-tools" "system-images;android-37;google_apis;x86_64"
$ avdmanager create avd -n ci_api37 -k "system-images;android-37;google_apis;x86_64" -f
$ adb start-server
$ emulator -avd ci_api37 -port 5560 -no-window -no-snapshot -no-boot-anim -no-audio -accel on &go deeper
Recall the order of the steps: install a system image, create an AVD from it, then boot it with the emulator command and talk to it through adb.
Explain the snapshot flags against each other, what -wipe-data resets and leaves alone, and why a device line is not a booted device.
Show a CI script that is idempotent and isolated: forced AVD creation, no snapshots or a read-only named one, a boot-completed wait and distinct ports per job.
Weigh cold-boot isolation against snapshot speed for the whole suite, and plan the move from the deprecated SDK tools to the Android CLI.
## The three pieces An **AVD** (Android Virtual Device) combines a **hardware profile**, a **system image** (the Android build for one API level, variant and CPU architecture), a storage area and other properties. A headless CI job needs one image, one AVD and one boot, each scripted: 1. **Install the image.** Run `sdkmanager "system-images;android-37;google_apis;x86_64"`, with the package path in quotes. On a server without a GUI, run `sdkmanager --licenses` first to accept the licenses. 2. **Create the AVD.** Run `avdmanager create avd -n ci_api37 -k "system-images;android-37;google_apis;x86_64"`. `-f` overwrites an AVD of the same name, which keeps reruns idempotent. `-p` moves it out of the default `~/.android/avd/` directory. 3. **Boot it.** Run `emulator -avd ci_api37` with the startup options below. You can set startup options only at launch, never on a running emulator. `sdkmanager`, `avdmanager` and the `emulator` tool are all marked **deprecated** now. The documentation points to the Android CLI instead: `android sdk install`, and `android emulator create`, `start`, `stop` and `list`. Expect to meet both in existing pipelines. The image variant matters. Images that include the Play Store are signed with a release key, so `adb root` is impossible on them. If a job needs root, for example to pull `/data/anr` or open an app database, use an AOSP image without Google apps. ## Startup options that matter in CI | Option | Effect | |---|---| | `-no-window` | no graphical window. You reach the device through adb or the console | | `-no-snapshot` | disables Quick Boot completely: a full boot that neither loads nor saves state | | `-no-snapshot-load` | cold boots, but saves state on exit | | `-no-snapshot-save` | quick boots if it can, but does not save on exit | | `-snapshot <name>` | loads that named snapshot and saves it back under the same name on exit | | `-wipe-data` | copies `userdata.img` over `userdata-qemu.img`, removing every installed app and setting | | `-no-boot-anim`, `-no-audio` | faster boot, and fewer audio-driver failures on Linux and Windows | | `-memory <MB>` | RAM from 1536 to 8192 MB, overriding the AVD's setting | `-wipe-data` does not touch `sdcard.img`. If the emulated SD card must be clean too, delete and recreate that file. ## Snapshots: speed against isolation By default an AVD saves a **Quick Boot** snapshot on exit and loads it on the next start, which can boot up to 10 times faster than a cold boot. In CI that is a trade-off. A snapshot carries state from the last run, and it is valid only for the emulator version, system image and AVD configuration it was saved with. Update any of them and every snapshot of that AVD becomes invalid, so the next start is a cold boot. Use either `-no-snapshot` for strict isolation, or a named snapshot prepared once and loaded with `-snapshot`. Because `-snapshot` saves back under the same name on exit, add `-no-snapshot-save` so a run does not overwrite the prepared state. ## Waiting for a real boot and running in parallel - The emulator connects to adb **while Android is still booting**, so `device` in `adb devices` does not mean the system is ready. Poll the system's boot-completed property with `adb shell getprop` until it reports that boot has finished, and only then install or test. - Each emulator takes a **pair of ports**: an even console port and the next odd port for adb. The first is 5554/5555, then 5556/5557 and so on, up to 5682, which allows 64 instances. The serial is `emulator-<console port>`. - Pass an even `-port` (5560, 5562, …) per job and target it with `-s emulator-5560` or `ANDROID_SERIAL`. The adb server scans odd ports 5555 to 5585 for emulators. An emulator started on an odd console port in that range before the server is running can stay invisible to `adb devices`, so start the adb server first. ## Matching the job to the app Create one AVD per system image the app supports, guided by the `minSdk` declared in its manifest. An app never runs on an image whose API level is below that. ## Common mistakes - Installing as soon as `adb devices` prints `device`, then chasing install failures that are really an unfinished boot. - Relying on Quick Boot for isolation and carrying one run's logged-in account into the next. - Expecting `-wipe-data` to clear the emulated SD card as well. - Starting the emulator before the adb server on an odd console port, so the device never appears. - Letting an emulator or image update invalidate every cached snapshot without noticing that each job now cold boots.
- How do you make the CI emulator behave like a slow mobile network for one suite?Set it at launch: `-netspeed edge` caps upload and download speeds, and `-netdelay gsm` adds latency between 150 and 550 ms. Both apply to the emulated Ethernet and cellular link. `-netfast` restores the defaults, `-netspeed full -netdelay none`. These are startup options, so the job sets them when it launches the emulator.
- Two jobs share a runner. How do you keep their emulators and adb commands apart?Start each emulator with its own even console port, for example `-port 5560` and `-port 5562`, so their serials are `emulator-5560` and `emulator-5562`. Each job then exports `ANDROID_SERIAL` or passes `-s`. Without a target, adb stops with "more than one device/emulator".
- Your job needs to pull a file from `/data`. Why does `adb root` fail on your image?System images that include the Play Store are signed with a release key, so root is not available on them. Create the AVD from an AOSP system image without Google apps or services, which allows `adb root` and `adb unroot`.
A Quick Boot snapshot is like waking a laptop from sleep instead of powering it on: fast, but everything left open last time is still there. -no-snapshot is powering on every time and never letting it sleep, and -wipe-data is a factory reset before you switch it on.
- Once adb devices shows the emulator as device, it is ready for installs
- -wipe-data also resets the emulated SD card image
- -no-snapshot-load means the emulator never writes a snapshot
- Cached Quick Boot snapshots survive an emulator or system image update
- Any port works for -port, and odd console ports are just as visible to adb