Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 1 | # Using an Android Emulator |
Nate Fischer | 16f9453 | 2019-03-27 20:51:07 | [diff] [blame] | 2 | Always use x86 emulators (or x86\_64 for testing 64-bit APKs). Although arm |
| 3 | emulators exist, they are so slow that they are not worth your time. |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 4 | |
Nate Fischer | 4a49458 | 2020-03-12 18:08:51 | [diff] [blame] | 5 | [TOC] |
Nate Fischer | acbbaab | 2019-04-23 16:46:20 | [diff] [blame] | 6 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 7 | ## Building for Emulation |
| 8 | You need to target the correct architecture via GN args: |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 9 | ```gn |
Nate Fischer | 16f9453 | 2019-03-27 20:51:07 | [diff] [blame] | 10 | target_cpu = "x86" # or "x64" if you have an x86_64 emulator |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 11 | ``` |
| 12 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 13 | ## Running an Emulator |
| 14 | |
| 15 | ### Using Prebuilt CIPD packages |
| 16 | |
| 17 | Chromium has a set of prebuilt images stored as CIPD packages. These are used |
| 18 | by various builders to run tests on the emulator. Their configurations are |
| 19 | currently stored in `//tools/android/avd/proto`. |
| 20 | |
| 21 | | File | Builder | |
| 22 | |:----:|:-------:| |
Haiyang Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 23 | | `tools/android/avd/proto/generic_android23.textpb` | [android-marshmallow-x86-rel][android-marshmallow-x86-rel] | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 24 | | `tools/android/avd/proto/generic_android28.textpb` | [android-pie-x86-rel][android-pie-x86-rel] | |
Haiyang Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 25 | | `tools/android/avd/proto/generic_playstore_android28.textpb` | [android-pie-x86-rel][android-pie-x86-rel] | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 26 | |
| 27 | You can use these configuration files to run the same emulator images locally. |
| 28 | |
Haiyang Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 29 | [android-marshmallow-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-marshmallow-x86-rel |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 30 | [android-pie-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-pie-x86-rel |
| 31 | |
Haiyang Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 32 | #### 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 Wen | 96981e20 | 2021-08-06 21:13:40 | [diff] [blame^] | 53 | 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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 60 | #### Running via the test runner |
| 61 | |
| 62 | The android test runner can run emulator instances on its own. In doing so, it |
| 63 | starts the emulator instances, runs tests against them, and then shuts them |
| 64 | down. 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 | |
| 101 | The test runner will set up and tear down the emulator on each invocation. |
| 102 | To manage emulator lifetime independently, use `tools/android/avd/avd.py`. |
| 103 | |
Haiyang Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 104 | > 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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 112 | |
| 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 Struiksma | e6fcbf0 | 2021-05-03 23:18:06 | [diff] [blame] | 125 | > To shut down the emulator, use `adb emu kill`. |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 126 | |
| 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 Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 149 | * `--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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 161 | ### Using Your Own Emulator Image |
| 162 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 163 | By far the easiest way to set up emulator images is to use Android Studio. |
| 164 | If you don't have an [Android Studio project](android_studio.md) already, you |
| 165 | can create a blank one to be able to reach the Virtual Device Manager screen. |
| 166 | |
| 167 | Refer to: https://developer.android.com/studio/run/managing-avds.html |
| 168 | |
| 169 | Where 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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 174 | #### Creating an Image |
| 175 | |
| 176 | ##### Choosing a Skin |
| 177 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 178 | Choose a skin with a small screen for better performance (unless you care about |
| 179 | testing large screens). |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 180 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 181 | ##### Choosing an Image |
| 182 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 183 | Android Studio's image labels roughly translate to the following: |
| 184 | |
Nate Fischer | acbbaab | 2019-04-23 16:46:20 | [diff] [blame] | 185 | | 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 Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 190 | |
| 191 | *** promo |
Nate Fischer | acbbaab | 2019-04-23 16:46:20 | [diff] [blame] | 192 | **Tip:** if you're not sure which to use, choose **Google APIs** under the **x86 |
| 193 | Images** tab in the Virtual Device Configuration wizard. |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 194 | *** |
| 195 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 196 | ##### Configuration |
| 197 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 198 | "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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 202 | ##### Known Issues |
| 203 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 204 | * 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 Fischer | 0dacc46 | 2021-04-06 16:10:24 | [diff] [blame] | 212 | * 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 Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 219 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 220 | |
| 221 | #### Starting an Emulator from the Command Line |
| 222 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 223 | Refer to: https://developer.android.com/studio/run/emulator-commandline.html. |
| 224 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 225 | *** promo |
| 226 | Ctrl-C will gracefully close an emulator. |
| 227 | *** |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 228 | |
Nate Fischer | 3680421 | 2020-03-31 02:12:49 | [diff] [blame] | 229 | *** promo |
| 230 | **Tip:** zsh users can add https://github.com/zsh-users/zsh-completions to |
| 231 | provide tab completion for the `emulator` command line tool. |
| 232 | *** |
| 233 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 234 | #### Basic Command Line Use |
| 235 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 236 | ```shell |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 237 | $ # List virtual devices that you've created: |
| 238 | $ ~/Android/Sdk/emulator/emulator -list-avds |
| 239 | $ # Start a named device: |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 240 | $ ~/Android/Sdk/emulator/emulator @EMULATOR_ID |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 241 | ``` |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 242 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 243 | #### Running a Headless Emulator |
| 244 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 245 | You can run an emulator without creating a window on your desktop (useful for |
| 246 | `ssh`): |
| 247 | ```shell |
Nate Fischer | 9cbee243 | 2019-04-10 14:51:58 | [diff] [blame] | 248 | $ ~/Android/Sdk/emulator/emulator -no-window @EMULATOR_ID |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 249 | $ # This also works for new enough emulator builds: |
| 250 | $ ~/Android/Sdk/emulator/emulator-headless @EMULATOR_ID |
| 251 | ``` |
| 252 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 253 | #### Running Multiple Emulators |
| 254 | |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 255 | Tests are automatically sharded amongst available devices. If you run multiple |
| 256 | emulators, then running test suites becomes much faster. Refer to the |
| 257 | "Multiple AVD instances" section of these [emulator release notes]( |
| 258 | https://androidstudio.googleblog.com/2018/11/emulator-28016-stable.html) |
| 259 | for more about how this works. |
| 260 | ```shell |
Andrew Grieve | c214adb | 2019-05-28 01:39:23 | [diff] [blame] | 261 | $ # 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 Fischer | e44e0f0 | 2019-05-29 20:09:53 | [diff] [blame] | 263 | $ # Start 12 emulators. More than 10 requires disabling audio on some OS's. Reducing cores increases parallelism. |
Andrew Grieve | c214adb | 2019-05-28 01:39:23 | [diff] [blame] | 264 | $ ( for i in $(seq 12); do ~/Android/Sdk/emulator/emulator @EMULATOR_ID -read-only -no-audio -cores 2 & done; wait ) |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 265 | ``` |
| 266 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 267 | #### Writable system partition |
| 268 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 269 | Unlike physical devices, an emulator's `/system` partition cannot be modified by |
| 270 | default (even on rooted devices). If you need to do so (such as to remove a |
| 271 | system app), you can start your emulator like so: |
| 272 | ```shell |
| 273 | $ ~/Android/Sdk/emulator/emulator -writable-system @EMULATOR_ID |
| 274 | ``` |
| 275 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 276 | ## 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 Fischer | 4a49458 | 2020-03-12 18:08:51 | [diff] [blame] | 280 | ## 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. |