At TuneIn, we use Docker containers to run Android Continuous Integration(CI) tasks. Docker shortens the CI feedback loop and also eliminates the need for build machine maintenance. However, things weren’t always this way. About two years ago when I started working at TuneIn our CI build machine was a Windows machine using TeamCity. One of my first projects was to migrate our TeamCity build jobs from a Windows machine to use MacOS agents, we successfully migrated all of our CI jobs to use the new MacOS agents. As the team grew we had more pull requests and commits which meant we had more Android jobs running in parallel. Our TeamCity jobs started to queue since we had two MacOS agents to build all of our code changes. To solve this issue we migrated our CI from MacOS agents to use Docker containers. Our Docker Android build containers run on Amazon EC2 instances. Now we have fewer queued jobs since we can have many Android Docker builds running in parallel. Below are some of the challenges we had before migrating to Docker.

1. No machine maintenance

Two years ago building the TuneIn Android application with Windows Machine.

One year ago building the TuneIn Android application with MacOS Machine.

Now building the TuneIn Android application with Docker.

Problem one: Before we had to manually update our build environments. This includes the OS version, security updates patches, and new software releases.

Solution: By using Docker containers we don’t need to maintain build machines, we only need to update our Dockerfile to install Android SDK components and Gradle.

2. Faster CI feedback

Problem two: Continuous Integration feedback took a long time. Before Docker, we had two MacOS agents to run all our Android build jobs. As the team grew our CI feedback time increased and we started to see our TeamCity jobs to queue(look at the image below).

Solution: Now with Docker, we can have many containers running in parallel in AWS. Using Docker we get faster CI feedback for pull requests updates. Currently our pull requests CI feedback time is ~15mins. On each PR we run: Unit tests, build APKs, Android lint and run UI acceptance tests.

TeamCity dashboard: Android queue jobs

Building the TuneIn Android app with Docker

What is Docker? Docker is a container-based virtualization platform similar to virtual machines. If you want to learn more about Docker check out their great documentation, try running docker hello-world. Docker is becoming more popular with CI companies such as CircleCI and Bitrise.

How to build your Android app using Docker

I created a Dockerfile that builds Android projects using Gradle 4.10 and targeting Android version 28. Next, we are going to pull Docker image, build an Android APK, run unit tests, Android lint and any Gradle task inside a Docker container.

1. Pull Android Docker image from Docker Hub. (andres2014 is my DockerHub username)

$docker pull <imageName>

$docker pull andres2014/android-build:part1

2. Now that we pulled the Docker image we are going to run container locally. Go to your Android App directory and run below Docker commands:

Run Gradle clean with the command:

$docker run --rm -v "$PWD":/home/gradle/ -w /home/gradle/MyApp andres2014/android-build:part1 gradle clean

Run Gradle lint with the command:

$docker run --rm -v "$PWD":/home/gradle/ -w /home/gradle/MyApp andres2014/android-build:part1 gradle lint

Run Gradle build with the command:

$docker run --rm -v "$PWD":/home/gradle/ -w /home/gradle/MyApp andres2014/android-build:part1 gradle assembleGoogleFlavorMyAppNameFreeArmv7aDebug

You can change the end of the Docker command and add the Gradle tasks that you want to run.