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 | |
Erik Chen | 254ec1cc | 2024-01-18 02:11:49 | [diff] [blame] | 15 | ### Googler-only Emulator Instructions |
| 16 | |
| 17 | See http://go/clank-emulator/ |
| 18 | |
Haiyang Pan | 03b1c6c | 2025-01-08 23:34:07 | [diff] [blame] | 19 | ### Using Prebuilt AVD configurations. |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 20 | |
Haiyang Pan | 03b1c6c | 2025-01-08 23:34:07 | [diff] [blame] | 21 | Chromium has a set of prebuilt AVD configurations stored as CIPD packages. |
| 22 | These are used by various builders to run tests on the emulator. Their |
| 23 | configurations are currently stored in [`//tools/android/avd/proto`](../tools/android/avd/proto/). |
Andrew Grieve | 1b9be14 | 2022-02-10 04:07:00 | [diff] [blame] | 24 | You can run this command to list them: |
| 25 | ```sh |
| 26 | tools/android/avd/avd.py list |
| 27 | ``` |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 28 | |
Haiyang Pan | 03b1c6c | 2025-01-08 23:34:07 | [diff] [blame] | 29 | | Configurations | Android Version | Form Factor | Builder | |
Haiyang Pan | 0fca0dc | 2024-03-25 17:50:30 | [diff] [blame] | 30 | |:-------------- |:--------------- |:------- |:---------- |:------- | |
Haiyang Pan | 03b1c6c | 2025-01-08 23:34:07 | [diff] [blame] | 31 | | `generic_android26.textpb` | 8.0 (O) | Phone | [android-oreo-x86-rel][android-oreo-x86-rel] | |
| 32 | | `generic_android27.textpb` | 8.1 (O_MR1) | Phone | N/A | |
| 33 | | `android_28_google_apis_x86.textpb` | 9 (P) | Phone | [android-pie-x86-rel][android-pie-x86-rel] | |
| 34 | | `android_29_google_apis_x86.textpb` | 10 (Q) | Phone | N/A | |
| 35 | | `android_30_google_apis_x86.textpb` | 11 (R) | Phone | [android-11-x86-rel][android-11-x86-rel] | |
| 36 | | `android_31_google_apis_x64.textpb` | 12 (S) | Phone | [android-12-x64-rel][android-12-x64-rel] | |
| 37 | | `android_32_google_apis_x64_foldable.textpb` | 12L (S_V2) | Foldable Phone | [android-12l-x64-dbg-tests][android-12l-x64-dbg-tests] | |
| 38 | | `android_32_google_apis_x64_foldable_landscape.textpb` | 12L (S_V2) | Foldable Phone (Landscape) | [android-12l-landscape-x64-dbg-tests][android-12l-landscape-x64-dbg-tests] | |
| 39 | | `android_33_google_apis_x64.textpb` | 13 (T) | Phone | [android-13-x64-rel][android-13-x64-rel] | |
| 40 | | `android_34_google_apis_x64.textpb` | 14 (U) | Phone | [android-14-x64-rel][android-14-x64-rel] | |
| 41 | | `android_35_google_apis_x64.textpb` | 15 (V) | Phone | [android-15-x64-rel][android-15-x64-rel] | |
| 42 | | `android_35_google_apis_x64_tablet.textpb` | 15 (V) | Tablet | [android-15-tablet-x64-dbg-tests][android-15-tablet-x64-dbg-tests] | |
| 43 | | `android_35_google_apis_x64_tablet_landscape.textpb` | 15 (V) | Tablet (Landscape) | [android-15-tablet-landscape-x64-dbg-tests][android-15-tablet-landscape-x64-dbg-tests] | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 44 | |
| 45 | You can use these configuration files to run the same emulator images locally. |
| 46 | |
Haiyang Pan | 03b1c6c | 2025-01-08 23:34:07 | [diff] [blame] | 47 | [android-oreo-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-oreo-x86-rel |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 48 | [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] | 49 | [android-11-x86-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-11-x86-rel |
Haiyang Pan | 2a55a45 | 2022-03-03 08:18:33 | [diff] [blame] | 50 | [android-12-x64-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-12-x64-rel |
Haiyang Pan | 0fca0dc | 2024-03-25 17:50:30 | [diff] [blame] | 51 | [android-12l-x64-dbg-tests]: https://ci.chromium.org/p/chromium/builders/ci/android-12l-x64-dbg-tests |
Haiyang Pan | 03b1c6c | 2025-01-08 23:34:07 | [diff] [blame] | 52 | [android-12l-landscape-x64-dbg-tests]: https://ci.chromium.org/p/chromium/builders/ci/android-12l-landscape-x64-dbg-tests |
Haiyang Pan | 0fca0dc | 2024-03-25 17:50:30 | [diff] [blame] | 53 | [android-13-x64-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-13-x64-rel |
| 54 | [android-14-x64-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-14-x64-rel |
Haiyang Pan | 03b1c6c | 2025-01-08 23:34:07 | [diff] [blame] | 55 | [android-15-x64-rel]: https://ci.chromium.org/p/chromium/builders/ci/android-15-x64-rel |
| 56 | [android-15-tablet-x64-dbg-tests]: https://ci.chromium.org/ui/p/chromium/builders/ci/android-15-tablet-x64-dbg-tests |
| 57 | [android-15-tablet-landscape-x64-dbg-tests]: https://ci.chromium.org/ui/p/chromium/builders/ci/android-15-tablet-landscape-x64-dbg-tests |
| 58 | |
| 59 | > Note: New AVD configurations for LOCAL development are also available now, |
| 60 | > whose file names end with `_local`, i.e. `*_local.textpb`. |
| 61 | > |
| 62 | > The only difference is that these configs has 12GB storage space instead of |
| 63 | > 4GB on the non-local ones. Larger storage space makes it easier for developers |
| 64 | > to install large APKs without hitting the space issue. |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 65 | |
Haiyang Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 66 | #### Prerequisite |
| 67 | |
| 68 | * Make sure KVM (Kernel-based Virtual Machine) is enabled. |
| 69 | See this |
| 70 | [link](https://developer.android.com/studio/run/emulator-acceleration#vm-linux) |
| 71 | from android studio for more details and instructions. |
| 72 | |
| 73 | * You need to have the permissions to use KVM. |
| 74 | Use the following command to see if you are in group `kvm`: |
| 75 | |
| 76 | ``` |
| 77 | $ grep kvm /etc/group |
| 78 | ``` |
| 79 | |
| 80 | If your username is not shown in the group, add yourself to the group: |
| 81 | |
| 82 | ``` |
| 83 | $ sudo adduser $USER kvm |
| 84 | $ newgrp kvm |
| 85 | ``` |
| 86 | |
Erik Chen | 254ec1cc | 2024-01-18 02:11:49 | [diff] [blame] | 87 | You need to log out and log back in so the new groups take effect. |
Peter Wen | 96981e20 | 2021-08-06 21:13:40 | [diff] [blame] | 88 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 89 | #### Running via the test runner |
| 90 | |
| 91 | The android test runner can run emulator instances on its own. In doing so, it |
| 92 | starts the emulator instances, runs tests against them, and then shuts them |
| 93 | down. This is how builders run the emulator. |
| 94 | |
| 95 | ##### Options |
| 96 | |
| 97 | * `--avd-config` |
| 98 | |
| 99 | To have the test runner run an emulator instance, use `--avd-config`: |
| 100 | |
| 101 | ``` |
| 102 | $ out/Debug/bin/run_base_unittests \ |
Haiyang Pan | 56581b3 | 2024-07-03 20:46:40 | [diff] [blame] | 103 | --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 104 | ``` |
| 105 | |
| 106 | * `--emulator-count` |
| 107 | |
| 108 | The test runner will launch one instance by default. To have it run multiple |
| 109 | instances, use `--emulator-count`: |
| 110 | |
| 111 | ``` |
| 112 | $ out/Debug/bin/run_base_unittests \ |
Haiyang Pan | 56581b3 | 2024-07-03 20:46:40 | [diff] [blame] | 113 | --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \ |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 114 | --emulator-count 4 |
| 115 | ``` |
| 116 | |
sbingler | 8d91e8c | 2024-04-23 16:36:58 | [diff] [blame] | 117 | * `--emulator-enable-network` |
| 118 | |
| 119 | The test runner runs the emulator without network access by default. To have |
| 120 | it run with network access, use `--emulator-enable-network`: |
| 121 | |
| 122 | ``` |
| 123 | $ out/Debug/bin/run_base_unittests \ |
Haiyang Pan | 56581b3 | 2024-07-03 20:46:40 | [diff] [blame] | 124 | --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \ |
sbingler | 8d91e8c | 2024-04-23 16:36:58 | [diff] [blame] | 125 | --emulator-enable-network |
| 126 | ``` |
| 127 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 128 | * `--emulator-window` |
| 129 | |
| 130 | The test runner runs the emulator in headless mode by default. To have it run |
| 131 | with a window, use `--emulator-window`: |
| 132 | |
| 133 | ``` |
| 134 | $ out/Debug/bin/run_base_unittests \ |
Haiyang Pan | 56581b3 | 2024-07-03 20:46:40 | [diff] [blame] | 135 | --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \ |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 136 | --emulator-window |
| 137 | ``` |
| 138 | |
| 139 | #### Running standalone |
| 140 | |
| 141 | The test runner will set up and tear down the emulator on each invocation. |
| 142 | To manage emulator lifetime independently, use `tools/android/avd/avd.py`. |
| 143 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 144 | ##### Options |
| 145 | |
| 146 | * `--avd-config` |
| 147 | |
| 148 | This behaves the same as it does for the test runner. |
| 149 | |
| 150 | ``` |
| 151 | $ tools/android/avd/avd.py start \ |
Haiyang Pan | 56581b3 | 2024-07-03 20:46:40 | [diff] [blame] | 152 | --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 153 | ``` |
| 154 | |
| 155 | > Note: `avd.py start` will start an emulator instance and then terminate. |
Ian Struiksma | e6fcbf0 | 2021-05-03 23:18:06 | [diff] [blame] | 156 | > To shut down the emulator, use `adb emu kill`. |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 157 | |
sbingler | 8d91e8c | 2024-04-23 16:36:58 | [diff] [blame] | 158 | * `--enable-network` |
| 159 | |
| 160 | Like the test runner, `avd.py` runs the emulator without network access by |
| 161 | default. To enable network access, use `--enable-network`: |
| 162 | |
| 163 | ``` |
| 164 | $ tools/android/avd/avd.py start \ |
Haiyang Pan | 56581b3 | 2024-07-03 20:46:40 | [diff] [blame] | 165 | --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \ |
sbingler | 8d91e8c | 2024-04-23 16:36:58 | [diff] [blame] | 166 | --enable-network |
| 167 | ``` |
| 168 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 169 | * `--emulator-window` |
| 170 | |
| 171 | Like the test runner, `avd.py` runs the emulator in headless mode by default. |
| 172 | To have it run with a window, use `--emulator-window`: |
| 173 | |
| 174 | ``` |
| 175 | $ tools/android/avd/avd.py start \ |
Haiyang Pan | 56581b3 | 2024-07-03 20:46:40 | [diff] [blame] | 176 | --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \ |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 177 | --emulator-window |
| 178 | ``` |
| 179 | |
Haiyang Pan | b06f846d | 2021-12-21 17:41:55 | [diff] [blame] | 180 | * `--gpu-mode GPU_MODE` |
| 181 | |
| 182 | Override the mode of hardware OpenGL ES emulation indicated by the AVD. |
| 183 | See "emulator -help-gpu" for a full list of modes. |
| 184 | |
| 185 | * `--no-read-only` |
| 186 | |
| 187 | `avd.py` runs the emulator in read-only mode by default. To run a modifiable |
| 188 | emulator, use `--no-read-only`: |
| 189 | |
| 190 | ``` |
| 191 | $ tools/android/avd/avd.py start \ |
Haiyang Pan | 56581b3 | 2024-07-03 20:46:40 | [diff] [blame] | 192 | --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \ |
Haiyang Pan | b06f846d | 2021-12-21 17:41:55 | [diff] [blame] | 193 | --no-read-only |
| 194 | ``` |
| 195 | |
Peter Wen | b105afb | 2021-12-07 20:24:36 | [diff] [blame] | 196 | * `--wipe-data` |
| 197 | |
Haiyang Pan | e5e2bc0 | 2023-10-20 19:31:53 | [diff] [blame] | 198 | Reset the /data partition to the factory defaults. This removes all user |
| 199 | settings from the AVD. |
Peter Wen | b105afb | 2021-12-07 20:24:36 | [diff] [blame] | 200 | |
| 201 | ``` |
| 202 | $ tools/android/avd/avd.py start \ |
Haiyang Pan | 56581b3 | 2024-07-03 20:46:40 | [diff] [blame] | 203 | --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \ |
Peter Wen | b105afb | 2021-12-07 20:24:36 | [diff] [blame] | 204 | --wipe-data |
| 205 | ``` |
| 206 | |
Haiyang Pan | b06f846d | 2021-12-21 17:41:55 | [diff] [blame] | 207 | * `--writable-system` |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 208 | |
Victor Vianna | 725b107 | 2022-03-04 16:02:59 | [diff] [blame] | 209 | Makes system & vendor image writable. It's necessary to run |
| 210 | ``` |
| 211 | adb root |
| 212 | adb remount |
| 213 | ``` |
| 214 | after the emulator starts. |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 215 | |
Haiyang Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 216 | * `--debug-tags` |
| 217 | |
| 218 | `avd.py` disables the emulator log by default. When this option is used, |
| 219 | emulator log will be enabled. It is useful when the emulator cannot be |
| 220 | launched correctly. See `emulator -help-debug-tags` for a full list of tags. |
Haiyang Pan | 7c2d20f | 2024-02-20 21:59:28 | [diff] [blame] | 221 | Use `--debug-tags=all` if you want to output all logs (warning: it is quite |
Peter Wen | b105afb | 2021-12-07 20:24:36 | [diff] [blame] | 222 | verbose). |
Haiyang Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 223 | |
| 224 | ``` |
| 225 | $ tools/android/avd/avd.py start \ |
Haiyang Pan | 56581b3 | 2024-07-03 20:46:40 | [diff] [blame] | 226 | --avd-config tools/android/avd/proto/android_33_google_apis_x64.textpb \ |
Haiyang Pan | f5faf23 | 2020-11-06 18:10:34 | [diff] [blame] | 227 | --debug-tags init,snapshot |
| 228 | ``` |
| 229 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 230 | ### Using Your Own Emulator Image |
| 231 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 232 | By far the easiest way to set up emulator images is to use Android Studio. |
| 233 | If you don't have an [Android Studio project](android_studio.md) already, you |
| 234 | can create a blank one to be able to reach the Virtual Device Manager screen. |
| 235 | |
| 236 | Refer to: https://developer.android.com/studio/run/managing-avds.html |
| 237 | |
| 238 | Where files live: |
| 239 | * System partition images are stored within the sdk directory. |
| 240 | * Emulator configs and data partition images are stored within |
| 241 | `~/.android/avd/`. |
| 242 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 243 | #### Creating an Image |
| 244 | |
| 245 | ##### Choosing a Skin |
| 246 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 247 | Choose a skin with a small screen for better performance (unless you care about |
| 248 | testing large screens). |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 249 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 250 | ##### Choosing an Image |
| 251 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 252 | Android Studio's image labels roughly translate to the following: |
| 253 | |
Nate Fischer | acbbaab | 2019-04-23 16:46:20 | [diff] [blame] | 254 | | AVD "Target" | Virtual Device Configuration tab | GMS? | Build Properties | |
| 255 | | --- | --- | --- | --- | |
| 256 | | Google Play | "Recommended" (the default tab) | This has GMS | `user`/`release-keys` | |
| 257 | | Google APIs | "x86 Images" | This has GMS | `userdebug`/`dev-keys` | |
| 258 | | No label | "x86 Images" | AOSP image, does not have GMS | `eng`/`test-keys` | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 259 | |
| 260 | *** promo |
Nate Fischer | acbbaab | 2019-04-23 16:46:20 | [diff] [blame] | 261 | **Tip:** if you're not sure which to use, choose **Google APIs** under the **x86 |
| 262 | Images** tab in the Virtual Device Configuration wizard. |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 263 | *** |
| 264 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 265 | ##### Configuration |
| 266 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 267 | "Show Advanced Settings" > scroll down: |
| 268 | * Set internal storage to 4000MB (component builds are really big). |
| 269 | * Set SD card to 1000MB (our tests push a lot of files to /sdcard). |
| 270 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 271 | ##### Known Issues |
| 272 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 273 | * Our test & installer scripts do not work with pre-MR1 Jelly Bean. |
| 274 | * Component builds do not work on pre-KitKat (due to the OS having a max |
| 275 | number of shared libraries). |
| 276 | * Jelly Bean and KitKat images sometimes forget to mount /sdcard :(. |
| 277 | * This causes tests to fail. |
| 278 | * To ensure it's there: `adb -s emulator-5554 shell mount` (look for /sdcard) |
| 279 | * Can often be fixed by editing `~/.android/avd/YOUR_DEVICE/config.ini`. |
| 280 | * Look for `hw.sdCard=no` and set it to `yes` |
Nate Fischer | 0dacc46 | 2021-04-06 16:10:24 | [diff] [blame] | 281 | * The "Google APIs" Android L and M emulator images are configured to expect |
| 282 | the "AOSP" WebView package (`com.android.webview`). This does not resemble |
| 283 | production devices with GMS, which expect the ["Google WebView" |
| 284 | configuration](/android_webview/docs/webview-providers.md#webview-provider-options) |
| 285 | (`com.google.android.webview` on L and M). See [Removing preinstalled |
| 286 | WebView](/android_webview/docs/build-instructions.md#Removing-preinstalled-WebView) |
| 287 | if you need to install a local build or official build. |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 288 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 289 | |
| 290 | #### Starting an Emulator from the Command Line |
| 291 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 292 | Refer to: https://developer.android.com/studio/run/emulator-commandline.html. |
| 293 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 294 | *** promo |
| 295 | Ctrl-C will gracefully close an emulator. |
| 296 | *** |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 297 | |
Nate Fischer | 3680421 | 2020-03-31 02:12:49 | [diff] [blame] | 298 | *** promo |
| 299 | **Tip:** zsh users can add https://github.com/zsh-users/zsh-completions to |
| 300 | provide tab completion for the `emulator` command line tool. |
| 301 | *** |
| 302 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 303 | #### Basic Command Line Use |
| 304 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 305 | ```shell |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 306 | $ # List virtual devices that you've created: |
| 307 | $ ~/Android/Sdk/emulator/emulator -list-avds |
| 308 | $ # Start a named device: |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 309 | $ ~/Android/Sdk/emulator/emulator @EMULATOR_ID |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 310 | ``` |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 311 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 312 | #### Running a Headless Emulator |
| 313 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 314 | You can run an emulator without creating a window on your desktop (useful for |
| 315 | `ssh`): |
| 316 | ```shell |
Nate Fischer | 9cbee243 | 2019-04-10 14:51:58 | [diff] [blame] | 317 | $ ~/Android/Sdk/emulator/emulator -no-window @EMULATOR_ID |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 318 | $ # This also works for new enough emulator builds: |
| 319 | $ ~/Android/Sdk/emulator/emulator-headless @EMULATOR_ID |
| 320 | ``` |
| 321 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 322 | #### Running Multiple Emulators |
| 323 | |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 324 | Tests are automatically sharded amongst available devices. If you run multiple |
| 325 | emulators, then running test suites becomes much faster. Refer to the |
| 326 | "Multiple AVD instances" section of these [emulator release notes]( |
| 327 | https://androidstudio.googleblog.com/2018/11/emulator-28016-stable.html) |
| 328 | for more about how this works. |
| 329 | ```shell |
Andrew Grieve | c214adb | 2019-05-28 01:39:23 | [diff] [blame] | 330 | $ # Start 8 emulators. Press Ctrl-C to stop them all. |
| 331 | $ ( 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] | 332 | $ # 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] | 333 | $ ( 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] | 334 | ``` |
| 335 | |
John Budorick | 041a7df | 2020-04-03 23:20:27 | [diff] [blame] | 336 | #### Writable system partition |
| 337 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 338 | Unlike physical devices, an emulator's `/system` partition cannot be modified by |
| 339 | default (even on rooted devices). If you need to do so (such as to remove a |
| 340 | system app), you can start your emulator like so: |
| 341 | ```shell |
| 342 | $ ~/Android/Sdk/emulator/emulator -writable-system @EMULATOR_ID |
| 343 | ``` |
| 344 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 345 | ## Using an Emulator |
| 346 | * Emulators show up just like devices via `adb devices` |
| 347 | * Device serials will look like "emulator-5554", "emulator-5556", etc. |
| 348 | |
Nate Fischer | 4a49458 | 2020-03-12 18:08:51 | [diff] [blame] | 349 | ## Emulator pros and cons |
| 350 | |
| 351 | ### Pros |
| 352 | * **Compiles are faster.** Many physical devices are arm64, whereas emulators |
| 353 | are typically x86 (32-bit). 64-bit builds may require 2 copies of the native |
| 354 | library (32-bit and 64-bit), so compiling for an arm64 phone is ~twice as |
| 355 | much work as for an emulator (for targets which support WebView). |
| 356 | * **APKs install faster.** Since emulators run on your workstation, adb can |
| 357 | push the APK onto the emulator without being [bandwidth-constrained by |
| 358 | USB](https://youtu.be/Mzop8bXZI3E). |
| 359 | * Emulators can be nice for working remotely. Physical devices usually require |
| 360 | `scp` or ssh port forwarding to copy the APK from your workstation and |
| 361 | install on a local device. Emulators run on your workstation, so there's **no |
| 362 | ssh slow-down**. |
| 363 | |
| 364 | ### Cons |
| 365 | * If you're investigating a hardware-specific bug report, you'll need a |
| 366 | physical device with the actual hardware to repro that issue. |
| 367 | * x86 emulators need a separate out directory, so building for both physical |
| 368 | devices and emulators takes up more disk space (not a problem if you build |
| 369 | exclusively for the emulator). |
| 370 | * `userdebug`/`eng` emulators don't come with the Play Store installed, so you |
| 371 | can't install third party applications. Sideloading is tricky, as not all |
| 372 | third-party apps support x86. |