MDXDave.de

Informatikstudent der TH Köln // Android + Coding

Laden...

GitLab Runner (CI) and Android

veröffentlicht vor 1 Jahr, am 6. Januar 2016  |  GitLab    CI    Build      |  1 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 http://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
tar xfv android*.tgz

In order to build the apps, you must download the needed SDK platforms and tools. To install ALL default plattforms (in my case 8, 10, and 15-23), use the following command:

android-sdk-linux/tools/android/ update sdk --no-ui

To filter packages you want to install, fetch a list of available packages and install them by number:

./android list sdk --all
./android update sdk -u -a -t {n}

({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 /home/gitlab-runner/.profile and add

export ANDROID_HOME=/home/gitlab-runner/android-sdk-linux
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.