Reading time: 2 minutes, 47 seconds

Building Continous Integration for an Android App with GitLab

Continous Integration is the next big thing, but it's very difficult to find a good tutorial on how to enable CI on Android Apps.
The purpose of this article is to give a quick overview on how to combine the power of docker and gitlab runners.

The Docker Setup

We want to start with the Docker-Container in which in the android app will be built.
Because we want to build a java project, we start with the openjdk image.

FROM openjdk:8-jdk

Next we define which SDKs and Build Tools, we want to use. This Step is really important because this differs for each android project.

Make sure to modify the SDK version to your needs

ENV ANDROID_TARGET_SDK="27" \
    ANDROID_BUILD_TOOLS="26.0.2"

Afterwards we install some tools to download and unzip the sdkmanager for linux

RUN apt-get --quiet update --yes
RUN apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1

RUN wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip && \
    unzip android-sdk.zip -d android-sdk-linux/

Now, that we have the sdkmanager sucessfully installed, we can download the sdks.

We also download the google services. You can remove them if you want.

RUN echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-${ANDROID_TARGET_SDK}"
RUN echo y | android-sdk-linux/tools/bin/sdkmanager "tools"
RUN echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}"

RUN echo y | android-sdk-linux/tools/bin/sdkmanager "extras;android;m2repository"
RUN echo y | android-sdk-linux/tools/bin/sdkmanager "extras;google;google_play_services"
RUN echo y | android-sdk-linux/tools/bin/sdkmanager "extras;google;m2repository"

In the last step we add the ANDROID_HOME environment variable. This makes it easy to use in the following build.

ENV ANDROID_HOME $PWD/android-sdk-linux

Hoorray! We are finished!

This is the complete Dockerfile

FROM openjdk:8-jdk
MAINTAINER Florian Baader <[email protected]>

ENV ANDROID_TARGET_SDK="27" \
    ANDROID_BUILD_TOOLS="26.0.2"

RUN apt-get --quiet update --yes
RUN apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1

RUN wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip && \
    unzip android-sdk.zip -d android-sdk-linux/

RUN echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-${ANDROID_TARGET_SDK}"
RUN echo y | android-sdk-linux/tools/bin/sdkmanager "tools"
RUN echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}"

RUN echo y | android-sdk-linux/tools/bin/sdkmanager "extras;android;m2repository"
RUN echo y | android-sdk-linux/tools/bin/sdkmanager "extras;google;google_play_services"
RUN echo y | android-sdk-linux/tools/bin/sdkmanager "extras;google;m2repository"

ENV ANDROID_HOME $PWD/android-sdk-linux

The build Setup

After defining our build environment, how can we build an android app inside of the container?

If you are familiar with gitlab-ci files you can just take a look at the file.

image: selectcode/docker-android-sdks

stages:
  - build
  - test
  - deploy

before_script:
  - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
  - export GRADLE_USER_HOME=`pwd`/.gradle
  - chmod +x ./gradlew
  - echo y | $ANDROID_HOME/tools/bin/sdkmanager --update

cache:
  paths:
    - .gradle/caches
    - app/build/

build:
  stage: build
  script:
  - ./gradlew clean assembleRelease
  artifacts:
    paths:
    - app/build/outputs
  tags:
   - ubuntu

unitTests:
  stage: test
  script:
    - ./gradlew test
  tags:
    - ubuntu

deployToPlayStore:
  stage: deploy
  script:
    - ./gradlew publishApkRelease
  tags:
    - ubuntu
  only:
   - master

Nächster Beitrag