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 | |:----:|:-------:| |
| 23 | | `tools/android/avd/proto/generic_android28.textpb` | [android-pie-x86-rel][android-pie-x86-rel] | |
| 24 | |
| 25 | You 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 | |
| 31 | The android test runner can run emulator instances on its own. In doing so, it |
| 32 | starts the emulator instances, runs tests against them, and then shuts them |
| 33 | down. 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 | |
| 70 | The test runner will set up and tear down the emulator on each invocation. |
| 71 | To 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 |
| 74 | use: |
| 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 Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 119 | By far the easiest way to set up emulator images is to use Android Studio. |
| 120 | If you don't have an [Android Studio project](android_studio.md) already, you |
| 121 | can create a blank one to be able to reach the Virtual Device Manager screen. |
| 122 | |
| 123 | Refer to: https://developer.android.com/studio/run/managing-avds.html |
| 124 | |
| 125 | Where 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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame^] | 130 | #### Creating an Image |
| 131 | |
| 132 | ##### Choosing a Skin |
| 133 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 134 | Choose a skin with a small screen for better performance (unless you care about |
| 135 | testing large screens). |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 136 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame^] | 137 | ##### Choosing an Image |
| 138 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 139 | Android Studio's image labels roughly translate to the following: |
| 140 | |
Nate Fischer | acbbaab | 2019-04-23 16:46:20 | [diff] [blame] | 141 | | 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 Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 146 | |
| 147 | *** promo |
Nate Fischer | acbbaab | 2019-04-23 16:46:20 | [diff] [blame] | 148 | **Tip:** if you're not sure which to use, choose **Google APIs** under the **x86 |
| 149 | Images** tab in the Virtual Device Configuration wizard. |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 150 | *** |
| 151 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame^] | 152 | ##### Configuration |
| 153 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 154 | "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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame^] | 158 | ##### Known Issues |
| 159 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 160 | * 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 Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame^] | 169 | |
| 170 | #### Starting an Emulator from the Command Line |
| 171 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 172 | Refer to: https://developer.android.com/studio/run/emulator-commandline.html. |
| 173 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 174 | *** promo |
| 175 | Ctrl-C will gracefully close an emulator. |
| 176 | *** |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 177 | |
Nate Fischer | 3680421 | 2020-03-31 02:12:49 | [diff] [blame] | 178 | *** promo |
| 179 | **Tip:** zsh users can add https://github.com/zsh-users/zsh-completions to |
| 180 | provide tab completion for the `emulator` command line tool. |
| 181 | *** |
| 182 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame^] | 183 | #### Basic Command Line Use |
| 184 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 185 | ```shell |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 186 | $ # List virtual devices that you've created: |
| 187 | $ ~/Android/Sdk/emulator/emulator -list-avds |
| 188 | $ # Start a named device: |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 189 | $ ~/Android/Sdk/emulator/emulator @EMULATOR_ID |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 190 | ``` |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 191 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame^] | 192 | #### Running a Headless Emulator |
| 193 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 194 | You can run an emulator without creating a window on your desktop (useful for |
| 195 | `ssh`): |
| 196 | ```shell |
Nate Fischer | 9cbee243 | 2019-04-10 14:51:58 | [diff] [blame] | 197 | $ ~/Android/Sdk/emulator/emulator -no-window @EMULATOR_ID |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 198 | $ # This also works for new enough emulator builds: |
| 199 | $ ~/Android/Sdk/emulator/emulator-headless @EMULATOR_ID |
| 200 | ``` |
| 201 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame^] | 202 | #### Running Multiple Emulators |
| 203 | |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 204 | Tests are automatically sharded amongst available devices. If you run multiple |
| 205 | emulators, then running test suites becomes much faster. Refer to the |
| 206 | "Multiple AVD instances" section of these [emulator release notes]( |
| 207 | https://androidstudio.googleblog.com/2018/11/emulator-28016-stable.html) |
| 208 | for more about how this works. |
| 209 | ```shell |
Andrew Grieve | c214adb | 2019-05-28 01:39:23 | [diff] [blame] | 210 | $ # 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 Fischer | e44e0f0 | 2019-05-29 20:09:53 | [diff] [blame] | 212 | $ # 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] | 213 | $ ( 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] | 214 | ``` |
| 215 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame^] | 216 | #### Writable system partition |
| 217 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 218 | Unlike physical devices, an emulator's `/system` partition cannot be modified by |
| 219 | default (even on rooted devices). If you need to do so (such as to remove a |
| 220 | system app), you can start your emulator like so: |
| 221 | ```shell |
| 222 | $ ~/Android/Sdk/emulator/emulator -writable-system @EMULATOR_ID |
| 223 | ``` |
| 224 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 225 | ## 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 Fischer | 4a49458 | 2020-03-12 18:08:51 | [diff] [blame] | 229 | ## 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. |