MDXDave.de

IT-Freelancer // Android + Coding

Laden...

GitLab Runner (CI) and Android

veröffentlicht vor 8 Jahren, am 6. Januar 2016  |  aktualisiert vor 5 Jahren  |  GitLab    CI    Build      |  0 Kommentare

1. GitLab Runner

GitLab is a nice tool to manage your git projects. It is like Github, but you can install the Community Edition (CE) version on your own server. GitLab can build your projects automatically by using so-called "runners". In this short tutorial I want to show you, how you can use GitLab runners with the Android SDK to build your Android projects automatically on your server.

2. Install and register gitlab-runner

First you should install gitlab-runner on your server if you have not done it already. Install them how shown in the documentation from GitLab. Afterwards register the runner with GitLab. Navigate to the runners section in the GitLab administration panel and copy the token. Afterwards enter the following command:

sudo gitlab-ci-multi-runner register
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci )
https:///ci
Please enter the gitlab-ci token for this runner
Please enter the gitlab-ci description for this runner
Android
Please enter the gitlab-ci tags for this runner (comma separated):
android
INFO[0012] xxx Registering runner... succeeded
Please enter the executor: shell, parallels, docker, docker-ssh, ssh:
shell
INFO[0014] Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

3. Android SDK

To build Android projects on your server, you need to install the Android SDK. At first install the necessary dependencies:

apt-get install wget tar unzip default-jdk default-jre lib32stdc++6 lib32z1

Afterwards download the current Android SDK (you can check the Android developer's website for new version) and extract it:

mkdir -p /home/gitlab-runner && cd /home/gitlab-runner
wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
unzip sdk-tools-linux-4333796.zip

In order to build the apps, you must download the needed SDK platforms and tools. To filter packages you want to install, fetch a list of available packages and install them by the package name (for example build-tools 28.0.3:

tools/bin/sdkmanager --list
tools/bin/sdkmanager --install "build-tools;28.0.3"

({n} can be replaced by multiple numbers - comma seperated)

4. Preparing

If you have multiple projects with the same gradle version, we can use a shared folder for them. Create a new folder and set user and group to gitlab-runner:

mkdir -p /home/gitlab-runner/gradle
chown gitlab-runner:gitlab-runner /home/gitlab-runner/gradle

Edit /etc/profile and add

export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
export ANDROID_SDK_ROOT=/home/gitlab-runner
export GRADLE_USER_HOME=/home/gitlab-runner/gradle

So we don't need to define this variables per project in gitlab.

5. Your project

To automatically build the project by the server, we need to make some small changes.

Create a file with the name ``.gitlab-ci.yml`` in the root folder of your project with following content:

before_script:
 - chmod +x gradlew
build:
 script:
 - ./gradlew build
 tags:
 - android

Additionally you should add following lines to your ``build.gradle`` files of your modules, to avoid abort due to Lint errors:

android {
 lintOptions {
 abortOnError false
 }
}

6. Commit and push

Commit your changes and push them. The runner should now automatically build the project. More information about gitlab continous integration are available here.