blob: 91844dc4dad6a03ca1bf04e8e27048900220b9fc [file] [log] [blame] [view]
Andrew Grieveae094e392018-06-15 16:10:221# Using an Android Emulator
Nate Fischer16f94532019-03-27 20:51:072Always use x86 emulators (or x86\_64 for testing 64-bit APKs). Although arm
3emulators exist, they are so slow that they are not worth your time.
Andrew Grieveae094e392018-06-15 16:10:224
Nate Fischer4a494582020-03-12 18:08:515[TOC]
Nate Fischeracbbaab2019-04-23 16:46:206
Andrew Grieveae094e392018-06-15 16:10:227## Building for Emulation
8You need to target the correct architecture via GN args:
Nate Fischer7cbdeae2019-01-24 20:29:159```gn
Nate Fischer16f94532019-03-27 20:51:0710target_cpu = "x86" # or "x64" if you have an x86_64 emulator
Andrew Grieveae094e392018-06-15 16:10:2211```
12
John Budorick041a7df2020-04-03 23:20:2713## Running an Emulator
14
Erik Chen254ec1cc2024-01-18 02:11:4915### Googler-only Emulator Instructions
16
17See http://go/clank-emulator/
18
Haiyang Pan03b1c6c2025-01-08 23:34:0719### Using Prebuilt AVD configurations.
John Budorick041a7df2020-04-03 23:20:2720
Haiyang Pan03b1c6c2025-01-08 23:34:0721Chromium has a set of prebuilt AVD configurations stored as CIPD packages.
22These are used by various builders to run tests on the emulator. Their
23configurations are currently stored in [`//tools/android/avd/proto`](../tools/android/avd/proto/).
Andrew Grieve1b9be142022-02-10 04:07:0024You can run this command to list them:
25```sh
26tools/android/avd/avd.py list
27```
John Budorick041a7df2020-04-03 23:20:2728
Haiyang Pan03b1c6c2025-01-08 23:34:0729| Configurations | Android Version | Form Factor | Builder |
Haiyang Pan0fca0dc2024-03-25 17:50:3030|:-------------- |:--------------- |:------- |:---------- |:------- |
Haiyang Pan03b1c6c2025-01-08 23:34:0731| `generic_android26.textpb` | 8.0 (O) | Phone | [android-oreo-x86-rel][android-oreo-x86-rel] |
32| `generic_android27.textpb` | 8.1 (O_MR1) | Phone | N/A |
33| `android_28_google_apis_x86.textpb` | 9 (P) | Phone | [android-pie-x86-rel][android-pie-x86-rel] |
34| `android_29_google_apis_x86.textpb` | 10 (Q) | Phone | N/A |
35| `android_30_google_apis_x86.textpb` | 11 (R) | Phone | [android-11-x86-rel][android-11-x86-rel] |
36| `android_31_google_apis_x64.textpb` | 12 (S) | Phone | [android-12-x64-rel][android-12-x64-rel] |
37| `android_32_google_apis_x64_foldable.textpb` | 12L (S_V2) | Foldable Phone | [android-12l-x64-dbg-tests][android-12l-x64-dbg-tests] |
38| `android_32_google_apis_x64_foldable_landscape.textpb` | 12L (S_V2) | Foldable Phone (Landscape) | [android-12l-landscape-x64-dbg-tests][android-12l-landscape-x64-dbg-tests] |
39| `android_33_google_apis_x64.textpb` | 13 (T) | Phone | [android-13-x64-rel][android-13-x64-rel] |
40| `android_34_google_apis_x64.textpb` | 14 (U) | Phone | [android-14-x64-rel][android-14-x64-rel] |
41| `android_35_google_apis_x64.textpb` | 15 (V) | Phone | [android-15-x64-rel][android-15-x64-rel] |
42| `android_35_google_apis_x64_tablet.textpb` | 15 (V) | Tablet | [android-15-tablet-x64-dbg-tests][android-15-tablet-x64-dbg-tests] |
43| `android_35_google_apis_x64_tablet_landscape.textpb` | 15 (V) | Tablet (Landscape) | [android-15-tablet-landscape-x64-dbg-tests][android-15-tablet-landscape-x64-dbg-tests] |
John Budorick041a7df2020-04-03 23:20:2744
45You can use these configuration files to run the same emulator images locally.
46
Haiyang Pan03b1c6c2025-01-08 23:34:0747[android-oreo-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-oreo-x86-rel
John Budorick041a7df2020-04-03 23:20:2748[android-pie-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-pie-x86-rel
Haiyang Panb06f846d2021-12-21 17:41:5549[android-11-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-11-x86-rel
Haiyang Pan2a55a452022-03-03 08:18:3350[android-12-x64-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-12-x64-rel
Haiyang Pan0fca0dc2024-03-25 17:50:3051[android-12l-x64-dbg-tests]: https://ci.chromium.org/p/chromium/builders/ci/android-12l-x64-dbg-tests
Haiyang Pan03b1c6c2025-01-08 23:34:0752[android-12l-landscape-x64-dbg-tests]: https://ci.chromium.org/p/chromium/builders/ci/android-12l-landscape-x64-dbg-tests
Haiyang Pan0fca0dc2024-03-25 17:50:3053[android-13-x64-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-13-x64-rel
54[android-14-x64-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-14-x64-rel
Haiyang Pan03b1c6c2025-01-08 23:34:0755[android-15-x64-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-15-x64-rel
56[android-15-tablet-x64-dbg-tests]: https://ci.chromium.org/ui/p/chromium/builders/ci/android-15-tablet-x64-dbg-tests
57[android-15-tablet-landscape-x64-dbg-tests]: https://ci.chromium.org/ui/p/chromium/builders/ci/android-15-tablet-landscape-x64-dbg-tests
58
59> Note: New AVD configurations for LOCAL development are also available now,
60> whose file names end with `_local`, i.e. `*_local.textpb`.
61>
62> The only difference is that these configs has 12GB storage space instead of
63> 4GB on the non-local ones. Larger storage space makes it easier for developers
64> to install large APKs without hitting the space issue.
John Budorick041a7df2020-04-03 23:20:2765
Haiyang Panf5faf232020-11-06 18:10:3466#### Prerequisite
67
68 * Make sure KVM (Kernel-based Virtual Machine) is enabled.
69 See this
70 [link](https://developer.android.com/studio/run/emulator-acceleration#vm-linux)
71 from android studio for more details and instructions.
72
73 * You need to have the permissions to use KVM.
74 Use the following command to see if you are in group `kvm`:
75
76 ```
77 $ grep kvm /etc/group
78 ```
79
80 If your username is not shown in the group, add yourself to the group:
81
82 ```
83 $ sudo adduser $USER kvm
84 $ newgrp kvm
85 ```
86
Erik Chen254ec1cc2024-01-18 02:11:4987 You need to log out and log back in so the new groups take effect.
Peter Wen96981e202021-08-06 21:13:4088
John Budorick041a7df2020-04-03 23:20:2789#### Running via the test runner
90
91The android test runner can run emulator instances on its own. In doing so, it
92starts the emulator instances, runs tests against them, and then shuts them
93down. This is how builders run the emulator.
94
95##### Options
96
97 * `--avd-config`
98
99 To have the test runner run an emulator instance, use `--avd-config`:
100
101 ```
102 $ out/Debug/bin/run_base_unittests \
Haiyang Pan56581b32024-07-03 20:46:40103 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb
John Budorick041a7df2020-04-03 23:20:27104 ```
105
106 * `--emulator-count`
107
108 The test runner will launch one instance by default. To have it run multiple
109 instances, use `--emulator-count`:
110
111 ```
112 $ out/Debug/bin/run_base_unittests \
Haiyang Pan56581b32024-07-03 20:46:40113 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
John Budorick041a7df2020-04-03 23:20:27114 --emulator-count 4
115 ```
116
sbingler8d91e8c2024-04-23 16:36:58117 * `--emulator-enable-network`
118
119 The test runner runs the emulator without network access by default. To have
120 it run with network access, use `--emulator-enable-network`:
121
122 ```
123 $ out/Debug/bin/run_base_unittests \
Haiyang Pan56581b32024-07-03 20:46:40124 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
sbingler8d91e8c2024-04-23 16:36:58125 --emulator-enable-network
126 ```
127
John Budorick041a7df2020-04-03 23:20:27128 * `--emulator-window`
129
130 The test runner runs the emulator in headless mode by default. To have it run
131 with a window, use `--emulator-window`:
132
133 ```
134 $ out/Debug/bin/run_base_unittests \
Haiyang Pan56581b32024-07-03 20:46:40135 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
John Budorick041a7df2020-04-03 23:20:27136 --emulator-window
137 ```
138
139#### Running standalone
140
141The test runner will set up and tear down the emulator on each invocation.
142To manage emulator lifetime independently, use `tools/android/avd/avd.py`.
143
John Budorick041a7df2020-04-03 23:20:27144##### Options
145
146 * `--avd-config`
147
148 This behaves the same as it does for the test runner.
149
150 ```
151 $ tools/android/avd/avd.py start \
Haiyang Pan56581b32024-07-03 20:46:40152 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb
John Budorick041a7df2020-04-03 23:20:27153 ```
154
155 > Note: `avd.py start` will start an emulator instance and then terminate.
Ian Struiksmae6fcbf02021-05-03 23:18:06156 > To shut down the emulator, use `adb emu kill`.
John Budorick041a7df2020-04-03 23:20:27157
sbingler8d91e8c2024-04-23 16:36:58158 * `--enable-network`
159
160 Like the test runner, `avd.py` runs the emulator without network access by
161 default. To enable network access, use `--enable-network`:
162
163 ```
164 $ tools/android/avd/avd.py start \
Haiyang Pan56581b32024-07-03 20:46:40165 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
sbingler8d91e8c2024-04-23 16:36:58166 --enable-network
167 ```
168
John Budorick041a7df2020-04-03 23:20:27169 * `--emulator-window`
170
171 Like the test runner, `avd.py` runs the emulator in headless mode by default.
172 To have it run with a window, use `--emulator-window`:
173
174 ```
175 $ tools/android/avd/avd.py start \
Haiyang Pan56581b32024-07-03 20:46:40176 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
John Budorick041a7df2020-04-03 23:20:27177 --emulator-window
178 ```
179
Haiyang Panb06f846d2021-12-21 17:41:55180 * `--gpu-mode GPU_MODE`
181
182 Override the mode of hardware OpenGL ES emulation indicated by the AVD.
183 See "emulator -help-gpu" for a full list of modes.
184
185 * `--no-read-only`
186
187 `avd.py` runs the emulator in read-only mode by default. To run a modifiable
188 emulator, use `--no-read-only`:
189
190 ```
191 $ tools/android/avd/avd.py start \
Haiyang Pan56581b32024-07-03 20:46:40192 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
Haiyang Panb06f846d2021-12-21 17:41:55193 --no-read-only
194 ```
195
Peter Wenb105afb2021-12-07 20:24:36196 * `--wipe-data`
197
Haiyang Pane5e2bc02023-10-20 19:31:53198 Reset the /data partition to the factory defaults. This removes all user
199 settings from the AVD.
Peter Wenb105afb2021-12-07 20:24:36200
201 ```
202 $ tools/android/avd/avd.py start \
Haiyang Pan56581b32024-07-03 20:46:40203 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
Peter Wenb105afb2021-12-07 20:24:36204 --wipe-data
205 ```
206
Haiyang Panb06f846d2021-12-21 17:41:55207 * `--writable-system`
John Budorick041a7df2020-04-03 23:20:27208
Victor Vianna725b1072022-03-04 16:02:59209 Makes system & vendor image writable. It's necessary to run
210 ```
211 adb root
212 adb remount
213 ```
214 after the emulator starts.
John Budorick041a7df2020-04-03 23:20:27215
Haiyang Panf5faf232020-11-06 18:10:34216 * `--debug-tags`
217
218 `avd.py` disables the emulator log by default. When this option is used,
219 emulator log will be enabled. It is useful when the emulator cannot be
220 launched correctly. See `emulator -help-debug-tags` for a full list of tags.
Haiyang Pan7c2d20f2024-02-20 21:59:28221 Use `--debug-tags=all` if you want to output all logs (warning: it is quite
Peter Wenb105afb2021-12-07 20:24:36222 verbose).
Haiyang Panf5faf232020-11-06 18:10:34223
224 ```
225 $ tools/android/avd/avd.py start \
Haiyang Pan56581b32024-07-03 20:46:40226 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
Haiyang Panf5faf232020-11-06 18:10:34227 --debug-tags init,snapshot
228 ```
229
John Budorick041a7df2020-04-03 23:20:27230### Using Your Own Emulator Image
231
Andrew Grieveae094e392018-06-15 16:10:22232By far the easiest way to set up emulator images is to use Android Studio.
233If you don't have an [Android Studio project](android_studio.md) already, you
234can create a blank one to be able to reach the Virtual Device Manager screen.
235
236Refer to: https://developer.android.com/studio/run/managing-avds.html
237
238Where files live:
239 * System partition images are stored within the sdk directory.
240 * Emulator configs and data partition images are stored within
241 `~/.android/avd/`.
242
John Budorick041a7df2020-04-03 23:20:27243#### Creating an Image
244
245##### Choosing a Skin
246
Nate Fischer7cbdeae2019-01-24 20:29:15247Choose a skin with a small screen for better performance (unless you care about
248testing large screens).
Andrew Grieveae094e392018-06-15 16:10:22249
John Budorick041a7df2020-04-03 23:20:27250##### Choosing an Image
251
Nate Fischer7cbdeae2019-01-24 20:29:15252Android Studio's image labels roughly translate to the following:
253
Nate Fischeracbbaab2019-04-23 16:46:20254| AVD "Target" | Virtual Device Configuration tab | GMS? | Build Properties |
255| --- | --- | --- | --- |
256| Google Play | "Recommended" (the default tab) | This has GMS | `user`/`release-keys` |
257| Google APIs | "x86 Images" | This has GMS | `userdebug`/`dev-keys` |
258| No label | "x86 Images" | AOSP image, does not have GMS | `eng`/`test-keys` |
Nate Fischer7cbdeae2019-01-24 20:29:15259
260*** promo
Nate Fischeracbbaab2019-04-23 16:46:20261**Tip:** if you're not sure which to use, choose **Google APIs** under the **x86
262Images** tab in the Virtual Device Configuration wizard.
Nate Fischer7cbdeae2019-01-24 20:29:15263***
264
John Budorick041a7df2020-04-03 23:20:27265##### Configuration
266
Nate Fischer7cbdeae2019-01-24 20:29:15267"Show Advanced Settings" > scroll down:
268* Set internal storage to 4000MB (component builds are really big).
269* Set SD card to 1000MB (our tests push a lot of files to /sdcard).
270
John Budorick041a7df2020-04-03 23:20:27271##### Known Issues
272
Andrew Grieveae094e392018-06-15 16:10:22273 * Our test & installer scripts do not work with pre-MR1 Jelly Bean.
274 * Component builds do not work on pre-KitKat (due to the OS having a max
275 number of shared libraries).
276 * Jelly Bean and KitKat images sometimes forget to mount /sdcard :(.
277 * This causes tests to fail.
278 * To ensure it's there: `adb -s emulator-5554 shell mount` (look for /sdcard)
279 * Can often be fixed by editing `~/.android/avd/YOUR_DEVICE/config.ini`.
280 * Look for `hw.sdCard=no` and set it to `yes`
Nate Fischer0dacc462021-04-06 16:10:24281 * The "Google APIs" Android L and M emulator images are configured to expect
282 the "AOSP" WebView package (`com.android.webview`). This does not resemble
283 production devices with GMS, which expect the ["Google WebView"
284 configuration](/android_webview/docs/webview-providers.md#webview-provider-options)
285 (`com.google.android.webview` on L and M). See [Removing preinstalled
286 WebView](/android_webview/docs/build-instructions.md#Removing-preinstalled-WebView)
287 if you need to install a local build or official build.
Andrew Grieveae094e392018-06-15 16:10:22288
John Budorick041a7df2020-04-03 23:20:27289
290#### Starting an Emulator from the Command Line
291
Andrew Grieveae094e392018-06-15 16:10:22292Refer to: https://developer.android.com/studio/run/emulator-commandline.html.
293
Nate Fischer7cbdeae2019-01-24 20:29:15294*** promo
295Ctrl-C will gracefully close an emulator.
296***
Andrew Grieveae094e392018-06-15 16:10:22297
Nate Fischer36804212020-03-31 02:12:49298*** promo
299**Tip:** zsh users can add https://github.com/zsh-users/zsh-completions to
300provide tab completion for the `emulator` command line tool.
301***
302
John Budorick041a7df2020-04-03 23:20:27303#### Basic Command Line Use
304
Nate Fischer7cbdeae2019-01-24 20:29:15305```shell
Andrew Grievec8f2703d2019-05-22 20:04:44306$ # List virtual devices that you've created:
307$ ~/Android/Sdk/emulator/emulator -list-avds
308$ # Start a named device:
Nate Fischer7cbdeae2019-01-24 20:29:15309$ ~/Android/Sdk/emulator/emulator @EMULATOR_ID
Andrew Grieveae094e392018-06-15 16:10:22310```
Nate Fischer7cbdeae2019-01-24 20:29:15311
John Budorick041a7df2020-04-03 23:20:27312#### Running a Headless Emulator
313
Nate Fischer7cbdeae2019-01-24 20:29:15314You can run an emulator without creating a window on your desktop (useful for
315`ssh`):
316```shell
Nate Fischer9cbee2432019-04-10 14:51:58317$ ~/Android/Sdk/emulator/emulator -no-window @EMULATOR_ID
Andrew Grievec8f2703d2019-05-22 20:04:44318$ # This also works for new enough emulator builds:
319$ ~/Android/Sdk/emulator/emulator-headless @EMULATOR_ID
320```
321
John Budorick041a7df2020-04-03 23:20:27322#### Running Multiple Emulators
323
Andrew Grievec8f2703d2019-05-22 20:04:44324Tests are automatically sharded amongst available devices. If you run multiple
325emulators, then running test suites becomes much faster. Refer to the
326"Multiple AVD instances" section of these [emulator release notes](
327https://androidstudio.googleblog.com/2018/11/emulator-28016-stable.html)
328for more about how this works.
329```shell
Andrew Grievec214adb2019-05-28 01:39:23330$ # Start 8 emulators. Press Ctrl-C to stop them all.
331$ ( for i in $(seq 8); do ~/Android/Sdk/emulator/emulator @EMULATOR_ID -read-only & done; wait )
Nate Fischere44e0f02019-05-29 20:09:53332$ # Start 12 emulators. More than 10 requires disabling audio on some OS's. Reducing cores increases parallelism.
Andrew Grievec214adb2019-05-28 01:39:23333$ ( for i in $(seq 12); do ~/Android/Sdk/emulator/emulator @EMULATOR_ID -read-only -no-audio -cores 2 & done; wait )
Nate Fischer7cbdeae2019-01-24 20:29:15334```
335
John Budorick041a7df2020-04-03 23:20:27336#### Writable system partition
337
Nate Fischer7cbdeae2019-01-24 20:29:15338Unlike physical devices, an emulator's `/system` partition cannot be modified by
339default (even on rooted devices). If you need to do so (such as to remove a
340system app), you can start your emulator like so:
341```shell
342$ ~/Android/Sdk/emulator/emulator -writable-system @EMULATOR_ID
343```
344
Andrew Grieveae094e392018-06-15 16:10:22345## Using an Emulator
346 * Emulators show up just like devices via `adb devices`
347 * Device serials will look like "emulator-5554", "emulator-5556", etc.
348
Nate Fischer4a494582020-03-12 18:08:51349## Emulator pros and cons
350
351### Pros
352 * **Compiles are faster.** Many physical devices are arm64, whereas emulators
353 are typically x86 (32-bit). 64-bit builds may require 2 copies of the native
354 library (32-bit and 64-bit), so compiling for an arm64 phone is ~twice as
355 much work as for an emulator (for targets which support WebView).
356 * **APKs install faster.** Since emulators run on your workstation, adb can
357 push the APK onto the emulator without being [bandwidth-constrained by
358 USB](https://youtu.be/Mzop8bXZI3E).
359 * Emulators can be nice for working remotely. Physical devices usually require
360 `scp` or ssh port forwarding to copy the APK from your workstation and
361 install on a local device. Emulators run on your workstation, so there's **no
362 ssh slow-down**.
363
364### Cons
365 * If you're investigating a hardware-specific bug report, you'll need a
366 physical device with the actual hardware to repro that issue.
367 * x86 emulators need a separate out directory, so building for both physical
368 devices and emulators takes up more disk space (not a problem if you build
369 exclusively for the emulator).
370 * `userdebug`/`eng` emulators don't come with the Play Store installed, so you
371 can't install third party applications. Sideloading is tricky, as not all
372 third-party apps support x86.