blob: 99eb1037354d04afdb96efc68c075d515fd281ff [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|:----:|:-------:|
23| `tools/android/avd/proto/generic_android28.textpb` | [android-pie-x86-rel][android-pie-x86-rel] |
24
25You can use these configuration files to run the same emulator images locally.
26
27[android-pie-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-pie-x86-rel
28
29#### Running via the test runner
30
31The android test runner can run emulator instances on its own. In doing so, it
32starts the emulator instances, runs tests against them, and then shuts them
33down. This is how builders run the emulator.
34
35##### Options
36
37 * `--avd-config`
38
39 To have the test runner run an emulator instance, use `--avd-config`:
40
41 ```
42 $ out/Debug/bin/run_base_unittests \
43 --avd-config tools/android/avd/proto/generic_android28.textpb
44 ```
45
46 * `--emulator-count`
47
48 The test runner will launch one instance by default. To have it run multiple
49 instances, use `--emulator-count`:
50
51 ```
52 $ out/Debug/bin/run_base_unittests \
53 --avd-config tools/android/avd/proto/generic_android28.textpb \
54 --emulator-count 4
55 ```
56
57 * `--emulator-window`
58
59 The test runner runs the emulator in headless mode by default. To have it run
60 with a window, use `--emulator-window`:
61
62 ```
63 $ out/Debug/bin/run_base_unittests \
64 --avd-config tools/android/avd/proto/generic_android28.textpb \
65 --emulator-window
66 ```
67
68#### Running standalone
69
70The test runner will set up and tear down the emulator on each invocation.
71To manage emulator lifetime independently, use `tools/android/avd/avd.py`.
72
73**NOTE**: You'll first need to install the emulator configuration you intend to
74use:
75
76 ```
77 $ tools/android/avd/avd.py install \
78 --avd-config tools/android/avd/proto/generic_android28.textpb
79 ```
80
81##### Options
82
83 * `--avd-config`
84
85 This behaves the same as it does for the test runner.
86
87 ```
88 $ tools/android/avd/avd.py start \
89 --avd-config tools/android/avd/proto/generic_android28.textpb
90 ```
91
92 > Note: `avd.py start` will start an emulator instance and then terminate.
93 > To shut down the emulator, use `adb emu stop`.
94
95 * `--emulator-window`
96
97 Like the test runner, `avd.py` runs the emulator in headless mode by default.
98 To have it run with a window, use `--emulator-window`:
99
100 ```
101 $ tools/android/avd/avd.py start \
102 --avd-config tools/android/avd/proto/generic_android28.textpb \
103 --emulator-window
104 ```
105
106 * `--no-read-only`
107
108 `avd.py` runs the emulator in read-only mode by default. To run a modifiable
109 emulator, use `--no-read-only`:
110
111 ```
112 $ tools/android/avd/avd.py start \
113 --avd-config tools/android/avd/proto/generic_android28.textpb \
114 --no-read-only
115 ```
116
117### Using Your Own Emulator Image
118
Andrew Grieveae094e392018-06-15 16:10:22119By far the easiest way to set up emulator images is to use Android Studio.
120If you don't have an [Android Studio project](android_studio.md) already, you
121can create a blank one to be able to reach the Virtual Device Manager screen.
122
123Refer to: https://developer.android.com/studio/run/managing-avds.html
124
125Where files live:
126 * System partition images are stored within the sdk directory.
127 * Emulator configs and data partition images are stored within
128 `~/.android/avd/`.
129
John Budorick041a7df2020-04-03 23:20:27130#### Creating an Image
131
132##### Choosing a Skin
133
Nate Fischer7cbdeae2019-01-24 20:29:15134Choose a skin with a small screen for better performance (unless you care about
135testing large screens).
Andrew Grieveae094e392018-06-15 16:10:22136
John Budorick041a7df2020-04-03 23:20:27137##### Choosing an Image
138
Nate Fischer7cbdeae2019-01-24 20:29:15139Android Studio's image labels roughly translate to the following:
140
Nate Fischeracbbaab2019-04-23 16:46:20141| AVD "Target" | Virtual Device Configuration tab | GMS? | Build Properties |
142| --- | --- | --- | --- |
143| Google Play | "Recommended" (the default tab) | This has GMS | `user`/`release-keys` |
144| Google APIs | "x86 Images" | This has GMS | `userdebug`/`dev-keys` |
145| No label | "x86 Images" | AOSP image, does not have GMS | `eng`/`test-keys` |
Nate Fischer7cbdeae2019-01-24 20:29:15146
147*** promo
Nate Fischeracbbaab2019-04-23 16:46:20148**Tip:** if you're not sure which to use, choose **Google APIs** under the **x86
149Images** tab in the Virtual Device Configuration wizard.
Nate Fischer7cbdeae2019-01-24 20:29:15150***
151
John Budorick041a7df2020-04-03 23:20:27152##### Configuration
153
Nate Fischer7cbdeae2019-01-24 20:29:15154"Show Advanced Settings" > scroll down:
155* Set internal storage to 4000MB (component builds are really big).
156* Set SD card to 1000MB (our tests push a lot of files to /sdcard).
157
John Budorick041a7df2020-04-03 23:20:27158##### Known Issues
159
Andrew Grieveae094e392018-06-15 16:10:22160 * Our test & installer scripts do not work with pre-MR1 Jelly Bean.
161 * Component builds do not work on pre-KitKat (due to the OS having a max
162 number of shared libraries).
163 * Jelly Bean and KitKat images sometimes forget to mount /sdcard :(.
164 * This causes tests to fail.
165 * To ensure it's there: `adb -s emulator-5554 shell mount` (look for /sdcard)
166 * Can often be fixed by editing `~/.android/avd/YOUR_DEVICE/config.ini`.
167 * Look for `hw.sdCard=no` and set it to `yes`
168
John Budorick041a7df2020-04-03 23:20:27169
170#### Starting an Emulator from the Command Line
171
Andrew Grieveae094e392018-06-15 16:10:22172Refer to: https://developer.android.com/studio/run/emulator-commandline.html.
173
Nate Fischer7cbdeae2019-01-24 20:29:15174*** promo
175Ctrl-C will gracefully close an emulator.
176***
Andrew Grieveae094e392018-06-15 16:10:22177
Nate Fischer36804212020-03-31 02:12:49178*** promo
179**Tip:** zsh users can add https://github.com/zsh-users/zsh-completions to
180provide tab completion for the `emulator` command line tool.
181***
182
John Budorick041a7df2020-04-03 23:20:27183#### Basic Command Line Use
184
Nate Fischer7cbdeae2019-01-24 20:29:15185```shell
Andrew Grievec8f2703d2019-05-22 20:04:44186$ # List virtual devices that you've created:
187$ ~/Android/Sdk/emulator/emulator -list-avds
188$ # Start a named device:
Nate Fischer7cbdeae2019-01-24 20:29:15189$ ~/Android/Sdk/emulator/emulator @EMULATOR_ID
Andrew Grieveae094e392018-06-15 16:10:22190```
Nate Fischer7cbdeae2019-01-24 20:29:15191
John Budorick041a7df2020-04-03 23:20:27192#### Running a Headless Emulator
193
Nate Fischer7cbdeae2019-01-24 20:29:15194You can run an emulator without creating a window on your desktop (useful for
195`ssh`):
196```shell
Nate Fischer9cbee2432019-04-10 14:51:58197$ ~/Android/Sdk/emulator/emulator -no-window @EMULATOR_ID
Andrew Grievec8f2703d2019-05-22 20:04:44198$ # This also works for new enough emulator builds:
199$ ~/Android/Sdk/emulator/emulator-headless @EMULATOR_ID
200```
201
John Budorick041a7df2020-04-03 23:20:27202#### Running Multiple Emulators
203
Andrew Grievec8f2703d2019-05-22 20:04:44204Tests are automatically sharded amongst available devices. If you run multiple
205emulators, then running test suites becomes much faster. Refer to the
206"Multiple AVD instances" section of these [emulator release notes](
207https://androidstudio.googleblog.com/2018/11/emulator-28016-stable.html)
208for more about how this works.
209```shell
Andrew Grievec214adb2019-05-28 01:39:23210$ # Start 8 emulators. Press Ctrl-C to stop them all.
211$ ( for i in $(seq 8); do ~/Android/Sdk/emulator/emulator @EMULATOR_ID -read-only & done; wait )
Nate Fischere44e0f02019-05-29 20:09:53212$ # Start 12 emulators. More than 10 requires disabling audio on some OS's. Reducing cores increases parallelism.
Andrew Grievec214adb2019-05-28 01:39:23213$ ( 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:15214```
215
John Budorick041a7df2020-04-03 23:20:27216#### Writable system partition
217
Nate Fischer7cbdeae2019-01-24 20:29:15218Unlike physical devices, an emulator's `/system` partition cannot be modified by
219default (even on rooted devices). If you need to do so (such as to remove a
220system app), you can start your emulator like so:
221```shell
222$ ~/Android/Sdk/emulator/emulator -writable-system @EMULATOR_ID
223```
224
Andrew Grieveae094e392018-06-15 16:10:22225## Using an Emulator
226 * Emulators show up just like devices via `adb devices`
227 * Device serials will look like "emulator-5554", "emulator-5556", etc.
228
Nate Fischer4a494582020-03-12 18:08:51229## Emulator pros and cons
230
231### Pros
232 * **Compiles are faster.** Many physical devices are arm64, whereas emulators
233 are typically x86 (32-bit). 64-bit builds may require 2 copies of the native
234 library (32-bit and 64-bit), so compiling for an arm64 phone is ~twice as
235 much work as for an emulator (for targets which support WebView).
236 * **APKs install faster.** Since emulators run on your workstation, adb can
237 push the APK onto the emulator without being [bandwidth-constrained by
238 USB](https://youtu.be/Mzop8bXZI3E).
239 * Emulators can be nice for working remotely. Physical devices usually require
240 `scp` or ssh port forwarding to copy the APK from your workstation and
241 install on a local device. Emulators run on your workstation, so there's **no
242 ssh slow-down**.
243
244### Cons
245 * If you're investigating a hardware-specific bug report, you'll need a
246 physical device with the actual hardware to repro that issue.
247 * x86 emulators need a separate out directory, so building for both physical
248 devices and emulators takes up more disk space (not a problem if you build
249 exclusively for the emulator).
250 * `userdebug`/`eng` emulators don't come with the Play Store installed, so you
251 can't install third party applications. Sideloading is tricky, as not all
252 third-party apps support x86.