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 |
Haiyang Pan | b06f846d | 2021-12-21 17:41:55 | [diff] [blame] | 19 | currently stored in [`//tools/android/avd/proto`](../tools/android/avd/proto/). |
Andrew Grieve | 1b9be14 | 2022-02-10 04:07:00 | [diff] [blame^] | 20 | You can run this command to list them: |
| 21 | ```sh |
| 22 | tools/android/avd/avd.py list |
| 23 | ``` |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 24 | |
Haiyang Pan | b06f846d | 2021-12-21 17:41:55 | [diff] [blame] | 25 | | 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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 37 | |
| 38 | You can use these configuration files to run the same emulator images locally. |
| 39 | |
Haiyang Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 40 | [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] | 41 | [android-pie-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-pie-x86-rel |
Haiyang Pan | b06f846d | 2021-12-21 17:41:55 | [diff] [blame] | 42 | [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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 44 | |
Haiyang Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 45 | #### 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 Wen | 96981e20 | 2021-08-06 21:13:40 | [diff] [blame] | 66 | 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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 73 | #### Running via the test runner |
| 74 | |
| 75 | The android test runner can run emulator instances on its own. In doing so, it |
| 76 | starts the emulator instances, runs tests against them, and then shuts them |
| 77 | down. 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 | |
| 114 | The test runner will set up and tear down the emulator on each invocation. |
| 115 | To manage emulator lifetime independently, use `tools/android/avd/avd.py`. |
| 116 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 117 | ##### 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 Struiksma | e6fcbf0 | 2021-05-03 23:18:06 | [diff] [blame] | 129 | > To shut down the emulator, use `adb emu kill`. |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 130 | |
| 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 Pan | b06f846d | 2021-12-21 17:41:55 | [diff] [blame] | 142 | * `--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 Wen | b105afb | 2021-12-07 20:24:36 | [diff] [blame] | 158 | * `--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 Pan | b06f846d | 2021-12-21 17:41:55 | [diff] [blame] | 171 | * `--writable-system` |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 172 | |
Haiyang Pan | b06f846d | 2021-12-21 17:41:55 | [diff] [blame] | 173 | Makes system & vendor image writable after adb remount. |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 174 | |
Haiyang Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 175 | * `--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 Wen | b105afb | 2021-12-07 20:24:36 | [diff] [blame] | 180 | Use `--debug-tags="*"` if you want to output all logs (warning: it is quite |
| 181 | verbose). |
Haiyang Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 182 | |
| 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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 189 | ### Using Your Own Emulator Image |
| 190 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 191 | By far the easiest way to set up emulator images is to use Android Studio. |
| 192 | If you don't have an [Android Studio project](android_studio.md) already, you |
| 193 | can create a blank one to be able to reach the Virtual Device Manager screen. |
| 194 | |
| 195 | Refer to: https://developer.android.com/studio/run/managing-avds.html |
| 196 | |
| 197 | Where 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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 202 | #### Creating an Image |
| 203 | |
| 204 | ##### Choosing a Skin |
| 205 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 206 | Choose a skin with a small screen for better performance (unless you care about |
| 207 | testing large screens). |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 208 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 209 | ##### Choosing an Image |
| 210 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 211 | Android Studio's image labels roughly translate to the following: |
| 212 | |
Nate Fischer | acbbaab | 2019-04-23 16:46:20 | [diff] [blame] | 213 | | 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 Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 218 | |
| 219 | *** promo |
Nate Fischer | acbbaab | 2019-04-23 16:46:20 | [diff] [blame] | 220 | **Tip:** if you're not sure which to use, choose **Google APIs** under the **x86 |
| 221 | Images** tab in the Virtual Device Configuration wizard. |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 222 | *** |
| 223 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 224 | ##### Configuration |
| 225 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 226 | "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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 230 | ##### Known Issues |
| 231 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 232 | * 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 Fischer | 0dacc46 | 2021-04-06 16:10:24 | [diff] [blame] | 240 | * 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 Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 247 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 248 | |
| 249 | #### Starting an Emulator from the Command Line |
| 250 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 251 | Refer to: https://developer.android.com/studio/run/emulator-commandline.html. |
| 252 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 253 | *** promo |
| 254 | Ctrl-C will gracefully close an emulator. |
| 255 | *** |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 256 | |
Nate Fischer | 3680421 | 2020-03-31 02:12:49 | [diff] [blame] | 257 | *** promo |
| 258 | **Tip:** zsh users can add https://github.com/zsh-users/zsh-completions to |
| 259 | provide tab completion for the `emulator` command line tool. |
| 260 | *** |
| 261 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 262 | #### Basic Command Line Use |
| 263 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 264 | ```shell |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 265 | $ # List virtual devices that you've created: |
| 266 | $ ~/Android/Sdk/emulator/emulator -list-avds |
| 267 | $ # Start a named device: |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 268 | $ ~/Android/Sdk/emulator/emulator @EMULATOR_ID |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 269 | ``` |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 270 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 271 | #### Running a Headless Emulator |
| 272 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 273 | You can run an emulator without creating a window on your desktop (useful for |
| 274 | `ssh`): |
| 275 | ```shell |
Nate Fischer | 9cbee243 | 2019-04-10 14:51:58 | [diff] [blame] | 276 | $ ~/Android/Sdk/emulator/emulator -no-window @EMULATOR_ID |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 277 | $ # This also works for new enough emulator builds: |
| 278 | $ ~/Android/Sdk/emulator/emulator-headless @EMULATOR_ID |
| 279 | ``` |
| 280 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 281 | #### Running Multiple Emulators |
| 282 | |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 283 | Tests are automatically sharded amongst available devices. If you run multiple |
| 284 | emulators, then running test suites becomes much faster. Refer to the |
| 285 | "Multiple AVD instances" section of these [emulator release notes]( |
| 286 | https://androidstudio.googleblog.com/2018/11/emulator-28016-stable.html) |
| 287 | for more about how this works. |
| 288 | ```shell |
Andrew Grieve | c214adb | 2019-05-28 01:39:23 | [diff] [blame] | 289 | $ # 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 Fischer | e44e0f0 | 2019-05-29 20:09:53 | [diff] [blame] | 291 | $ # 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] | 292 | $ ( 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] | 293 | ``` |
| 294 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 295 | #### Writable system partition |
| 296 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 297 | Unlike physical devices, an emulator's `/system` partition cannot be modified by |
| 298 | default (even on rooted devices). If you need to do so (such as to remove a |
| 299 | system app), you can start your emulator like so: |
| 300 | ```shell |
| 301 | $ ~/Android/Sdk/emulator/emulator -writable-system @EMULATOR_ID |
| 302 | ``` |
| 303 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 304 | ## 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 Fischer | 4a49458 | 2020-03-12 18:08:51 | [diff] [blame] | 308 | ## 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. |