Skip to content

Instantly share code, notes, and snippets.

@WestonThayer
Last active September 9, 2021 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WestonThayer/75a5098c8d38cb51a3a44ffe55a1e23c to your computer and use it in GitHub Desktop.
Save WestonThayer/75a5098c8d38cb51a3a44ffe55a1e23c to your computer and use it in GitHub Desktop.
Notes on running Android emulators in the cloud for testing TalkBack

Twitter thread for context: https://twitter.com/__grunet/status/1434197313795330049

Q: Is it possible to run the Android Emulator in the cloud?

A: Yes, either in a bare metal cloud or with nested virtualization.

Q: Is it easy to set up?

A: Sort of, I'm sure a lot of it could be scripted. https://github.com/budtmo/docker-android has put a lot of work into making it fast once you have a Linux machine ready (I didn't test it). With nested virtualization, you could do these steps once and take a VM image.

Q: Is it cost-efficient?

A: As long as you shut down instances when not using them, you should have low spend, but depends on your cloud provider's pricing & instance size/features.

Q: How's the perf?

A: Lot's of pitfalls, but workable.

Here are the rough steps to follow:

1. Find a cloud provider

AWS, Google Cloud Platform, Azure, you name it. AWS has "metal" instances which enable you to avoid nested virtualization. Google Cloud supports nested virtualization.

Many have free trials. Create an account with one of them and make sure you can create VMs.

2. Install Linux on a cloud machine

I like Ubuntu, but likely any Debian flavor would work. If you're spinning up a VM, make sure the instance type supports nested virtualization or you add the necessary flag when creating it (otherwise you'll have to recreate the VM to set the flag).

As for machine type, I'd suggest 4vcpus (2 cores) and 8+GB RAM. Use a fast disk, like SSD with at least 100GB on it (Android Studio, emulator, and images all take a ton of room).

What about a GPU? When you use the Android Emulator on your Mac, graphics acceleration is enabled, which probably helps a ton (as compared to all rendering operations performed on the CPU). Many cloud providers allow adding a GPU, but they're expensive. I haven't tested whether the Android Emulator can use the GPU in a VM.

3. Configure Linux for remote access

Cloud machines run the server flavors of Linux OSs, so theres no desktop to remotely connect to at first. You can add one, like ubuntu-desktop. And you'll need a remote access solution as well. I recommend NoMachine since it's free, very fast, and supports audio out of the box. This tutorial covers it for GCP: https://www.youtube.com/watch?v=UYLA_fFdciA

For NoMachine, you'll need to allow port 4000 through your cloud network's firewall to the machine.

4. Install Android Studio

I'll provide commands that summerize the Android Studio guide:

# Enable x86 libs in package management
sudo dpkg --add-architecture i386
# Update to get the x86 package availability
sudo apt-get update
# https://developer.android.com/studio/install#64bit-libs
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 libbz2-1.0:i386

# If you're running a VM (that you enabled nested virtualization
# for of course), you *need* to add KVM so that you can leverage
# https://developer.android.com/studio/run/emulator-acceleration#vm-linux
# Without, the emulator will run with ARM emulation, dirt slow

# Get kvm-ok, a little utility just to double check you enabled
# nested virtualization. Should output "KVM acceleration can be used"
sudo apt-get install cpu-checker
kvm-ok

# Now actually install KVM's libs
sudo apt-get install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils
sudo adduser <your username> kvm
sudo reboot

# After reboot, ready to finally install Studio
wget https://...studio-url.tar.gz
tar -xf ./android-studio...tar.gz
cd android-studio
bin/studio.sh

Finally, Studio will start it's GUI and go through normal onboarding.

5. Create an AVD

This can be done using Studio's Tools > AVD Manager GUI. In general they should all work, but here are some considerations:

  • The official hardware profiles (e.x. Pixel 2) are slow. I think this is because they try to cap the amount of RAM available to the emulator to match the actual device, and they are pixel-dense. Without a GPU, more pixels means slower rendering
  • If you're creating a custom profile, suggest giving it at least 6GB of RAM, 4GB in the JVM heap, and plenty of on-device storage (the default is only 800MB). Try halving the display size of a Pixel 2 (540x960)
  • Unfortunately custom hardware profiles aren't licensed for Google Play, which means you have to get the TalkBack APK or find an alternative way to install Google Apps/Google Play
  • Turning off animations in Settings > Accessibility will speed things up as well

Note that the Android Emulator will display a modal when started under nested virtualization, warning that it makes no promises on performance. You can ignore it.

Alternatives to consider

The Android Emulator isn't the only option. There's also:

  • Android Cuttlefish - also an emulator, "design to run on Google Compute Engine". Need to build from AOSP source
  • Genymotion - proprietary Anrdoid images that can be run in any cloud provider, or directly from Genymotion as SaaS. It doesn't appear to be virtualized, so nested virtualization isn't required. Their WebRTC viewer supports audio and they help you install the Play Store via https://opengapps.org/, which means you can add TalkBack. Touch emulation is natively supported, you can perform basic 2 finger gestures. Similar to https://www.bluestacks.com/, but less focused on gaming
  • Anbox - runs Android apps natively on Linux. I wasn't able to get it working. Potential downsides include: old Android version, unknown if system-level add-on apps like TalkBack work, no touch emulation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment