blob: 302cef3321528079d9ab716dd7ecd328d00ebfe4 [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
John Budorick041a7df2020-04-03 23:20:2719### Using Prebuilt CIPD packages
20
21Chromium has a set of prebuilt images stored as CIPD packages. These are used
22by various builders to run tests on the emulator. Their configurations are
Haiyang Panb06f846d2021-12-21 17:41:5523currently 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 Pan0fca0dc2024-03-25 17:50:3029| Configurations | Android Version | CPU Arch| AVD Target | Builder |
30|:-------------- |:--------------- |:------- |:---------- |:------- |
Haiyang Pan56581b32024-07-03 20:46:4031| `generic_android26.textpb` | 8.0 (O) | x86 | google_apis | N/A |
32| `generic_android27.textpb` | 8.1 (O_MR1) | x86 | google_apis | N/A |
33| `android_28_google_apis_x86.textpb` | 9 (P) | x86 | google_apis | [android-pie-x86-rel][android-pie-x86-rel] |
Haiyang Pan0fca0dc2024-03-25 17:50:3034| `android_29_google_apis_x86.textpb` | 10 (Q) | x86 | google_apis | N/A |
35| `android_30_google_apis_x86.textpb` | 11 (R) | x86 | google_apis | [android-11-x86-rel][android-11-x86-rel] |
36| `android_31_google_apis_x64.textpb` | 12 (S) | x86_64 | google_apis | [android-12-x64-rel][android-12-x64-rel] |
37| `android_32_google_apis_x64_foldable.textpb` | 12L (S_V2) | x86_64 | google_apis | [android-12l-x64-dbg-tests][android-12l-x64-dbg-tests] |
38| `android_33_google_apis_x64.textpb` | 13 (T) | x86_64 | google_apis | [android-13-x64-rel][android-13-x64-rel] |
39| `android_34_google_apis_x64.textpb` | 14 (U) | x86_64 | google_apis | [android-14-x64-rel][android-14-x64-rel] |
John Budorick041a7df2020-04-03 23:20:2740
41You can use these configuration files to run the same emulator images locally.
42
43[android-pie-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-pie-x86-rel
Haiyang Panb06f846d2021-12-21 17:41:5544[android-11-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-11-x86-rel
Haiyang Pan2a55a452022-03-03 08:18:3345[android-12-x64-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-12-x64-rel
Haiyang Pan0fca0dc2024-03-25 17:50:3046[android-12l-x64-dbg-tests]: https://ci.chromium.org/p/chromium/builders/ci/android-12l-x64-dbg-tests
47[android-13-x64-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-13-x64-rel
48[android-14-x64-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-14-x64-rel
John Budorick041a7df2020-04-03 23:20:2749
Haiyang Panf5faf232020-11-06 18:10:3450#### Prerequisite
51
52 * Make sure KVM (Kernel-based Virtual Machine) is enabled.
53 See this
54 [link](https://developer.android.com/studio/run/emulator-acceleration#vm-linux)
55 from android studio for more details and instructions.
56
57 * You need to have the permissions to use KVM.
58 Use the following command to see if you are in group `kvm`:
59
60 ```
61 $ grep kvm /etc/group
62 ```
63
64 If your username is not shown in the group, add yourself to the group:
65
66 ```
67 $ sudo adduser $USER kvm
68 $ newgrp kvm
69 ```
70
Erik Chen254ec1cc2024-01-18 02:11:4971 You need to log out and log back in so the new groups take effect.
Peter Wen96981e202021-08-06 21:13:4072
John Budorick041a7df2020-04-03 23:20:2773#### Running via the test runner
74
75The android test runner can run emulator instances on its own. In doing so, it
76starts the emulator instances, runs tests against them, and then shuts them
77down. This is how builders run the emulator.
78
79##### Options
80
81 * `--avd-config`
82
83 To have the test runner run an emulator instance, use `--avd-config`:
84
85 ```
86 $ out/Debug/bin/run_base_unittests \
Haiyang Pan56581b32024-07-03 20:46:4087 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb
John Budorick041a7df2020-04-03 23:20:2788 ```
89
90 * `--emulator-count`
91
92 The test runner will launch one instance by default. To have it run multiple
93 instances, use `--emulator-count`:
94
95 ```
96 $ out/Debug/bin/run_base_unittests \
Haiyang Pan56581b32024-07-03 20:46:4097 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
John Budorick041a7df2020-04-03 23:20:2798 --emulator-count 4
99 ```
100
sbingler8d91e8c2024-04-23 16:36:58101 * `--emulator-enable-network`
102
103 The test runner runs the emulator without network access by default. To have
104 it run with network access, use `--emulator-enable-network`:
105
106 ```
107 $ out/Debug/bin/run_base_unittests \
Haiyang Pan56581b32024-07-03 20:46:40108 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
sbingler8d91e8c2024-04-23 16:36:58109 --emulator-enable-network
110 ```
111
John Budorick041a7df2020-04-03 23:20:27112 * `--emulator-window`
113
114 The test runner runs the emulator in headless mode by default. To have it run
115 with a window, use `--emulator-window`:
116
117 ```
118 $ out/Debug/bin/run_base_unittests \
Haiyang Pan56581b32024-07-03 20:46:40119 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
John Budorick041a7df2020-04-03 23:20:27120 --emulator-window
121 ```
122
123#### Running standalone
124
125The test runner will set up and tear down the emulator on each invocation.
126To manage emulator lifetime independently, use `tools/android/avd/avd.py`.
127
John Budorick041a7df2020-04-03 23:20:27128##### Options
129
130 * `--avd-config`
131
132 This behaves the same as it does for the test runner.
133
134 ```
135 $ tools/android/avd/avd.py start \
Haiyang Pan56581b32024-07-03 20:46:40136 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb
John Budorick041a7df2020-04-03 23:20:27137 ```
138
139 > Note: `avd.py start` will start an emulator instance and then terminate.
Ian Struiksmae6fcbf02021-05-03 23:18:06140 > To shut down the emulator, use `adb emu kill`.
John Budorick041a7df2020-04-03 23:20:27141
sbingler8d91e8c2024-04-23 16:36:58142 * `--enable-network`
143
144 Like the test runner, `avd.py` runs the emulator without network access by
145 default. To enable network access, use `--enable-network`:
146
147 ```
148 $ tools/android/avd/avd.py start \
Haiyang Pan56581b32024-07-03 20:46:40149 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
sbingler8d91e8c2024-04-23 16:36:58150 --enable-network
151 ```
152
John Budorick041a7df2020-04-03 23:20:27153 * `--emulator-window`
154
155 Like the test runner, `avd.py` runs the emulator in headless mode by default.
156 To have it run with a window, use `--emulator-window`:
157
158 ```
159 $ tools/android/avd/avd.py start \
Haiyang Pan56581b32024-07-03 20:46:40160 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
John Budorick041a7df2020-04-03 23:20:27161 --emulator-window
162 ```
163
Haiyang Panb06f846d2021-12-21 17:41:55164 * `--gpu-mode GPU_MODE`
165
166 Override the mode of hardware OpenGL ES emulation indicated by the AVD.
167 See "emulator -help-gpu" for a full list of modes.
168
169 * `--no-read-only`
170
171 `avd.py` runs the emulator in read-only mode by default. To run a modifiable
172 emulator, use `--no-read-only`:
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 \
Haiyang Panb06f846d2021-12-21 17:41:55177 --no-read-only
178 ```
179
Peter Wenb105afb2021-12-07 20:24:36180 * `--wipe-data`
181
Haiyang Pane5e2bc02023-10-20 19:31:53182 Reset the /data partition to the factory defaults. This removes all user
183 settings from the AVD.
Peter Wenb105afb2021-12-07 20:24:36184
185 ```
186 $ tools/android/avd/avd.py start \
Haiyang Pan56581b32024-07-03 20:46:40187 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
Peter Wenb105afb2021-12-07 20:24:36188 --wipe-data
189 ```
190
Haiyang Panb06f846d2021-12-21 17:41:55191 * `--writable-system`
John Budorick041a7df2020-04-03 23:20:27192
Victor Vianna725b1072022-03-04 16:02:59193 Makes system & vendor image writable. It's necessary to run
194 ```
195 adb root
196 adb remount
197 ```
198 after the emulator starts.
John Budorick041a7df2020-04-03 23:20:27199
Haiyang Panf5faf232020-11-06 18:10:34200 * `--debug-tags`
201
202 `avd.py` disables the emulator log by default. When this option is used,
203 emulator log will be enabled. It is useful when the emulator cannot be
204 launched correctly. See `emulator -help-debug-tags` for a full list of tags.
Haiyang Pan7c2d20f2024-02-20 21:59:28205 Use `--debug-tags=all` if you want to output all logs (warning: it is quite
Peter Wenb105afb2021-12-07 20:24:36206 verbose).
Haiyang Panf5faf232020-11-06 18:10:34207
208 ```
209 $ tools/android/avd/avd.py start \
Haiyang Pan56581b32024-07-03 20:46:40210 --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \
Haiyang Panf5faf232020-11-06 18:10:34211 --debug-tags init,snapshot
212 ```
213
John Budorick041a7df2020-04-03 23:20:27214### Using Your Own Emulator Image
215
Andrew Grieveae094e392018-06-15 16:10:22216By far the easiest way to set up emulator images is to use Android Studio.
217If you don't have an [Android Studio project](android_studio.md) already, you
218can create a blank one to be able to reach the Virtual Device Manager screen.
219
220Refer to: https://developer.android.com/studio/run/managing-avds.html
221
222Where files live:
223 * System partition images are stored within the sdk directory.
224 * Emulator configs and data partition images are stored within
225 `~/.android/avd/`.
226
John Budorick041a7df2020-04-03 23:20:27227#### Creating an Image
228
229##### Choosing a Skin
230
Nate Fischer7cbdeae2019-01-24 20:29:15231Choose a skin with a small screen for better performance (unless you care about
232testing large screens).
Andrew Grieveae094e392018-06-15 16:10:22233
John Budorick041a7df2020-04-03 23:20:27234##### Choosing an Image
235
Nate Fischer7cbdeae2019-01-24 20:29:15236Android Studio's image labels roughly translate to the following:
237
Nate Fischeracbbaab2019-04-23 16:46:20238| AVD "Target" | Virtual Device Configuration tab | GMS? | Build Properties |
239| --- | --- | --- | --- |
240| Google Play | "Recommended" (the default tab) | This has GMS | `user`/`release-keys` |
241| Google APIs | "x86 Images" | This has GMS | `userdebug`/`dev-keys` |
242| No label | "x86 Images" | AOSP image, does not have GMS | `eng`/`test-keys` |
Nate Fischer7cbdeae2019-01-24 20:29:15243
244*** promo
Nate Fischeracbbaab2019-04-23 16:46:20245**Tip:** if you're not sure which to use, choose **Google APIs** under the **x86
246Images** tab in the Virtual Device Configuration wizard.
Nate Fischer7cbdeae2019-01-24 20:29:15247***
248
John Budorick041a7df2020-04-03 23:20:27249##### Configuration
250
Nate Fischer7cbdeae2019-01-24 20:29:15251"Show Advanced Settings" > scroll down:
252* Set internal storage to 4000MB (component builds are really big).
253* Set SD card to 1000MB (our tests push a lot of files to /sdcard).
254
John Budorick041a7df2020-04-03 23:20:27255##### Known Issues
256
Andrew Grieveae094e392018-06-15 16:10:22257 * Our test & installer scripts do not work with pre-MR1 Jelly Bean.
258 * Component builds do not work on pre-KitKat (due to the OS having a max
259 number of shared libraries).
260 * Jelly Bean and KitKat images sometimes forget to mount /sdcard :(.
261 * This causes tests to fail.
262 * To ensure it's there: `adb -s emulator-5554 shell mount` (look for /sdcard)
263 * Can often be fixed by editing `~/.android/avd/YOUR_DEVICE/config.ini`.
264 * Look for `hw.sdCard=no` and set it to `yes`
Nate Fischer0dacc462021-04-06 16:10:24265 * The "Google APIs" Android L and M emulator images are configured to expect
266 the "AOSP" WebView package (`com.android.webview`). This does not resemble
267 production devices with GMS, which expect the ["Google WebView"
268 configuration](/android_webview/docs/webview-providers.md#webview-provider-options)
269 (`com.google.android.webview` on L and M). See [Removing preinstalled
270 WebView](/android_webview/docs/build-instructions.md#Removing-preinstalled-WebView)
271 if you need to install a local build or official build.
Andrew Grieveae094e392018-06-15 16:10:22272
John Budorick041a7df2020-04-03 23:20:27273
274#### Starting an Emulator from the Command Line
275
Andrew Grieveae094e392018-06-15 16:10:22276Refer to: https://developer.android.com/studio/run/emulator-commandline.html.
277
Nate Fischer7cbdeae2019-01-24 20:29:15278*** promo
279Ctrl-C will gracefully close an emulator.
280***
Andrew Grieveae094e392018-06-15 16:10:22281
Nate Fischer36804212020-03-31 02:12:49282*** promo
283**Tip:** zsh users can add https://github.com/zsh-users/zsh-completions to
284provide tab completion for the `emulator` command line tool.
285***
286
John Budorick041a7df2020-04-03 23:20:27287#### Basic Command Line Use
288
Nate Fischer7cbdeae2019-01-24 20:29:15289```shell
Andrew Grievec8f2703d2019-05-22 20:04:44290$ # List virtual devices that you've created:
291$ ~/Android/Sdk/emulator/emulator -list-avds
292$ # Start a named device:
Nate Fischer7cbdeae2019-01-24 20:29:15293$ ~/Android/Sdk/emulator/emulator @EMULATOR_ID
Andrew Grieveae094e392018-06-15 16:10:22294```
Nate Fischer7cbdeae2019-01-24 20:29:15295
John Budorick041a7df2020-04-03 23:20:27296#### Running a Headless Emulator
297
Nate Fischer7cbdeae2019-01-24 20:29:15298You can run an emulator without creating a window on your desktop (useful for
299`ssh`):
300```shell
Nate Fischer9cbee2432019-04-10 14:51:58301$ ~/Android/Sdk/emulator/emulator -no-window @EMULATOR_ID
Andrew Grievec8f2703d2019-05-22 20:04:44302$ # This also works for new enough emulator builds:
303$ ~/Android/Sdk/emulator/emulator-headless @EMULATOR_ID
304```
305
John Budorick041a7df2020-04-03 23:20:27306#### Running Multiple Emulators
307
Andrew Grievec8f2703d2019-05-22 20:04:44308Tests are automatically sharded amongst available devices. If you run multiple
309emulators, then running test suites becomes much faster. Refer to the
310"Multiple AVD instances" section of these [emulator release notes](
311https://androidstudio.googleblog.com/2018/11/emulator-28016-stable.html)
312for more about how this works.
313```shell
Andrew Grievec214adb2019-05-28 01:39:23314$ # Start 8 emulators. Press Ctrl-C to stop them all.
315$ ( for i in $(seq 8); do ~/Android/Sdk/emulator/emulator @EMULATOR_ID -read-only & done; wait )
Nate Fischere44e0f02019-05-29 20:09:53316$ # Start 12 emulators. More than 10 requires disabling audio on some OS's. Reducing cores increases parallelism.
Andrew Grievec214adb2019-05-28 01:39:23317$ ( 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:15318```
319
John Budorick041a7df2020-04-03 23:20:27320#### Writable system partition
321
Nate Fischer7cbdeae2019-01-24 20:29:15322Unlike physical devices, an emulator's `/system` partition cannot be modified by
323default (even on rooted devices). If you need to do so (such as to remove a
324system app), you can start your emulator like so:
325```shell
326$ ~/Android/Sdk/emulator/emulator -writable-system @EMULATOR_ID
327```
328
Andrew Grieveae094e392018-06-15 16:10:22329## Using an Emulator
330 * Emulators show up just like devices via `adb devices`
331 * Device serials will look like "emulator-5554", "emulator-5556", etc.
332
Nate Fischer4a494582020-03-12 18:08:51333## Emulator pros and cons
334
335### Pros
336 * **Compiles are faster.** Many physical devices are arm64, whereas emulators
337 are typically x86 (32-bit). 64-bit builds may require 2 copies of the native
338 library (32-bit and 64-bit), so compiling for an arm64 phone is ~twice as
339 much work as for an emulator (for targets which support WebView).
340 * **APKs install faster.** Since emulators run on your workstation, adb can
341 push the APK onto the emulator without being [bandwidth-constrained by
342 USB](https://youtu.be/Mzop8bXZI3E).
343 * Emulators can be nice for working remotely. Physical devices usually require
344 `scp` or ssh port forwarding to copy the APK from your workstation and
345 install on a local device. Emulators run on your workstation, so there's **no
346 ssh slow-down**.
347
348### Cons
349 * If you're investigating a hardware-specific bug report, you'll need a
350 physical device with the actual hardware to repro that issue.
351 * x86 emulators need a separate out directory, so building for both physical
352 devices and emulators takes up more disk space (not a problem if you build
353 exclusively for the emulator).
354 * `userdebug`/`eng` emulators don't come with the Play Store installed, so you
355 can't install third party applications. Sideloading is tricky, as not all
356 third-party apps support x86.