Creating a dev container image for Android app development

Building and developing Android apps requires some initial setup. Specifically, you need to ensure that you have the correct versions of Java, Gradle, and the Android SDK installed on your development machine. In some cases, you may also need to install Node.js if you’re building a Cordova app for example.

Fortunately, dev containers can make the setup process much easier. Dev containers are containerized environments for developing software. They integrate well with VS Code. By using a dev container, you can automate the installation of all the necessary tools and dependencies, saving you time and hassle. In the next section, we’ll walk through the steps required to set up a dev container for Android app development.

To set up our dev container, we’ll start with the universal dev container image provided by Microsoft. This image includes a wide range of useful tools and libraries for development, making it an ideal base for our needs. From there, we’ll add some custom Dockerfile snippets that install the Android SDK and other necessary components. These snippets and scripts have been sourced from the mindrunner/docker-android-sdk repository. With these pieces in place, we’ll have a fully-functional Android development environment that we can use to build and test our apps.

Let’s clone this project:

git clone https://github.com/mindrunner/docker-android-sdk.git

And let’s add a new Dockerfile:

# Use the Microsoft dev container as the base image
FROM mcr.microsoft.com/vscode/devcontainers/universal

# Set environment variables used by the Android SDK
ENV ANDROID_SDK_HOME /opt/android-sdk-linux
ENV ANDROID_SDK_ROOT /opt/android-sdk-linux
ENV ANDROID_HOME /opt/android-sdk-linux
ENV ANDROID_SDK /opt/android-sdk-linux

# Set Debian to not prompt for user input during package installation
ENV DEBIAN_FRONTEND noninteractive

# Update package list and install packages required for Android app development
RUN apt-get update -yqq && \
    apt-get install -y \
      curl \
      expect \
      git \
      make \
      wget \
      unzip \
      vim \
      openssh-client \
      locales \
      libarchive-tools && \
    apt-get clean && rm -rf /var/lib/apt/lists/* && \
    localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8

# Set the system language to US English
ENV LANG en_US.UTF-8

# Create a new group and user with UID 1001
RUN groupadd android && \
    useradd -d /opt/android-sdk-linux -g android -u 1001 android

# Copy the tools and licenses directories to the /opt directory in the image
COPY tools /opt/tools
COPY licenses /opt/licenses

# Set the working directory to /opt/android-sdk-linux and run the entrypoint script
WORKDIR /opt/android-sdk-linux
RUN /opt/tools/entrypoint.sh built-in

Now to build it docker build -t android-sdk . from the root of the repository.

This results in the following devcontainer.json:

{
  "dockerFile": "Dockerfile"
}

A devcontainer.json contains the specification for the development environment. In practice, you can configure more specific things for your project in this file. For example you can choose specific software versions here or allow access to resources. To switch Java or Gradle versions, check the documentation of SDKMAN and the features part of the dev container specification.

With this, we’re good to start developing Android apps using this environment. You could use this either locally with VS Code or remotely using Github Codespaces. It’s also great for helping new contributors to your software project get set up quicker.

To wrap up the project, the entire setup has been proposed here: mindrunner/docker-android-sdk#49