Setting up Gitlab CI for Android project

Aleksandr Shepeliev
3 min readSep 27, 2018

--

I have an old Intel® Core® i7 desktop that is laying uselessly in my storeroom. So I’ve decided to give it a second life as an Android build runner for Gitlab CI. As it turned out, it isn’t a complicated task. Sure, after you have spent all the day in order to figure it out :)

Prerequirements

Preparing Gitlab runner

We have to build Docker image with Android SDK. I’ve already done it, so you can use my image from Docker Hub, or build your own using mine as a sample: https://github.com/shepeliev/android-sdk

The docker image has preinstalled emulator with x86 system image that requires hardware acceleration. So we had to install KVM on our host machine:

sudo apt install qemu-kvm

Installing the Gitlab runner

sudo docker run --rm -it -v \
$HOME/.gitlab-runner:/etc/gitlab-runner \
gitlab/gitlab-runner:latest register

You should enter the following data:

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): 
https://gitlab.com/
Please enter the gitlab-ci token for this runner:
<copy the token from your project CI/CD settings at gitlab.com>
Please enter the gitlab-ci description for this runner:
[aa8103e11669]:
<just give a name to your runner>
Please enter the gitlab-ci tags for this runner (comma separated):
docker, android
Registering runner... succeeded runner=Qyf_E69X
Please enter the executor: virtualbox, docker-ssh, parallels, ssh, docker-ssh+machine, kubernetes, docker, shell, docker+machine:
docker
Please enter the default Docker image (e.g. ruby:2.1):
shepeliev/android-sdk

Make sure you have entered android in tags list, as we will use it later in CI config file.

After finishing, config.toml will be created in$HOME/.gitlab-runner directory. The config should be tweaked a little bit:

sudo nano $HOME/.gitlab-runner/config.toml

Then find privileged option in [runners.docker] section and set it true.

Now we can start our runner. In order to Docker container be able to use KVM for emulator acceleration we have to run it in privileged mode:

sudo docker run -v $HOME/.gitlab-runner:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
--name gitlab-runner --restart always -d --privileged \
gitlab/gitlab-runner:latest

After the runner has been started it should appear in CI/DI settings of the project:

That’s it. Let's try how it works.

Testing the runner

Add .gitlab-ci.yml into the root of the project:

unit_tests:
tags:
- android
script:
- ./gradlew test

connected_tests:
tags:
- android
script:
- /opt/start-default-emulator.sh
- ./gradlew cAT

Here we instruct Gitlab to run the build only on runners that are tagged as android.

Commit. Go to Project -> CI/CD -> Pipelines at Gitlab. It’s working!

P.S. Despite, it seems to be everything okay, I have an issue with this solution in one of my projects. I don’t know why, but Espresso UI tests fail after passing 5–7 test cases. The problem is the same as this Stack Overflow question:

https://stackoverflow.com/questions/50521487/test-instrumentation-process-crashes-when-running-in-emulator-with-no-window.

However, it has no answer yet. Feel free comment if you have any idea why it may happen.

--

--