Setting up and running the Android Emulator on continuous integration (CI) or deployment (CD) is now easier than ever before with our pre-built Android Emulator Containers. These containers allow you to find and run the right version of the Emulator without the headache of dependency management, which makes it easy to scale automated tests as part of a CI/CD system without the upkeep cost of a physical device farm.
Earlier this year, we released Android Emulator download and Docker image generator scripts to help developers who were struggling to deploy and debug remote emulators. These scripts made it easier to find the right system image, manage system dependencies, and run the Android Emulator.
We are now taking this one step further and experimentally providing pre-built Android Emulator Containers for each major Emulator release. These containers remove the need to manually run the generator yourself, saving time and complexity. Don’t worry, the pre-built containers still support all of the features offered by those built with the Docker scripts, like adb and web access.
Linux KVM is required to run these containers and can be enabled by running on bare metal or on a virtual machine with nested virtualization. The correct option depends on your cloud provider, so read our documentation for recommendations.
The script below is an illustration of how you can integrate an Android Emulator Container into your system and use it to run tests.
#!/bin/bash
# This is the remote image we are going to run.
# Docker will obtain it for us if needed.
DOCKER_IMAGE=us-docker.pkg.dev/android-emulator-268719/images/r-google-x64:30.0.23
# This is the forwarding port. Higher ports are preferred as to not interfere
# with adb's ability to scan for emulators.
PORT=15555
# This will launch the container in the background.
container_id=$(docker run -d \
-e "ADBKEY=$(cat ~/.android/adbkey)" --device /dev/kvm --publish \
8554:8554/tcp --publish $PORT:5555/tcp \
$DOCKER_IMAGE)
echo "The container is running with id: $container_id"
# Note you might see something like:
# failed to connect to localhost:15555
# this merely indicates that the container is not yet ready.
echo "Connecting to forwarded adb port."
adb connect localhost:$PORT
# we basically have to wait until `docker ps` shows us as healthy.
# this can take a bit as the emulator needs to boot up!
echo "Waiting until the device is ready"
adb wait-for-device
# The device is now booting, or close to be booted
# We just wait until the sys.boot_completed property is set to 1.
while [ "`adb shell getprop sys.boot_completed | tr -d '\r' `" != "1" ] ;
do
echo "Still waiting for boot.."
sleep 1;
done
# Now you can use the emulator as usual for example:
# ./gradlew installDebug
# ./gradlew connectedAndroidTest
# etc..
echo "The device is ready"
echo "Run the following command to stop the container:"
echo "docker stop ${container_id}"
Check out our README for further information on getting started and taking advantage of the Android Emulator Containers. This is our first time offering pre-built Emulator Containers, so please report any problems or feature requests on our issue tracker.