Cursive D

The Officially Official Rants Ravings and Lunatical Blog Postings of Darren "Cursive D" OConnell

Building Android Apks Using GitLab CI


I am never content with the status quo. If there is a better way of doing something then I prefer to use that method instead of staying with what I know. Recently I feel in love with GitLab and its many offerings. One such offering is GitLab CI.
GitLab CI (or Continuous Integration) allows you to build your software using the docker image of your choosing, download the compiled version as well as test the software in question. I started asking myself if I can do it with regular desktop software, then why can’t I use it to build my android software. This is a brief overview on how to setup GitLab CI to build an apk.

Before I begin I should note that this method only builds unsigned debug apks. While this is fine for “nightly builds”, you can’t submit these apk files to Google Play (you will still need to build locally for that). Also we need to assume several things. 1) you are using git for revision control. 2) you have gitlab (either through work, your own copy or a gitlab.com account). and 3) you are using gradle for your builds (included with Android Studio)

The set up is really easy and only requires a few changes to your repo.
1) add “local.properties” to your .gitignore file (the local location of your Sdk will conflict with the location in the Docker Image used in gitlab-ci)
2 you need a .gitlab-ci.yml file with the following contents.

#Docker Image
image: openjdk:8-jdk

#Variables Need to match local
variables:
ANDROID_COMPILE_SDK: "25"
ANDROID_BUILD_TOOLS: "24.0.0"
ANDROID_SDK_TOOLS: "24.4.1"

#executes before script of job
before_script:
- apt-get --quiet update --yes
- apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
- wget --quiet --output-document=android-sdk.tgz https://dl.google.com/android/android-sdk_r${ANDROID_SDK_TOOLS}-linux.tgz
- tar --extract --gzip --file=android-sdk.tgz
- echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter android-${ANDROID_COMPILE_SDK}
- echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter platform-tools
- echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter build-tools-${ANDROID_BUILD_TOOLS}
- echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter extra-android-m2repository
- echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter extra-google-google_play_services
- echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter extra-google-m2repository
- export ANDROID_HOME=$PWD/android-sdk-linux
- export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
- mkdir $PWD/android-sdk-linux/licenses
- echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55" > "$PWD/android-sdk-linux/licenses/android-sdk-license"
- chmod +x ./gradlew

#executes after script of job
after_script:

#build stages
stages:
- build

builder:
stage: build
only:
- trunk #only execute on trunk branch
script:
- ./gradlew assembleDebug
artifacts:
paths:
- app/build/outputs/apk/

3)After that commit the changes to your gitlab repo.

To turn on the build system
1) On Gitlab go to CI/CD Pipeline and make sure “Shared Runners” is turned on
2) Go to Pipelines -> Run pipeline -> Create pipeline
This sets up the build system for future use and triggers an initial build. You can check the status of the current build by going to Pipelines and looking at the top most build (Status will say running/failed/passed)

and thats it. Every time you commit to trunk it will automatically build an apk.
For more information about GitLab go to https://about.gitlab.com/features/

(Final Notes: in the .gitlab-ci.yml file trunk refers to my master branch. you will need to change that to reflect your master branch (master, trunk, etc) otherwise it will fail to build. You can also build other branches if desired by copying everything under builder and change the new builder header to something else (builder2 for example) and change the branch name to whatever new branch you want to build (devel for example).)

Powered by w3.css Copyright Darren M. OConnell 2016-2017 All Rights Reserved