Android gradle on gitlab-ci with Sonar

Posted by: on Sep 30, 2014 | No Comments

Backstory
Gradle has a bug where the plugin sonar-runner does not work properly. There is a fix in the nightly builds (2.1-rc1) which fixes this issue. However, the plugin android studio uses is way older than 2.1-rc1. And it’s not possible to use the newer gradle binary when building in android studio or outside.
For version control I use gitlab and for continous integration testing I use gitlab-ci. Gitlab-ci uses runners that are decoupled from the server that hosts the code, which for security reasons is great.

So this post is a guide in how to install a gitlab-ci runner with a sonar runner. Tested and working on Ubuntu 14.04.

Install dependencies
sudo apt-get update -y
sudo apt-get install -y wget curl gcc libxml2-dev libxslt-dev libcurl4-openssl-dev libreadline6-dev libc6-dev libssl-dev make build-essential zlib1g-dev openssh-server git-core libyaml-dev postfix libpq-dev libicu-dev

Install ruby from source.
mkdir /tmp/ruby && cd /tmp/ruby
curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p353.tar.gz | tar xz
cd ruby-2.0.0-p353
./configure --disable-install-rdoc
make
sudo make install

Set up runners.
sudo gem install bundler
sudo adduser --disabled-login --gecos 'GitLab CI Runner' gitlab_ci_runner
sudo su gitlab_ci_runner
cd ~/
git clone https://gitlab.com/gitlab-org/gitlab-ci-runner.git
cd gitlab-ci-runner

Install the gems
bundle install --deployment
Set up the runner interactively.
bundle exec ./bin/setup
Place an init.d file.
exit;
cd /home/gitlab_ci_runner/gitlab-ci-runner
sudo cp ./lib/support/init.d/gitlab_ci_runner /etc/init.d/gitlab-ci-runner
sudo chmod +x /etc/init.d/gitlab-ci-runner
sudo update-rc.d gitlab-ci-runner defaults 21

Start the runner
sudo service gitlab-ci-runner start

Download the sonar runner, optionally copy the directory from another source if you have it already. Check the latest version over at Sonar Currently the latest version is 2.4.
sudo su gitlab_ci_runner
wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip
unzip sonar-runner-dist-2.4.zip

Optionally create a symlink in order to ease future update
sudo su gitlab_ci_runner
ln -s sonar-runner sonar-runner-2.4

sonar-runner/bin must be added to the PATH of the user. This can be done differently, either use your favorite editor as the gitlab_ci_runner user and just edit .profile to include the PATH or try using echo.
sudo su gitlab_ci_runner
echo 'PATH=/home/gitlab_ci_runner/sonar-runner/bin:$PATH' >> .profile

In order for the gitlab-ci runner to successfully run tests you have to modify the config file within sonar-runner/conf/ to point to your sonar server and database server. In addition the android project has to include a sonar runner properties file.
After configuring the config file within sonar-runner/conf I’ve added the following sonar-project.properties

sonar.projectKey=android-my-project-key
sonar.projectName=MyProjectName
sonar.projectVersion=1.0

sonar.sources=app/src/main/java

sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.analysis.mode=analysis
sonar.profile=Android Lint

And in the build settings within gitlab-ci I have the following settings:
./gradlew build
sonar-runner

Obviously, in order to use gradlew build the ANDROID_HOME variable must be set for the gitlab_ci_runner user and also point to a path that’s readable by that user. So you obviously need the android-sdk for the platform you want to build on.

Leave a Reply

%d bloggers like this: