blob: 648e529c48daced6e54eab54870c8cd3e2ab4bdf [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
15### Using Prebuilt CIPD packages
16
17Chromium has a set of prebuilt images stored as CIPD packages. These are used
18by various builders to run tests on the emulator. Their configurations are
Haiyang Panb06f846d2021-12-21 17:41:5519currently stored in [`//tools/android/avd/proto`](../tools/android/avd/proto/).
Andrew Grieve1b9be142022-02-10 04:07:0020You can run this command to list them:
21```sh
22tools/android/avd/avd.py list
23```
John Budorick041a7df2020-04-03 23:20:2724
Haiyang Panb06f846d2021-12-21 17:41:5525| Configurations | Android Version | AVD Target | Builder |
26|:-------------- |:--------------- |:---------- |:------- |
27| `generic_android23.textpb` | M | google_apis | [android-marshmallow-x86-rel][android-marshmallow-x86-rel] |
28| `generic_android27.textpb` | O | google_apis | N/A |
29| `generic_playstore_android27.textpb` | O | google_apis_playstore | N/A |
30| `generic_android28.textpb` | P | google_apis | [android-pie-x86-rel][android-pie-x86-rel] |
31| `generic_playstore_android28.textpb` | P | google_apis_playstore | [android-pie-x86-rel][android-pie-x86-rel] |
32| `generic_android29.textpb` | 10 (Q) | google_apis | N/A |
33| `generic_android30.textpb` | 11 (R) | google_apis | [android-11-x86-rel][android-11-x86-rel] |
34| `generic_playstore_android30.textpb` | 11 (R) | google_apis_playstore | [android-11-x86-rel][android-11-x86-rel] |
35| `generic_android31.textpb` | 12 (S) | google_apis | [android-12-x64-fyi-rel][android-12-x64-fyi-rel] |
36| `generic_playstore_android31.textpb` | 12 (S) | google_apis_playstore | [android-12-x64-fyi-rel][android-12-x64-fyi-rel] |
John Budorick041a7df2020-04-03 23:20:2737
38You can use these configuration files to run the same emulator images locally.
39
Haiyang Panf5faf232020-11-06 18:10:3440[android-marshmallow-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-marshmallow-x86-rel
John Budorick041a7df2020-04-03 23:20:2741[android-pie-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-pie-x86-rel
Haiyang Panb06f846d2021-12-21 17:41:5542[android-11-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-11-x86-rel
43[android-12-x64-fyi-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-12-x64-fyi-rel
John Budorick041a7df2020-04-03 23:20:2744
Haiyang Panf5faf232020-11-06 18:10:3445#### Prerequisite
46
47 * Make sure KVM (Kernel-based Virtual Machine) is enabled.
48 See this
49 [link](https://developer.android.com/studio/run/emulator-acceleration#vm-linux)
50 from android studio for more details and instructions.
51
52 * You need to have the permissions to use KVM.
53 Use the following command to see if you are in group `kvm`:
54
55 ```
56 $ grep kvm /etc/group
57 ```
58
59 If your username is not shown in the group, add yourself to the group:
60
61 ```
62 $ sudo adduser $USER kvm
63 $ newgrp kvm
64 ```
65
Peter Wen96981e202021-08-06 21:13:4066 You need to log out and log back in so the new groups take effect. Or you
67 can use the following on a per-shell basis without logging out:
68
69 ```
70 $ su - $USER
71 ```
72
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 \
87 --avd-config tools/android/avd/proto/generic_android28.textpb
88 ```
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 \
97 --avd-config tools/android/avd/proto/generic_android28.textpb \
98 --emulator-count 4
99 ```
100
101 * `--emulator-window`
102
103 The test runner runs the emulator in headless mode by default. To have it run
104 with a window, use `--emulator-window`:
105
106 ```
107 $ out/Debug/bin/run_base_unittests \
108 --avd-config tools/android/avd/proto/generic_android28.textpb \
109 --emulator-window
110 ```
111
112#### Running standalone
113
114The test runner will set up and tear down the emulator on each invocation.
115To manage emulator lifetime independently, use `tools/android/avd/avd.py`.
116
John Budorick041a7df2020-04-03 23:20:27117##### Options
118
119 * `--avd-config`
120
121 This behaves the same as it does for the test runner.
122
123 ```
124 $ tools/android/avd/avd.py start \
125 --avd-config tools/android/avd/proto/generic_android28.textpb
126 ```
127
128 > Note: `avd.py start` will start an emulator instance and then terminate.
Ian Struiksmae6fcbf02021-05-03 23:18:06129 > To shut down the emulator, use `adb emu kill`.
John Budorick041a7df2020-04-03 23:20:27130
131 * `--emulator-window`
132
133 Like the test runner, `avd.py` runs the emulator in headless mode by default.
134 To have it run with a window, use `--emulator-window`:
135
136 ```
137 $ tools/android/avd/avd.py start \
138 --avd-config tools/android/avd/proto/generic_android28.textpb \
139 --emulator-window
140 ```
141
Haiyang Panb06f846d2021-12-21 17:41:55142 * `--gpu-mode GPU_MODE`
143
144 Override the mode of hardware OpenGL ES emulation indicated by the AVD.
145 See "emulator -help-gpu" for a full list of modes.
146
147 * `--no-read-only`
148
149 `avd.py` runs the emulator in read-only mode by default. To run a modifiable
150 emulator, use `--no-read-only`:
151
152 ```
153 $ tools/android/avd/avd.py start \
154 --avd-config tools/android/avd/proto/generic_android28.textpb \
155 --no-read-only
156 ```
157
Peter Wenb105afb2021-12-07 20:24:36158 * `--wipe-data`
159
160 Since the prebuilt playstore images use adbkey from the GCE bots that created
161 them, they may appear to be "unauthorized" when used locally. Pass this flag
162 to reset the user data image locally to fix it after installing a prebuilt
163 image:
164
165 ```
166 $ tools/android/avd/avd.py start \
167 --avd-config tools/android/avd/proto/generic_playstore_android28.textpb \
168 --wipe-data
169 ```
170
Haiyang Panb06f846d2021-12-21 17:41:55171 * `--writable-system`
John Budorick041a7df2020-04-03 23:20:27172
Haiyang Panb06f846d2021-12-21 17:41:55173 Makes system & vendor image writable after adb remount.
John Budorick041a7df2020-04-03 23:20:27174
Haiyang Panf5faf232020-11-06 18:10:34175 * `--debug-tags`
176
177 `avd.py` disables the emulator log by default. When this option is used,
178 emulator log will be enabled. It is useful when the emulator cannot be
179 launched correctly. See `emulator -help-debug-tags` for a full list of tags.
Peter Wenb105afb2021-12-07 20:24:36180 Use `--debug-tags="*"` if you want to output all logs (warning: it is quite
181 verbose).
Haiyang Panf5faf232020-11-06 18:10:34182
183 ```
184 $ tools/android/avd/avd.py start \
185 --avd-config tools/android/avd/proto/generic_android28.textpb \
186 --debug-tags init,snapshot
187 ```
188
John Budorick041a7df2020-04-03 23:20:27189### Using Your Own Emulator Image
190
Andrew Grieveae094e392018-06-15 16:10:22191By far the easiest way to set up emulator images is to use Android Studio.
192If you don't have an [Android Studio project](android_studio.md) already, you
193can create a blank one to be able to reach the Virtual Device Manager screen.
194
195Refer to: https://developer.android.com/studio/run/managing-avds.html
196
197Where files live:
198 * System partition images are stored within the sdk directory.
199 * Emulator configs and data partition images are stored within
200 `~/.android/avd/`.
201
John Budorick041a7df2020-04-03 23:20:27202#### Creating an Image
203
204##### Choosing a Skin
205
Nate Fischer7cbdeae2019-01-24 20:29:15206Choose a skin with a small screen for better performance (unless you care about
207testing large screens).
Andrew Grieveae094e392018-06-15 16:10:22208
John Budorick041a7df2020-04-03 23:20:27209##### Choosing an Image
210
Nate Fischer7cbdeae2019-01-24 20:29:15211Android Studio's image labels roughly translate to the following:
212
Nate Fischeracbbaab2019-04-23 16:46:20213| AVD "Target" | Virtual Device Configuration tab | GMS? | Build Properties |
214| --- | --- | --- | --- |
215| Google Play | "Recommended" (the default tab) | This has GMS | `user`/`release-keys` |
216| Google APIs | "x86 Images" | This has GMS | `userdebug`/`dev-keys` |
217| No label | "x86 Images" | AOSP image, does not have GMS | `eng`/`test-keys` |
Nate Fischer7cbdeae2019-01-24 20:29:15218
219*** promo
Nate Fischeracbbaab2019-04-23 16:46:20220**Tip:** if you're not sure which to use, choose **Google APIs** under the **x86
221Images** tab in the Virtual Device Configuration wizard.
Nate Fischer7cbdeae2019-01-24 20:29:15222***
223
John Budorick041a7df2020-04-03 23:20:27224##### Configuration
225
Nate Fischer7cbdeae2019-01-24 20:29:15226"Show Advanced Settings" > scroll down:
227* Set internal storage to 4000MB (component builds are really big).
228* Set SD card to 1000MB (our tests push a lot of files to /sdcard).
229
John Budorick041a7df2020-04-03 23:20:27230##### Known Issues
231
Andrew Grieveae094e392018-06-15 16:10:22232 * Our test & installer scripts do not work with pre-MR1 Jelly Bean.
233 * Component builds do not work on pre-KitKat (due to the OS having a max
234 number of shared libraries).
235 * Jelly Bean and KitKat images sometimes forget to mount /sdcard :(.
236 * This causes tests to fail.
237 * To ensure it's there: `adb -s emulator-5554 shell mount` (look for /sdcard)
238 * Can often be fixed by editing `~/.android/avd/YOUR_DEVICE/config.ini`.
239 * Look for `hw.sdCard=no` and set it to `yes`
Nate Fischer0dacc462021-04-06 16:10:24240 * The "Google APIs" Android L and M emulator images are configured to expect
241 the "AOSP" WebView package (`com.android.webview`). This does not resemble
242 production devices with GMS, which expect the ["Google WebView"
243 configuration](/android_webview/docs/webview-providers.md#webview-provider-options)
244 (`com.google.android.webview` on L and M). See [Removing preinstalled
245 WebView](/android_webview/docs/build-instructions.md#Removing-preinstalled-WebView)
246 if you need to install a local build or official build.
Andrew Grieveae094e392018-06-15 16:10:22247
John Budorick041a7df2020-04-03 23:20:27248
249#### Starting an Emulator from the Command Line
250
Andrew Grieveae094e392018-06-15 16:10:22251Refer to: https://developer.android.com/studio/run/emulator-commandline.html.
252
Nate Fischer7cbdeae2019-01-24 20:29:15253*** promo
254Ctrl-C will gracefully close an emulator.
255***
Andrew Grieveae094e392018-06-15 16:10:22256
Nate Fischer36804212020-03-31 02:12:49257*** promo
258**Tip:** zsh users can add https://github.com/zsh-users/zsh-completions to
259provide tab completion for the `emulator` command line tool.
260***
261
John Budorick041a7df2020-04-03 23:20:27262#### Basic Command Line Use
263
Nate Fischer7cbdeae2019-01-24 20:29:15264```shell
Andrew Grievec8f2703d2019-05-22 20:04:44265$ # List virtual devices that you've created:
266$ ~/Android/Sdk/emulator/emulator -list-avds
267$ # Start a named device:
Nate Fischer7cbdeae2019-01-24 20:29:15268$ ~/Android/Sdk/emulator/emulator @EMULATOR_ID
Andrew Grieveae094e392018-06-15 16:10:22269```
Nate Fischer7cbdeae2019-01-24 20:29:15270
John Budorick041a7df2020-04-03 23:20:27271#### Running a Headless Emulator
272
Nate Fischer7cbdeae2019-01-24 20:29:15273You can run an emulator without creating a window on your desktop (useful for
274`ssh`):
275```shell
Nate Fischer9cbee2432019-04-10 14:51:58276$ ~/Android/Sdk/emulator/emulator -no-window @EMULATOR_ID
Andrew Grievec8f2703d2019-05-22 20:04:44277$ # This also works for new enough emulator builds:
278$ ~/Android/Sdk/emulator/emulator-headless @EMULATOR_ID
279```
280
John Budorick041a7df2020-04-03 23:20:27281#### Running Multiple Emulators
282
Andrew Grievec8f2703d2019-05-22 20:04:44283Tests are automatically sharded amongst available devices. If you run multiple
284emulators, then running test suites becomes much faster. Refer to the
285"Multiple AVD instances" section of these [emulator release notes](
286https://androidstudio.googleblog.com/2018/11/emulator-28016-stable.html)
287for more about how this works.
288```shell
Andrew Grievec214adb2019-05-28 01:39:23289$ # Start 8 emulators. Press Ctrl-C to stop them all.
290$ ( for i in $(seq 8); do ~/Android/Sdk/emulator/emulator @EMULATOR_ID -read-only & done; wait )
Nate Fischere44e0f02019-05-29 20:09:53291$ # Start 12 emulators. More than 10 requires disabling audio on some OS's. Reducing cores increases parallelism.
Andrew Grievec214adb2019-05-28 01:39:23292$ ( 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:15293```
294
John Budorick041a7df2020-04-03 23:20:27295#### Writable system partition
296
Nate Fischer7cbdeae2019-01-24 20:29:15297Unlike physical devices, an emulator's `/system` partition cannot be modified by
298default (even on rooted devices). If you need to do so (such as to remove a
299system app), you can start your emulator like so:
300```shell
301$ ~/Android/Sdk/emulator/emulator -writable-system @EMULATOR_ID
302```
303
Andrew Grieveae094e392018-06-15 16:10:22304## Using an Emulator
305 * Emulators show up just like devices via `adb devices`
306 * Device serials will look like "emulator-5554", "emulator-5556", etc.
307
Nate Fischer4a494582020-03-12 18:08:51308## Emulator pros and cons
309
310### Pros
311 * **Compiles are faster.** Many physical devices are arm64, whereas emulators
312 are typically x86 (32-bit). 64-bit builds may require 2 copies of the native
313 library (32-bit and 64-bit), so compiling for an arm64 phone is ~twice as
314 much work as for an emulator (for targets which support WebView).
315 * **APKs install faster.** Since emulators run on your workstation, adb can
316 push the APK onto the emulator without being [bandwidth-constrained by
317 USB](https://youtu.be/Mzop8bXZI3E).
318 * Emulators can be nice for working remotely. Physical devices usually require
319 `scp` or ssh port forwarding to copy the APK from your workstation and
320 install on a local device. Emulators run on your workstation, so there's **no
321 ssh slow-down**.
322
323### Cons
324 * If you're investigating a hardware-specific bug report, you'll need a
325 physical device with the actual hardware to repro that issue.
326 * x86 emulators need a separate out directory, so building for both physical
327 devices and emulators takes up more disk space (not a problem if you build
328 exclusively for the emulator).
329 * `userdebug`/`eng` emulators don't come with the Play Store installed, so you
330 can't install third party applications. Sideloading is tricky, as not all
331 third-party apps support x86.