Android Docker Image

Julien sez'n
4 min readJul 20, 2023

--

🐳 Dockerizing the Android Development Environment..

Docker is like magic for packaging applications along with their dependencies into containers. 🎩

✨ By using Docker, you can encapsulate the Android SDK and Fastlane setup, making it easier to share and deploy your projects across various machines.

Let’s start 🏗️ building a Docker image based on OpenJDK 17-slim, which is the official image for Java development. The image will include everything needed for building Android apps, such as the Android SDK and Fastlane.

FROM  --platform=linux/amd64 openjdk:17-slimENV ANDROID_SDK_TOOLS 9477386
ENV ANDROID_SDK_URL https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}_latest.zip
ENV ANDROID_BUILD_TOOLS_VERSION 33.0.0
ENV ANDROID_HOME /usr/local/android-sdk-linux
ENV ANDROID_VERSION 33
ENV PATH $PATH:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/cmdline-tools/bin
# Set user to root for necessary permissions
USER root
# Install required packages
RUN apt-get update && \
apt-get install -y --no-install-recommends unzip curl && \
mkdir "$ANDROID_HOME" .android && \
cd "$ANDROID_HOME" && \
curl -o sdk.zip $ANDROID_SDK_URL && \
unzip sdk.zip && \
rm sdk.zip && \
# Download Android SDK
yes | sdkmanager --licenses --sdk_root=$ANDROID_HOME && \
sdkmanager --update --sdk_root=$ANDROID_HOME && \
sdkmanager --sdk_root=$ANDROID_HOME "build-tools;${ANDROID_BUILD_TOOLS_VERSION}" \
"platforms;android-${ANDROID_VERSION}" \
"platform-tools" \
"extras;android;m2repository" \
"extras;google;m2repository" && \
# Install Fastlane
apt-get install --no-install-recommends -y --allow-unauthenticated build-essential git ruby-full && \
gem install rake && \
gem install fastlane && \
gem install bundler && \
gem install screengrab && \
# Clean up
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
apt-get autoremove -y && \
apt-get clean

This script is a Dockerfile, a set of instructions used to create a Docker image. It’s a multi-line script that sets up an environment for Android development and installs Fastlane, a tool for automating Android and iOS app deployment.

Here’s a step-by-step explanation:

  1. FROM --platform=linux/amd64 openjdk:17-slim
  • This line specifies the base image for the Docker container. It uses OpenJDK version 17 (Slim version) as the base.

2. ENV ANDROID_SDK_TOOLS 9477386

  • This sets an environment variable named ANDROID_SDK_TOOLS with the value 9477386

3. ENV ANDROID_SDK_URL https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}_latest.zip

  • This sets an environment variable named ANDROID_SDK_URL with the value containing the URL to download the Android SDK tools zip file. The value is constructed using the previously set ANDROID_SDK_TOOLS variable.

4. ENV ANDROID_BUILD_TOOLS_VERSION 33.0.0

  • This sets an environment variable named ANDROID_BUILD_TOOLS_VERSION with the value 33.0.0, specifying the version of Android Build Tools to be installed.

5. ENV ANDROID_HOME /usr/local/android-sdk-linux

  • This sets an environment variable named ANDROID_HOME with the value /usr/local/android-sdk-linux, which is the installation directory for the Android SDK.

6. ENV ANDROID_VERSION 33

  • This sets an environment variable named ANDROID_VERSION with the value 33, specifying the Android API version to be installed.

7. ENV PATH $PATH:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/cmdline-tools/bin

  • This adds the Android SDK tools and platform-tools directories to the system PATH environment variable, so that the Android command-line tools can be accessed from anywhere.

8. USER root

  • This sets the user to “root” within the Docker container, allowing the subsequent commands to be executed with root privileges.

8. RUN ... (could be one line by instruction, but with RUN you can chain them)

  • This block contains several commands that are executed during the image build process:
  • apt-get update updates the package repository metadata.
  • apt-get install -y --no-install-recommends unzip curl installs "unzip" and "curl" packages required for downloading and extracting the Android SDK.
  • mkdir "$ANDROID_HOME" .android creates the Android SDK directory and a hidden ".android" directory inside it.
  • cd "$ANDROID_HOME" changes the working directory to the Android SDK directory.
  • curl -o sdk.zip $ANDROID_SDK_URL downloads the Android SDK zip file specified in the ANDROID_SDK_URL variable.
  • unzip sdk.zip extracts the downloaded SDK zip file.
  • rm sdk.zip removes the downloaded SDK zip file after extraction.
  • yes | sdkmanager ... accepts the licenses for the Android SDK components and installs various Android SDK packages, including build tools, platforms, and additional repositories.
  • apt-get install ... installs packages required for Fastlane, such as "build-essential," "git," and "ruby-full."
  • gem install ... installs the Fastlane and other Ruby gems used for app deployment and automation.
  • The rm, apt-get autoremove, and apt-get clean commands clean up unnecessary files and free up space within the Docker image.

Overall, this Dockerfile sets up an environment suitable for Android app development, installs the Android SDK, and includes Fastlane for streamlined app deployment processes.

docker build --platform linux/x86_64 -t jdk17:android .  

I used to use jangrew image before, but with need of some more features, became to dive a bit into Docker, to use Jdk17when was supported by android, fastlane etc..

If you just need the image, you can use this one:

image: registry.gitlab.com/szndev/ci-droid:latest

image: registry.gitlab.com/szndev/ci-droid:jdk17

image: registry.gitlab.com/szndev/ci-droid:jdk20

The code is here for the jdks 11, 17, 20

https://gitlab.com/szndev/ci-droid

--

--