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 | acbbaab | 2019-04-23 16:46:20 | [diff] [blame] | 5 | *** note |
| 6 | **Note:** apps with native code must be compiled specifically for the device |
| 7 | architecture, so make sure your copy of the app supports x86. Also, be aware the |
| 8 | Play Store may not display ARM-only applications for an x86 emulator. The steps |
| 9 | below show how to locally compile chromium-based apps for x86. |
| 10 | *** |
| 11 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 12 | ## Building for Emulation |
| 13 | You need to target the correct architecture via GN args: |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 14 | ```gn |
Nate Fischer | 16f9453 | 2019-03-27 20:51:07 | [diff] [blame] | 15 | target_cpu = "x86" # or "x64" if you have an x86_64 emulator |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 16 | ``` |
| 17 | |
| 18 | ## Creating an Emulator Image |
| 19 | By far the easiest way to set up emulator images is to use Android Studio. |
| 20 | If you don't have an [Android Studio project](android_studio.md) already, you |
| 21 | can create a blank one to be able to reach the Virtual Device Manager screen. |
| 22 | |
| 23 | Refer to: https://developer.android.com/studio/run/managing-avds.html |
| 24 | |
| 25 | Where files live: |
| 26 | * System partition images are stored within the sdk directory. |
| 27 | * Emulator configs and data partition images are stored within |
| 28 | `~/.android/avd/`. |
| 29 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 30 | ### Choosing a Skin |
| 31 | Choose a skin with a small screen for better performance (unless you care about |
| 32 | testing large screens). |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 33 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 34 | ### Choosing an Image |
| 35 | Android Studio's image labels roughly translate to the following: |
| 36 | |
Nate Fischer | acbbaab | 2019-04-23 16:46:20 | [diff] [blame] | 37 | | AVD "Target" | Virtual Device Configuration tab | GMS? | Build Properties | |
| 38 | | --- | --- | --- | --- | |
| 39 | | Google Play | "Recommended" (the default tab) | This has GMS | `user`/`release-keys` | |
| 40 | | Google APIs | "x86 Images" | This has GMS | `userdebug`/`dev-keys` | |
| 41 | | No label | "x86 Images" | AOSP image, does not have GMS | `eng`/`test-keys` | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 42 | |
| 43 | *** promo |
Nate Fischer | acbbaab | 2019-04-23 16:46:20 | [diff] [blame] | 44 | **Tip:** if you're not sure which to use, choose **Google APIs** under the **x86 |
| 45 | Images** tab in the Virtual Device Configuration wizard. |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 46 | *** |
| 47 | |
| 48 | ### Configuration |
| 49 | "Show Advanced Settings" > scroll down: |
| 50 | * Set internal storage to 4000MB (component builds are really big). |
| 51 | * Set SD card to 1000MB (our tests push a lot of files to /sdcard). |
| 52 | |
| 53 | ### Known Issues |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 54 | * Our test & installer scripts do not work with pre-MR1 Jelly Bean. |
| 55 | * Component builds do not work on pre-KitKat (due to the OS having a max |
| 56 | number of shared libraries). |
| 57 | * Jelly Bean and KitKat images sometimes forget to mount /sdcard :(. |
| 58 | * This causes tests to fail. |
| 59 | * To ensure it's there: `adb -s emulator-5554 shell mount` (look for /sdcard) |
| 60 | * Can often be fixed by editing `~/.android/avd/YOUR_DEVICE/config.ini`. |
| 61 | * Look for `hw.sdCard=no` and set it to `yes` |
| 62 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 63 | ## Starting an Emulator from the Command Line |
| 64 | Refer to: https://developer.android.com/studio/run/emulator-commandline.html. |
| 65 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 66 | *** promo |
| 67 | Ctrl-C will gracefully close an emulator. |
| 68 | *** |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 69 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 70 | ### Basic Command Line Use |
| 71 | ```shell |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 72 | $ # List virtual devices that you've created: |
| 73 | $ ~/Android/Sdk/emulator/emulator -list-avds |
| 74 | $ # Start a named device: |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 75 | $ ~/Android/Sdk/emulator/emulator @EMULATOR_ID |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 76 | ``` |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 77 | |
| 78 | ### Running a Headless Emulator |
| 79 | You can run an emulator without creating a window on your desktop (useful for |
| 80 | `ssh`): |
| 81 | ```shell |
Nate Fischer | 9cbee243 | 2019-04-10 14:51:58 | [diff] [blame] | 82 | $ ~/Android/Sdk/emulator/emulator -no-window @EMULATOR_ID |
Andrew Grieve | c8f2703d | 2019-05-22 20:04:44 | [diff] [blame] | 83 | $ # This also works for new enough emulator builds: |
| 84 | $ ~/Android/Sdk/emulator/emulator-headless @EMULATOR_ID |
| 85 | ``` |
| 86 | |
| 87 | ### Running Multiple Emulators |
| 88 | Tests are automatically sharded amongst available devices. If you run multiple |
| 89 | emulators, then running test suites becomes much faster. Refer to the |
| 90 | "Multiple AVD instances" section of these [emulator release notes]( |
| 91 | https://androidstudio.googleblog.com/2018/11/emulator-28016-stable.html) |
| 92 | for more about how this works. |
| 93 | ```shell |
Andrew Grieve | c214adb | 2019-05-28 01:39:23 | [diff] [blame] | 94 | $ # Start 8 emulators. Press Ctrl-C to stop them all. |
| 95 | $ ( 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^] | 96 | $ # 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] | 97 | $ ( 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] | 98 | ``` |
| 99 | |
| 100 | ### Writable system partition |
| 101 | Unlike physical devices, an emulator's `/system` partition cannot be modified by |
| 102 | default (even on rooted devices). If you need to do so (such as to remove a |
| 103 | system app), you can start your emulator like so: |
| 104 | ```shell |
| 105 | $ ~/Android/Sdk/emulator/emulator -writable-system @EMULATOR_ID |
| 106 | ``` |
| 107 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 108 | ## Using an Emulator |
| 109 | * Emulators show up just like devices via `adb devices` |
| 110 | * Device serials will look like "emulator-5554", "emulator-5556", etc. |
| 111 | |