A Beginners Guide

Docker for Android Developers

How to set up an Android build and test environment in just a few minutes

Amit Gupta
VMware 360

--

As a mobile application developer that specializes on the Android platform for VMware’s End-User Computing team, it can sometimes be a challenge to manage all the dependencies, packages, versions. When a new employee joins the organization, it simply takes a great deal of time to understand, set up and build a test environment. Time becomes even more constrained, especially when it comes to preparing multiple machines for CI environments and then maintaining those environments.

For example, developers are concerned about maintaining:

  • Android SDK
  • Java/Kotlin
  • Gradle
  • Git
  • Emulator and devices
  • Long wait time for builds in build pipeline
  • Reproducible build environments

On the other hand, release or DevOps engineers might be more concerned about:

  • Git
  • Bamboo/Jenkins
  • VM environments
  • Emulator and Devices
  • Keeping build pipelines functional

So, how can Docker help address these issues to minimize feedback time, and increase stability?

What is Docker and why use it for Android Application Development?

Docker is an open source software platform for building applications based on containers — small and lightweight execution environments that make shared use of operating system kernel, but otherwise run in isolation from one another.

Docker can eliminate the need for build machine setup and maintenance. It helps speed up feedback time in both build and test phases of the application, reduces the build queue time and simplifies creating standard build and test environments.

Docker is based on client-server architecture. As you can see below, the Docker server is responsible for all the container actions. The Docker server receives commands from the Docker client.

Fig 1: How does Docker work?

Containers are built from images, which are the basic building blocks of Docker. In this example, we are using an image from “mingc/android-build-box”. This image is built for Android SDK and is used as a template for creating a container. Each image is organized in a layered fashion. Every change in the image will be added as a layer on top of it. Using command docker pullyou can get an image from the public Docker Hub registry. But, if you decide to build your own Android SDK image then the docker buildcommand will create an image which can be pushed to the docker registry using docker push.

Docker Registry is a centralized repository of Docker images. The public-facing Docker image repository is called Docker Hub (Docker Hub). Your enterprise might also have a private registry in Docker Hub or internally. The Docker registry acts like Git for images, where you can build images from your development machine, commit and push to the Docker registry.

Containers are the execution environment for Docker. Containers are created from images. Starting with a base image, you can add more layers in an existing image to suit your needs. We will be building our own application on top of an existing Docker image.

Using the two steps below, you can start building your Android application using containers.

STEP 1: Installations

What do you need to start with Docker:

  • Docker Desktop (Download HERE)
  • A Sample Android app. (Bring your own, or use one from HERE, This is a sample note application)

STEP 2: Building and Running

There are many pre-existing Android SDK images on Docker Hub. For this exercise, we will be using the Android Build Box. However, you can use any other existing Android SDK image from Docker Hub, or build your own image.

After installing Docker Desktop and navigating inside your project, run the following command in the terminal:

docker run --rm -v `pwd`:/project mingc/android-build-box bash -c 'cd /project; ./gradlew build'docker run --rm -v `pwd`:/project mingc/android-build-box bash -c 'cd /project; ./gradlew test'

In this command, we are using mingc/android-build-box image, which will be downloaded from Docker Hub on the first command execution. This image contains all necessary dependencies, tools to build and test an Android application.

Finally, we can map our project to the container images, launch bash and execute two commands.

  1. cd /project to go to the project directory on the container
  2. ./gradlew test to start build and running tests of the application.

NOTE: Android Build Box image does not contain Git or Gradle, but since all Android projects are built with Gradle Wrapper, Gradle will be downloaded at run time while building the application.

Coming soon

Look out for my upcoming posts, where I plan to cover more topics to help you run Android applications, build and test entirely on Docker, including:

  • How to build your own Docker Android SDK image for your project
  • How to build and test android application in build pipeline using Docker
  • How to create Android Emulator and Device Lab using Docker and Run UI tests.

References:

--

--

Amit Gupta
VMware 360

Entrepreneur, Mobile App Developer/Architect, Automation Architect, DevOps Engineer and Photographer.