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 | |
| 5 | ## Building for Emulation |
| 6 | You need to target the correct architecture via GN args: |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 7 | ```gn |
Nate Fischer | 16f9453 | 2019-03-27 20:51:07 | [diff] [blame^] | 8 | target_cpu = "x86" # or "x64" if you have an x86_64 emulator |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 9 | ``` |
| 10 | |
| 11 | ## Creating an Emulator Image |
| 12 | By far the easiest way to set up emulator images is to use Android Studio. |
| 13 | If you don't have an [Android Studio project](android_studio.md) already, you |
| 14 | can create a blank one to be able to reach the Virtual Device Manager screen. |
| 15 | |
| 16 | Refer to: https://developer.android.com/studio/run/managing-avds.html |
| 17 | |
| 18 | Where files live: |
| 19 | * System partition images are stored within the sdk directory. |
| 20 | * Emulator configs and data partition images are stored within |
| 21 | `~/.android/avd/`. |
| 22 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 23 | ### Choosing a Skin |
| 24 | Choose a skin with a small screen for better performance (unless you care about |
| 25 | testing large screens). |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 26 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 27 | ### Choosing an Image |
| 28 | Android Studio's image labels roughly translate to the following: |
| 29 | |
| 30 | | AVD "Target" | GMS? | Build Properties | |
| 31 | | --- | --- | --- | |
| 32 | | Google Play | This has GMS | `user`/`release-keys` | |
| 33 | | Google APIs | This has GMS | `userdebug`/`dev-keys` | |
| 34 | | No label | AOSP image, does not have GMS | `eng`/`test-keys` | |
| 35 | |
| 36 | *** promo |
| 37 | If you're not sure which to use, **choose Google APIs**. |
| 38 | *** |
| 39 | |
| 40 | ### Configuration |
| 41 | "Show Advanced Settings" > scroll down: |
| 42 | * Set internal storage to 4000MB (component builds are really big). |
| 43 | * Set SD card to 1000MB (our tests push a lot of files to /sdcard). |
| 44 | |
| 45 | ### Known Issues |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 46 | * Our test & installer scripts do not work with pre-MR1 Jelly Bean. |
| 47 | * Component builds do not work on pre-KitKat (due to the OS having a max |
| 48 | number of shared libraries). |
| 49 | * Jelly Bean and KitKat images sometimes forget to mount /sdcard :(. |
| 50 | * This causes tests to fail. |
| 51 | * To ensure it's there: `adb -s emulator-5554 shell mount` (look for /sdcard) |
| 52 | * Can often be fixed by editing `~/.android/avd/YOUR_DEVICE/config.ini`. |
| 53 | * Look for `hw.sdCard=no` and set it to `yes` |
| 54 | |
Andrew Grieve | a0190fd | 2018-06-15 17:52:49 | [diff] [blame] | 55 | ### Cloning an Image |
| 56 | Running tests on two emulators is twice as fast as running on one. Rather |
| 57 | than use the UI to create additional avds, you can clone an existing one via: |
| 58 | |
| 59 | ```shell |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 60 | $ tools/android/emulator/clone_avd.py \ |
Andrew Grieve | a0190fd | 2018-06-15 17:52:49 | [diff] [blame] | 61 | --source-ini ~/.android/avd/EMULATOR_ID.ini \ |
| 62 | --dest-ini ~/.android/avd/EMULATOR_ID_CLONED.ini \ |
| 63 | --display-name "Cloned Emulator" |
| 64 | ``` |
| 65 | |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 66 | ## Starting an Emulator from the Command Line |
| 67 | Refer to: https://developer.android.com/studio/run/emulator-commandline.html. |
| 68 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 69 | *** promo |
| 70 | Ctrl-C will gracefully close an emulator. |
| 71 | *** |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 72 | |
Nate Fischer | 7cbdeae | 2019-01-24 20:29:15 | [diff] [blame] | 73 | ### Basic Command Line Use |
| 74 | ```shell |
| 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 |
| 82 | $ sudo apt-get install xvfb-run |
| 83 | $ xvfb-run ~/Android/Sdk/emulator/emulator -gpu off @EMULATOR_ID |
| 84 | ``` |
| 85 | |
| 86 | ### Writable system partition |
| 87 | Unlike physical devices, an emulator's `/system` partition cannot be modified by |
| 88 | default (even on rooted devices). If you need to do so (such as to remove a |
| 89 | system app), you can start your emulator like so: |
| 90 | ```shell |
| 91 | $ ~/Android/Sdk/emulator/emulator -writable-system @EMULATOR_ID |
| 92 | ``` |
| 93 | |
| 94 | ### Remote Desktop |
| 95 | For better graphics performance, use virtualgl (Googlers, see |
| 96 | http://go/virtualgl): |
| 97 | ```shell |
| 98 | $ vglrun ~/Android/Sdk/emulator/emulator @EMULATOR_ID |
Andrew Grieve | ae094e39 | 2018-06-15 16:10:22 | [diff] [blame] | 99 | ``` |
| 100 | |
| 101 | ## Using an Emulator |
| 102 | * Emulators show up just like devices via `adb devices` |
| 103 | * Device serials will look like "emulator-5554", "emulator-5556", etc. |
| 104 | |