This post was written in 2016. I wrote an updated version on 2018. Android development with Docker 2018

I’ve been using Docker for developing servers and other web applications for a few months and I find it very comfortable. When I want to work on one of my projects I just need to clone the git repository and run a Docker command and everything is ready to start developing. The environment and all dependencies are installed inside the Docker container automatically and the developer doesn’t need to worry about a thing.

Today I decided to try to expand this concept to one of my Android projects. With Android development there are a few challenges to overcome. We need to get the correct development tools to build the project as well as a way to easily install the build into a device for testing. A few people have already done a lot of work on this subject so I’m going to use as much of their work as I can.

I created a Dockerfile based on jacekmarchwicki’s android image. I removed some stuff that I didn’t need and changed the versions of some of the Android tools:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
FROM ubuntu:14.04

RUN apt-get update

# Install java7
RUN apt-get install -y software-properties-common \
    && add-apt-repository -y ppa:webupd8team/java \
    && apt-get update
RUN echo oracle-java7-installer shared/accepted-oracle-license-v1-1 \
    select true | /usr/bin/debconf-set-selections
RUN apt-get install -y oracle-java7-installer

# Install Deps
RUN dpkg --add-architecture i386 && apt-get update \
    && apt-get install -y --force-yes expect wget \
    libc6-i386 lib32stdc++6 lib32gcc1 lib32ncurses5 lib32z1

# Install Android SDK
RUN cd /opt && wget --output-document=android-sdk.tgz --quiet \
    http://dl.google.com/android/android-sdk_r24.3.3-linux.tgz \
    && tar xzf android-sdk.tgz && rm -f android-sdk.tgz \
    && chown -R root.root android-sdk-linux

# Setup environment
ENV ANDROID_HOME /opt/android-sdk-linux
ENV PATH ${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools

# Install sdk elements
COPY tools /opt/tools
ENV PATH ${PATH}:/opt/tools
RUN ["/opt/tools/android-accept-licenses.sh", \
    "android update sdk --all --force --no-ui --filter platform-tools,tools,build-tools-23,build-tools-23.0.2,android-23,addon-google_apis_x86-google-23,extra-android-support,extra-android-m2repository,extra-google-m2repository,extra-google-google_play_services,sys-img-armeabi-v7a-android-23"]

# Cleaning
RUN apt-get clean

# Go to workspace
RUN mkdir -p /opt/workspace
WORKDIR /opt/workspace

You might also want to create a .dockerignore file so you don’t have problems with files generated when running a build in the container. My file looks like this:

1
2
build/
.gradle/

Before you can use this Dockerfile you will need to download android-accept-licenses.sh, save it under the tools folder and add execute permissions.

1
2
3
4
mkdir tools
cd tools
wget https://raw.githubusercontent.com/oren/docker-ionic/master/tools/android-accept-licenses.sh
chmod +x android-accept-licenses.sh

Now you can build the image:

1
docker build -t android-docker .

This step will take some time since it will download the operating system, all the dependencies and all the Android tools.

When the command is done you can start working on your project. I recommend you open a terminal using:

1
docker run -it --privileged --volume=$(pwd):/opt/workspace android-docker bash

The privileged flag is necessary so the docker container can access Android devices connected via USB. Building and installing your app in a device connected via USB requires only this command:

1
./gradlew installDebug

By adding the Dockerfile to your project and sharing these commands your team will have a reproducible and easy to set up Android development environment.

There are a few things that can be improved, for example, having an emulator and being able to test without a hardware device. For now, this workflow is good enough for me. I will explore in the future how to create and run an emulator inside the docker container.

[ android  automation  docker  mobile  ]
Android development with Docker android automation docker mobile
Playing with Kubernetes locally with Minikube automation docker
Introduction to GitLab automation docker
Introduction to Bitbucket Pipelines automation docker
Getting Rails to run in an Alpine container automation docker