Creating Custom Android Emulators for End-to-End Automation Tests

Ranvir Chhina
Tauk Blog
Published in
5 min readAug 24, 2021
A Google Pixel 4 Android Emulator

You can make an emulator using the AVD Manager UI tool, but how do you create one through the command line, or even better, automate the process to create your own pipeline? Read and find out.

The first phase of testing your Android application involves writing a test in a language of your choice. After you have written the test, you need to run it in either a real device or an emulator. In some situations, it would be useful to have an automated manner of creating android emulators and running them.

Prerequisites

Before you can go ahead create android emulators, you need the following items to be set up on your machine

  • Android and Java SDK: This article covers this item in the “Android Setup” section.
  • Update PATH: Adding tools/bin and emulator folder inside your Android SDK folder to your system's PATH variable
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/emulator

Using sdkmanager to install Android Images

An Android Image is just a version of Android that you can run on a computer. So if you wanted to test the very latest stuff you could download an Android O system image and run it on the emulator to see what Android O is like. This is its formal definition, if you are curious.

After setting up Android SDK, you will have access to this handy tool called the sdkmanager which will allow you to install or update Android images.

Sample output from “sdkmanager --help”
Sample output from “sdkmanager --help"

Using sdkmanager, we can first verify whether we have the needed image using the --list flag. The --list flag shows the already installed packages, available packages which can be downloaded, and the available updates. If not available locally, we can install the required image from the Google servers using the --install flag.

Sample output from “sdkmanager --list”

Once you decide which image you want from the “Available Packages:” section of the --list flag, you can download another image using the following command.

sdkmanager --install "system-images;android-30;google_apis_playstore;x86"

Using avdmanager to create an emulator

At this point, you either installed a system image or used a pre-installed one. Now, using this image as a blueprint we will create an emulator.

To create an Android Virtual Device, you can use the avdmanager tool exposed using the same aforementioned export command. This tool allows you to do basic emulator management like creating an emulator, moving it or deleting it.

Sample output from “avdmanager -h” and “avdmanager -h create avd”

Let’s go ahead and create an emulator and give it the name “pixel_4”. To do this, we will use create avd action and the following action options. The meanings of the action options utilized are defined above in the image.

echo "no" | avdmanager --verbose create avd --force --name "<AVD_NAME_GOES_HERE>" --package "system-images;android-30;google_apis_playstore;x86" --abi "x86"

The echo “no” is piped into the avdmanager action so that we can bypass the input prompt which comes up when you execute the action. This allows us to add this command to a bash script.

Editing the config.ini for the emulator

For every Android virtual device that is created, a directory gets created in the $HOME/.android/avd/ folder on your system. It will be named as follows <NAME>.avd. Within this directory, you will find the config.ini file.

After the create avd action finishes execution, you can further edit the config.ini file in the <NAME>.avd folder. This will allow us to configure the emulator hardware properties or startup options. If you skip this step, you will end up with a very bare bones emulator.

I will explain a few properties of this config.ini file, their meaning and examples. This documentation has a more detailed version here. Another useful template I found was at Android Source.

hw.ramSize — The amount of physical RAM on the device, in megabytes

// integer, default: 0 
hw.ramSize = 1536

hw.audioOutput— Whether the device can play audio

// boolean, default: yes 
hw.audioOutput = yes

Playstore.enabled— Whether the Google Play Store is installed on device

// boolean, default: yes 
Playstore.enabled = yes

hw.gps — Enable/Disable emulated OpenGLES GPU

// boolean, default: yes 
hw.gps = yes

hw.keyboard — Whether the device has a QWERTY keyboard

// boolean, default: yes 
hw.keyboard = yes

hw.lcd.density — A value used to roughly describe the density of the LCD screen for automatic resource/asset selection

// integer enum [120, 160, 240, 213, 320], default: 160
hw.lcd.density = 440

hw.lcd.height — LCD pixel height

// integer, default: 640 
hw.lcd.height = 2280

hw.lcd.width — LCD pixel width

// integer, default: 320 
hw.lcd.width = 1080

skin.name — Select a skin that controls what the device looks like when displayed in the emulator.

// string 
skin.name = pixel_4

skin.path — Path to the skin file. You can select another skin name from $ANDROID_HOME/skins/

// string 
skin.path = $HOME/Library/Android/sdk/skins/pixel_4

Running the AVD using emulator

Now the hard part is over. You have downloaded the blueprint, created the emulator and tweaked its properties. All that’s left is to start it.

To start the Android Virtual device, we use the following command

emulator @<AVD_NAME_GOES_HERE>

You can read more about the emulator tool here.

Packaging into a shell script

To automate the whole process, all of the commands preceding this section can be packaged into bash script and then executed before your Appium test. I have included a script below as a starting point of you custom emulator adventure.

#! /bin/zsh  
EMULATOR_NAME="pixel_4"
# Create emulator
echo "no" | avdmanager --verbose create avd --force --name "$EMULATOR_NAME" --package "system-images;android-30;google_apis_playstore;x86" --abi "x86"
# Customize emulator
TEMPLATE_PATH=$HOME/template-config.ini
CONFIG_PATH=$HOME/.android/avd/$EMULATOR_NAME.avd/config.ini
echo "" > $CONFIG_PATH
cat $TEMPLATE_PATH > $CONFIG_PATH
# Run Emulator
emulator @$EMULATOR_NAME &

Conclusion

Voila! You made it till the end. Now, you can make your own custom emulators through the command line. This opens more options for you and allows you — for example — to do automated regression testing against multiple Android API levels for your app.

--

--