Skip to content

Instantly share code, notes, and snippets.

@mrk-han
Last active May 1, 2024 21:12
Show Gist options
  • Save mrk-han/66ac1a724456cadf1c93f4218c6060ae to your computer and use it in GitHub Desktop.
Save mrk-han/66ac1a724456cadf1c93f4218c6060ae to your computer and use it in GitHub Desktop.
Installing and creating Emulators with AVDMANAGER (For Continuous Integration Server or Local Use)

Install and Create Emulators using AVDMANAGER and SDKMANAGER

TL;DR

For an emulator that mimics a Pixel 5 Device with Google APIs and ARM architecture (for an M1/M2 Macbook):

  1. List All System Images Available for Download: sdkmanager --list | grep system-images

  2. Download Image: sdkmanager --install "system-images;android-30;google_atd;arm64-v8a"

  3. Create Emulator: echo "no" | avdmanager --verbose create avd --force --name "pixel_5_api30_google_atd_emulator" --package "system-images;android-30;google_atd;arm64-v8a" --tag "google_atd" --abi "arm64-v8a" --device "pixel_5"

For M1/M2 Macbooks, use arm64-v8a as your "tag" or "target".

For Intel Macbooks use x86.

I recommend always using the new google_atd or aosp_atd images when possible. In my benchmarks, they are about 40% more efficient than the google_apis image.

Note: To check which devices you can create from the command line, run avdmanager list deviceand pass in the device name as the value of --device, e.g. --device "pixel_5" at the end of the command on step 3. (This includes tablets, etc)

Passing in a device will make the Emulator settings (usually found in the ~/.android/avd/emulator.avd/config.ini file) try to mimic that device. It is not actually the device. But, certain settings like pixel density, resolution, memory, partition size, etc will be changed. Generally the lower resolution devices will be less taxing on your CPU resources on CI, and are preferred especially without GPU/Hardware acceleration.

Continued...

For generic skin emulator with default apis (without google apis) for use with Intel Macbook on CI (Can use google_atd too):

  1. List All System Images Available for Download: sdkmanager --list | grep system-images

  2. Download Image: sdkmanager --install "system-images;android-30;aosp_atd;x86"

  3. Create Emulator: echo "no" | avdmanager --verbose create avd --force --name "generic_api30_aosp_atd_emulator" --package "system-images;android-30;aosp_atd;x86" --tag "aosp_atd" --abi "x86"

    If you do not use the --device flag, I recommend adding these lines to: ~/.android/avd/generic_10.avd/config.ini or else your emulator will have a very low resolution. 
    Note: Increasing resolution will decrease performance on CI.
    
    skin.name=1080x1920        # proper screen size for emulator
    hw.lcd.density=480
    hw.keyboard=yes            # enables keys from your laptop to be sent to the emulator
    
    If you cannot do this, you can still pass -skin 1080x1920 as an argument when starting the emulator. 
    
    Keep in mind, you can also pass the --device flag and use a device name from the avdmanager list device command which          should also set a default resolution because it will inherit the set of properties of that "device" in the config.ini.
    
  4. Run Emulator: emulator @generic_api30_aosp_atd_emulator & or emulator @pixel_5_google_atd_emulator &

Docs

See: Google's Emulator CLI Documentation for more info.

See: Google's AVDManager Documentation for more info.

See: Google's SDKManager Documentation for more info.

Aliases

Add aliases to your ~/.zshrc or ~/.bashrc to run the emulators with parameters more easily.

alias generic_api31_emulator='emulator @generic_api31_emulator -no-boot-anim -netdelay none -no-snapshot -wipe-data -skin 768x1280 &'

alias pixel_5 ='emulator @pixel_5 -no-boot-anim -netdelay none -no-snapshot -wipe-data &'

Starting multiple emulators

You can pass the -read-only parameter when starting up an emulator emulator @pixel_5 -read-only to run multiple devices at the same time.

Other

  1. aosp_atd and google_atd system images are only available on x86 and ARM architecture at API level 30.

See also: https://android-developers.googleblog.com/2021/10/whats-new-in-scalable-automated-testing.html

Output of sdkmanager --list | grep atd:

  system-images;android-30;aosp_atd;arm64-v8a                                              | 1            | AOSP ATD ARM 64 v8a System Image
  system-images;android-30;aosp_atd;x86                                                    | 1            | AOSP ATD Intel x86 Atom System Image
  system-images;android-30;google_atd;arm64-v8a                                            | 1            | Google APIs ATD ARM 64 v8a System Image
  system-images;android-30;google_atd;x86                                                  | 1            | Google APIs ATD Intel x86 Atom System Image
  1. avdmanager list device is great to figure out which emulators you can create
  2. The newest cmdline-tools located at $HOME/Library/Android/sdk/cmdline-tools/latest/bin were created for Java 9/10/11. If you are running into issues running sdkmanager, make sure to update your path in your ~/.zshrc to the tools to the new tools if you are on Java 11, e.g. export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin. If you are still on Java 8, you can use the old tools.
@justinmoon
Copy link

Helped me to just set ANDROID_AVD_HOME manually ...

@avnikolaev
Copy link

avnikolaev commented Apr 10, 2024

Hi, Guys!
I'm facing with issue when try to start emulator with created AVD.
I try to make remote development environment, because of Serverpod couldn't installed to Windows host.
The main question is - Is it really possible to start emulator from remote ssh in VSCode?
If YES - pls. help to solve my issue... I haven't any more idea. :(
Remote system is Ubuntu 23.10.1
image
INFO | Storing crashdata in: /tmp/android-serverpod/emu-crash-35.1.4.db, detection is enabled for process: 100787
INFO | Android emulator version 35.1.4.0 (build_id 11672324) (CL:N/A)
INFO | Found systemPath /home/serverpod/android/sdk/system-images/android-33-ext5/google_apis_playstore/x86_64/
INFO | Storing crashdata in: /tmp/android-serverpod/emu-crash-35.1.4.db, detection is enabled for process: 100787
INFO | Duplicate loglines will be removed, if you wish to see each individual line launch with the -log-nofilter flag.
INFO | Increasing RAM size to 3072MB
INFO | Warning: could not connect to display ((null):0, (null))
INFO | Warning: From 6.5.0, xcb-cursor0 or libxcb-cursor0 is needed to load the Qt xcb platform plugin. ((null):0, (null))
INFO | Info: Could not load the Qt platform plugin "xcb" in "/home/serverpod/android/sdk/emulator/lib64/qt/plugins" even though it was found. ((null):0, (null))
INFO | Fatal: This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: xcb, vnc, offscreen, linuxfb, minimal.
((null):0, (null))
[ERR] Fatal: This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
[ERR] Available platform plugins are: xcb, vnc, offscreen, linuxfb, minimal.
[ERR] ((null):0, (null))
[ERR] [100797:100797:20240410,180304.089230:ERROR elf_dynamic_array_reader.h:64] tag not found
[ERR] [100797:100797:20240410,180304.090202:ERROR process_memory_range.cc:75] read out of range
[ERR] [100797:100797:20240410,180304.091959:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[ERR] [100797:100797:20240410,180304.091983:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
[ERR] [100797:100797:20240410,180304.092941:ERROR process_memory_range.cc:75] read out of range
[ERR] [100797:100797:20240410,180304.092957:ERROR process_memory_range.cc:75] read out of range
[ERR] Aborted (core dumped)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment