blob: d8a1ee56d70b08da80e8f95f1364bef0b4bcdf15 [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
19currently stored in `//tools/android/avd/proto`.
20
21| File | Builder |
22|:----:|:-------:|
Haiyang Panf5faf232020-11-06 18:10:3423| `tools/android/avd/proto/generic_android23.textpb` | [android-marshmallow-x86-rel][android-marshmallow-x86-rel] |
John Budorick041a7df2020-04-03 23:20:2724| `tools/android/avd/proto/generic_android28.textpb` | [android-pie-x86-rel][android-pie-x86-rel] |
Haiyang Panf5faf232020-11-06 18:10:3425| `tools/android/avd/proto/generic_playstore_android28.textpb` | [android-pie-x86-rel][android-pie-x86-rel] |
John Budorick041a7df2020-04-03 23:20:2726
27You can use these configuration files to run the same emulator images locally.
28
Haiyang Panf5faf232020-11-06 18:10:3429[android-marshmallow-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-marshmallow-x86-rel
John Budorick041a7df2020-04-03 23:20:2730[android-pie-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-pie-x86-rel
31
Haiyang Panf5faf232020-11-06 18:10:3432#### Prerequisite
33
34 * Make sure KVM (Kernel-based Virtual Machine) is enabled.
35 See this
36 [link](https://developer.android.com/studio/run/emulator-acceleration#vm-linux)
37 from android studio for more details and instructions.
38
39 * You need to have the permissions to use KVM.
40 Use the following command to see if you are in group `kvm`:
41
42 ```
43 $ grep kvm /etc/group
44 ```
45
46 If your username is not shown in the group, add yourself to the group:
47
48 ```
49 $ sudo adduser $USER kvm
50 $ newgrp kvm
51 ```
52
Peter Wen96981e202021-08-06 21:13:4053 You need to log out and log back in so the new groups take effect. Or you
54 can use the following on a per-shell basis without logging out:
55
56 ```
57 $ su - $USER
58 ```
59
John Budorick041a7df2020-04-03 23:20:2760#### Running via the test runner
61
62The android test runner can run emulator instances on its own. In doing so, it
63starts the emulator instances, runs tests against them, and then shuts them
64down. This is how builders run the emulator.
65
66##### Options
67
68 * `--avd-config`
69
70 To have the test runner run an emulator instance, use `--avd-config`:
71
72 ```
73 $ out/Debug/bin/run_base_unittests \
74 --avd-config tools/android/avd/proto/generic_android28.textpb
75 ```
76
77 * `--emulator-count`
78
79 The test runner will launch one instance by default. To have it run multiple
80 instances, use `--emulator-count`:
81
82 ```
83 $ out/Debug/bin/run_base_unittests \
84 --avd-config tools/android/avd/proto/generic_android28.textpb \
85 --emulator-count 4
86 ```
87
88 * `--emulator-window`
89
90 The test runner runs the emulator in headless mode by default. To have it run
91 with a window, use `--emulator-window`:
92
93 ```
94 $ out/Debug/bin/run_base_unittests \
95 --avd-config tools/android/avd/proto/generic_android28.textpb \
96 --emulator-window
97 ```
98
99#### Running standalone
100
101The test runner will set up and tear down the emulator on each invocation.
102To manage emulator lifetime independently, use `tools/android/avd/avd.py`.
103
Haiyang Panf5faf232020-11-06 18:10:34104> Note: Before calling `avd.py start`, use `avd.py install` to install the
105> emulator configuration you intend to use. Otherwise the emulator won't start
106> correctly.
107>
108> ```
109> $ tools/android/avd/avd.py install \
110> --avd-config tools/android/avd/proto/generic_android28.textpb
111> ```
John Budorick041a7df2020-04-03 23:20:27112
113##### Options
114
115 * `--avd-config`
116
117 This behaves the same as it does for the test runner.
118
119 ```
120 $ tools/android/avd/avd.py start \
121 --avd-config tools/android/avd/proto/generic_android28.textpb
122 ```
123
124 > Note: `avd.py start` will start an emulator instance and then terminate.
Ian Struiksmae6fcbf02021-05-03 23:18:06125 > To shut down the emulator, use `adb emu kill`.
John Budorick041a7df2020-04-03 23:20:27126
127 * `--emulator-window`
128
129 Like the test runner, `avd.py` runs the emulator in headless mode by default.
130 To have it run with a window, use `--emulator-window`:
131
132 ```
133 $ tools/android/avd/avd.py start \
134 --avd-config tools/android/avd/proto/generic_android28.textpb \
135 --emulator-window
136 ```
137
138 * `--no-read-only`
139
140 `avd.py` runs the emulator in read-only mode by default. To run a modifiable
141 emulator, use `--no-read-only`:
142
143 ```
144 $ tools/android/avd/avd.py start \
145 --avd-config tools/android/avd/proto/generic_android28.textpb \
146 --no-read-only
147 ```
148
Haiyang Panf5faf232020-11-06 18:10:34149 * `--debug-tags`
150
151 `avd.py` disables the emulator log by default. When this option is used,
152 emulator log will be enabled. It is useful when the emulator cannot be
153 launched correctly. See `emulator -help-debug-tags` for a full list of tags.
154
155 ```
156 $ tools/android/avd/avd.py start \
157 --avd-config tools/android/avd/proto/generic_android28.textpb \
158 --debug-tags init,snapshot
159 ```
160
John Budorick041a7df2020-04-03 23:20:27161### Using Your Own Emulator Image
162
Andrew Grieveae094e392018-06-15 16:10:22163By far the easiest way to set up emulator images is to use Android Studio.
164If you don't have an [Android Studio project](android_studio.md) already, you
165can create a blank one to be able to reach the Virtual Device Manager screen.
166
167Refer to: https://developer.android.com/studio/run/managing-avds.html
168
169Where files live:
170 * System partition images are stored within the sdk directory.
171 * Emulator configs and data partition images are stored within
172 `~/.android/avd/`.
173
John Budorick041a7df2020-04-03 23:20:27174#### Creating an Image
175
176##### Choosing a Skin
177
Nate Fischer7cbdeae2019-01-24 20:29:15178Choose a skin with a small screen for better performance (unless you care about
179testing large screens).
Andrew Grieveae094e392018-06-15 16:10:22180
John Budorick041a7df2020-04-03 23:20:27181##### Choosing an Image
182
Nate Fischer7cbdeae2019-01-24 20:29:15183Android Studio's image labels roughly translate to the following:
184
Nate Fischeracbbaab2019-04-23 16:46:20185| AVD "Target" | Virtual Device Configuration tab | GMS? | Build Properties |
186| --- | --- | --- | --- |
187| Google Play | "Recommended" (the default tab) | This has GMS | `user`/`release-keys` |
188| Google APIs | "x86 Images" | This has GMS | `userdebug`/`dev-keys` |
189| No label | "x86 Images" | AOSP image, does not have GMS | `eng`/`test-keys` |
Nate Fischer7cbdeae2019-01-24 20:29:15190
191*** promo
Nate Fischeracbbaab2019-04-23 16:46:20192**Tip:** if you're not sure which to use, choose **Google APIs** under the **x86
193Images** tab in the Virtual Device Configuration wizard.
Nate Fischer7cbdeae2019-01-24 20:29:15194***
195
John Budorick041a7df2020-04-03 23:20:27196##### Configuration
197
Nate Fischer7cbdeae2019-01-24 20:29:15198"Show Advanced Settings" > scroll down:
199* Set internal storage to 4000MB (component builds are really big).
200* Set SD card to 1000MB (our tests push a lot of files to /sdcard).
201
John Budorick041a7df2020-04-03 23:20:27202##### Known Issues
203
Andrew Grieveae094e392018-06-15 16:10:22204 * Our test & installer scripts do not work with pre-MR1 Jelly Bean.
205 * Component builds do not work on pre-KitKat (due to the OS having a max
206 number of shared libraries).
207 * Jelly Bean and KitKat images sometimes forget to mount /sdcard :(.
208 * This causes tests to fail.
209 * To ensure it's there: `adb -s emulator-5554 shell mount` (look for /sdcard)
210 * Can often be fixed by editing `~/.android/avd/YOUR_DEVICE/config.ini`.
211 * Look for `hw.sdCard=no` and set it to `yes`
Nate Fischer0dacc462021-04-06 16:10:24212 * The "Google APIs" Android L and M emulator images are configured to expect
213 the "AOSP" WebView package (`com.android.webview`). This does not resemble
214 production devices with GMS, which expect the ["Google WebView"
215 configuration](/android_webview/docs/webview-providers.md#webview-provider-options)
216 (`com.google.android.webview` on L and M). See [Removing preinstalled
217 WebView](/android_webview/docs/build-instructions.md#Removing-preinstalled-WebView)
218 if you need to install a local build or official build.
Andrew Grieveae094e392018-06-15 16:10:22219
John Budorick041a7df2020-04-03 23:20:27220
221#### Starting an Emulator from the Command Line
222
Andrew Grieveae094e392018-06-15 16:10:22223Refer to: https://developer.android.com/studio/run/emulator-commandline.html.
224
Nate Fischer7cbdeae2019-01-24 20:29:15225*** promo
226Ctrl-C will gracefully close an emulator.
227***
Andrew Grieveae094e392018-06-15 16:10:22228
Nate Fischer36804212020-03-31 02:12:49229*** promo
230**Tip:** zsh users can add https://github.com/zsh-users/zsh-completions to
231provide tab completion for the `emulator` command line tool.
232***
233
John Budorick041a7df2020-04-03 23:20:27234#### Basic Command Line Use
235
Nate Fischer7cbdeae2019-01-24 20:29:15236```shell
Andrew Grievec8f2703d2019-05-22 20:04:44237$ # List virtual devices that you've created:
238$ ~/Android/Sdk/emulator/emulator -list-avds
239$ # Start a named device:
Nate Fischer7cbdeae2019-01-24 20:29:15240$ ~/Android/Sdk/emulator/emulator @EMULATOR_ID
Andrew Grieveae094e392018-06-15 16:10:22241```
Nate Fischer7cbdeae2019-01-24 20:29:15242
John Budorick041a7df2020-04-03 23:20:27243#### Running a Headless Emulator
244
Nate Fischer7cbdeae2019-01-24 20:29:15245You can run an emulator without creating a window on your desktop (useful for
246`ssh`):
247```shell
Nate Fischer9cbee2432019-04-10 14:51:58248$ ~/Android/Sdk/emulator/emulator -no-window @EMULATOR_ID
Andrew Grievec8f2703d2019-05-22 20:04:44249$ # This also works for new enough emulator builds:
250$ ~/Android/Sdk/emulator/emulator-headless @EMULATOR_ID
251```
252
John Budorick041a7df2020-04-03 23:20:27253#### Running Multiple Emulators
254
Andrew Grievec8f2703d2019-05-22 20:04:44255Tests are automatically sharded amongst available devices. If you run multiple
256emulators, then running test suites becomes much faster. Refer to the
257"Multiple AVD instances" section of these [emulator release notes](
258https://androidstudio.googleblog.com/2018/11/emulator-28016-stable.html)
259for more about how this works.
260```shell
Andrew Grievec214adb2019-05-28 01:39:23261$ # Start 8 emulators. Press Ctrl-C to stop them all.
262$ ( for i in $(seq 8); do ~/Android/Sdk/emulator/emulator @EMULATOR_ID -read-only & done; wait )
Nate Fischere44e0f02019-05-29 20:09:53263$ # Start 12 emulators. More than 10 requires disabling audio on some OS's. Reducing cores increases parallelism.
Andrew Grievec214adb2019-05-28 01:39:23264$ ( 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:15265```
266
John Budorick041a7df2020-04-03 23:20:27267#### Writable system partition
268
Nate Fischer7cbdeae2019-01-24 20:29:15269Unlike physical devices, an emulator's `/system` partition cannot be modified by
270default (even on rooted devices). If you need to do so (such as to remove a
271system app), you can start your emulator like so:
272```shell
273$ ~/Android/Sdk/emulator/emulator -writable-system @EMULATOR_ID
274```
275
Andrew Grieveae094e392018-06-15 16:10:22276## Using an Emulator
277 * Emulators show up just like devices via `adb devices`
278 * Device serials will look like "emulator-5554", "emulator-5556", etc.
279
Nate Fischer4a494582020-03-12 18:08:51280## Emulator pros and cons
281
282### Pros
283 * **Compiles are faster.** Many physical devices are arm64, whereas emulators
284 are typically x86 (32-bit). 64-bit builds may require 2 copies of the native
285 library (32-bit and 64-bit), so compiling for an arm64 phone is ~twice as
286 much work as for an emulator (for targets which support WebView).
287 * **APKs install faster.** Since emulators run on your workstation, adb can
288 push the APK onto the emulator without being [bandwidth-constrained by
289 USB](https://youtu.be/Mzop8bXZI3E).
290 * Emulators can be nice for working remotely. Physical devices usually require
291 `scp` or ssh port forwarding to copy the APK from your workstation and
292 install on a local device. Emulators run on your workstation, so there's **no
293 ssh slow-down**.
294
295### Cons
296 * If you're investigating a hardware-specific bug report, you'll need a
297 physical device with the actual hardware to repro that issue.
298 * x86 emulators need a separate out directory, so building for both physical
299 devices and emulators takes up more disk space (not a problem if you build
300 exclusively for the emulator).
301 * `userdebug`/`eng` emulators don't come with the Play Store installed, so you
302 can't install third party applications. Sideloading is tricky, as not all
303 third-party apps support x86.