Android docker gradle-6.x build container

Marcel Lamm
Nov 7, 2020

--

So you want to compile your fancy android app within a docker container?

Can be used e.g. in a CI-pipeline to build the .apk artifacts.

I am using 2 docker container, bundled together in a sh script. Placed in a scripts/ directory.

scripts/base-build-container/Dockerfile

FROM gradle:6.7-jdk8
USER root

ENV SDK_URL="https://dl.google.com/android/repository/commandlinetools-linux-6858069_latest.zip" \
ANDROID_HOME="/usr/local/android-sdk" \
ANDROID_VERSION=30 \
ANDROID_BUILD_TOOLS_VERSION=30.0.2

WORKDIR /opt/workspace

# Download Android SDK
RUN mkdir "$ANDROID_HOME" .android \
&& cd "$ANDROID_HOME" \
&& curl -o sdk.zip $SDK_URL \
&& unzip sdk.zip \
&& rm sdk.zip

RUN mkdir "$ANDROID_HOME/licenses" || true \
&& echo "24333f8a63b6825ea9c5514f83c2829b004d1fee" > "$ANDROID_HOME/licenses/android-sdk-license"

RUN yes | $ANDROID_HOME/cmdline-tools/bin/sdkmanager --sdk_root=$ANDROID_HOME --licenses

# Install Android Build Tool and Libraries
RUN $ANDROID_HOME/cmdline-tools/bin/sdkmanager --sdk_root=$ANDROID_HOME --update
RUN $ANDROID_HOME/cmdline-tools/bin/sdkmanager --sdk_root=$ANDROID_HOME "build-tools;${ANDROID_BUILD_TOOLS_VERSION}" \
"platforms;android-${ANDROID_VERSION}" \
"platform-tools"

# Install Build Essentials
RUN apt-get update && apt-get install build-essential -y && apt-get install file -y && apt-get install apt-utils -y

RUN echo "Europe/Berlin" > /etc/timezone

scripts/build-container/Dockerfile

FROM base-build-superapp:latest

RUN mkdir SuperAppProject

ADD SuperAppProject/build.gradle SuperAppProject/build.gradle
ADD SuperAppProject/gradle.properties SuperAppProject/gradle.properties
ADD SuperAppProject/settings.gradle SuperAppProject/settings.gradle
ADD SuperAppProject/SuperApp SuperAppProject/SuperApp

scripts/build.sh

#!/bin/sh

set -e

docker build -t base-build-superapp -f ./scripts/base-build-container/Dockerfile .
docker build -t build-superapp -f ./scripts/build-container/Dockerfile .

# compile all the stuff
docker run -v $(pwd):/opt/workspace --rm busybox rm -rf /opt/workspace/out
mkdir -p ./out

docker run --rm -v $(pwd)/out:/opt/workspace/SuperAppProject/SuperApp/build/outputs -w /opt/workspace/SuperAppProject build-superapp:latest /bin/sh -c "gradle build"

--

--