Deploying Android Emulators on AWS EC2 [1/3] | ARM Architecture and Genymotion | Solutions for a Solo Developer

Uddeshya Singh
Nov 10, 2020 · 8 min read

Exploring the use cases where android emulators come in handy, how to set them up on the EC2 instances, what variants you can run on cheap t2 instances, and what a solo developer should expect when planning to use AWS instances for cloud emulator deployment.

Image for post
Image for post
Photo by Gilles Lambert on Unsplash

Disclaimer: The author of this blog post documents his opinions and experiences towards android ROM and kernel in particular, anything other than that like android app development and gradle builds are not his forte and any pieces of advice will not be found under this post. Thank you for the patience, Read on!

Psst: This post is also available with cod embeddings on dev.to!

This post is a part of the series Deploying Android Emulators on AWS EC2.

Introduction and Use Cases 😌

  • Thorough testing of apps via background pipeline while not consuming your system’s resources and you can carry on with your development.
  • Testing of applications on android smartphones that you do not physically possess.
  • Logging and Monitoring the behavior of your firebase event tags on multiple devices at once.
  • CI/CD pipeline loves an android emulator and all your unit tests would pass through them.

What is an Android Emulator? 🥧

Taking the definition directly out of Android’s book, we can define the Android Emulator as a tool that simulates Android devices on your computer so that you can test your application on a variety of devices and Android API levels without needing to have each physical device.

The emulator provides almost all of the capabilities of a real Android device. You can simulate incoming phone calls and text messages, specify the location of the device, simulate different network speeds, simulate rotation and other hardware sensors, access the Google Play Store, and much more.

Setting Up the Emulator Environment 🧑🏾‍💻

I’ll be using a classic t2.medium instance (Ubuntu 18.04, 64 bit, x86 arch) for our purpose, with adding 25 GB storage to the instance (believe me, classic 8GB won’t cut it), make sure you have the authentication (.pem) key for logging into your instance and let it bootup!

One important thing to note about this entire usage is that you can’t access your emulator using GUI interfaces (actually, you can but that’s something I have planned for some later post :P ), so we need to make sure that anything and everything we do is done solely by CLI (aka, our good old terminal!)

Now, once you have logged into your t2.medium instance, you’ll need to clone this repository: Emulator-Environment-Setup into your instance. This will contain your command-line tools zip file. (currently hosted version by android doesn’t work for me :/)

Now follow the commands step by step.

$ sudo apt update && sudo apt install default-jdk unzip -y
$ cd Emulator-Environment-Setup
$ unzip commandlinetools-linux-6609375_latest.zip
$ mkdir ../cmdline-tools
$ mv tools ../cmdline-tools

By now you have taken care of the basics, now we need to set the path variables (which would save us the pain of writing the entire path every time we want to call sdkmanager or emulator etc.)

$ nano ~/.bashrc

Now make sure your bashrc file looks like

Image for post
Image for post
bashrc file

Save this file (Ctrl + S) and get out (Ctrl + X)

Now, load the preferences and you are good to use the SDK tools, and accept all the licenses by typing y .

$ source ~/.bashrc
$ sdkmanager --licenses

You shall witness a screen like this!

Image for post
Image for post

Now, we just need to set up the emulator, platform-tools and we can create our own Android Virtual Device!

$ sdkmanager "emulator"
$ sdkmanager "platform-tools"
$ mkdir platforms

With this, your basic environment setup is done! Now let’s take care of creating a virtual device.

Android Virtual Device on AWS 😁

$ sdkmanager --list | grep system-images

You will find multiple system images tailored to multiple devices, with a general format of

“system-images;<version>;<gapi_setting>;<emulator_architecture>”

Take a look at the given configurations, they are all of android-25 (Nougat) and come in variants of android_tv, default, google_apis etc, with 2 architectures namely ARM and Intel’s x86 which further bifurcates into 32 bit and 64-bit variants.

Image for post
Image for post

But we will specifically be using system-images;android-25;google_apis;armeabi-v7a version. Now let me justify this choice.

Why android-25? 🤔

Why the google_api version? 🙇🏽‍♂️

Why armeabi-v7a architecture? 💁🏾‍♂️️

$ sudo apt install cpu-checker -y
$ kvm-ok

This would greet you with the following message.

INFO: Your CPU does not support KVM extensions
INFO: For more detailed results, you should run this as root
HINT: sudo /usr/sbin/kvm-ok

Why do intel emulators not work in these EC2 instances? 😭

Intel’s HAXM (Hardware Acceleration Execution Manager) is a hypervisor component for Windows and macOS. There’s KVM (Kernel-based Virtual Machine) for Linux. With hardware-acceleration, the Android emulator can run virtual devices at speeds similar to that of your workstation CPU.

Image for post
Image for post
2 layer virtualization

When we choose system-images;android-25;default;x86 we are trying to run a virtual machine within a virtual machine. It is a second level or nested virtualization that we are trying to achieve which, unfortunately, is not yet supported. The Intel hardware supports only a single level of hardware-assisted virtualization, adding support for efficient (i.e., not painfully slow) nesting requires a lot of clever software engineering in hypervisors.

Run a Testing Device 🏃🏽‍♂️

$ sdkmanager "system-images;android-25;google_apis;armeabi-v7a"
$ echo "no" | avdmanager create avd --name "testDevice" -k "system-images;android-25;google_apis;armeabi-v7a"

So, you just created an AVD, how to check that out? You can use avdmanager to check your virtual android devices.

$ avdmanager list avd
Image for post
Image for post
virtual devices

Now, let’s run this virtual device!

$ emulator -ports 5554,5555 -avd testDevice -writable-system -no-window -no-audio -gpu swiftshader_indirect -show-kernel &

-ports 5554,5555 define the connection ports on which the android debug bridge will connect to this emulator (in this case, 5554) and they have to be consecutive!

-writable-system would give you access to modify any file or push any file as you wish in root settings.

-no-window would simply run the emulator headless (without this flag, it won’t work because we don’t have any OpenGL based graphics setup on a CLI)

-no-audio would take away it’s audio support (something I’d suggest you do)

-gpu swiftshader_indirect to prevent a boot loop.

-show-kernel something for you to maintain your sanity and check the kernel’s progress as it boots up because it WILL take forever (60–90 minutes) to boot up properly.

These are the testimonials on why you should not go for something this slow because this would be counterproductive, you can rather test on your local devices rather on a device which will take 1 hour to function properly and still give you an average of 2.5 minutes to install a normal APK!

Why Genymotion and how good is it? 📱

Genymotion sample

Genymotion is a go-to emulator for many organizations still and the amazing support and speed which their emulators provide are good enough for the purpose we want to solve, if and only if, your pocket allows you that much freedom.

But, still, $0.7/hr per emulator is a bit too costly, at least if you wanna do testing on a large scale.

Closing thoughts and what to expect from the upcoming post? 👨🏽‍🏫

In the next one, we’ll explore how to set up multiple emulators in a single instance and explore the android debug bridge in depth while customizing our device and run simple applications with intel emulators (which are at least 10x faster than these ones)!

References and Links🔗

Project Heuristics

Un assortiment incroyable

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store