Build Android – Continuous Integration with Jenkins and Docker

header_image

This tutorial assumes that you have Jenkins in the docker. You can read about installing the same here.

 

Once the jenkins instance, named jenkins-master, is installed go to the root bash i.e. command prompt for your container.

e.g. docker exec -it --user root jenkins-master bash

From the bash command run the following:

> cd /opt

Know the sdk version and download:

> wget http://dl.google.com/android/android-sdk_r24.0.1-linux.tgz

unzip file:

> tar zxvf <filename of the just downloaded file>

You can now remove the file you just downloaded:

rm <filename of the just downloaded file>

Now some environment variables are required to be set.

vi /etc/profile.d/android.sh

——————————————————–
By default the vim is not present in docker
you need to install by the following

apt-get update
apt-get install vim
——————————————————-

Add the following lines:

export ANDROID_HOME="/opt/android-sdk-linux"
export PATH="$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH"

Then reload the file:

source /etc/profile

Now you should be able to call android from the command.

screen-shot-2016-11-05-at-3-54-19-pm

First list the available android sdk and platform tools:

android list sdk --all --extended

Get the item serial number from output and run:

> android update sdk -u -a -t <serial number>

Replace <serial number> for required: platform version,  android support library and sdk version.

Check your android gradle file for the required version.

For the Android SDK to be accessible by Jenkins, execute the following:

> chmod -R 755 /opt/android-sdk-linux

If you get this error
Cannot run program “/usr/local/android-sdk-linux/build-tools/19.0.3/aapt”: error=2, No such file or directory

then run this command: 

> apt-get install lib32stdc++6 lib32z1

 

All set and now reboot your container: 

docker restart CONTAINERNAME/ID

 If everything is done correctly, you should be able to set the gradle task and generate android apk/jar/aar :-).

screen-shot-2016-11-05-at-3-59-07-pm
Reference: https://www.digitalocean.com/community/tutorials/how-to-build-android-apps-with-jenkins