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

Android – Share code between multiple applications

Physical Path way

It may work well when you have a common drive location among code contributors. If you are only the one who maintain this library for a  different projects then this can your favourite option.

Open your app settings.gradle and add these lines:

include ':app'
include ':networkservices'
include ':common'

project (':networkservices').projectDir = new File('/Users/mramit/Documents/gits/lib/networkservices')
project (':common').projectDir = new File('/Users/mramit/Documents/gits/lib/common')

How to use it in app/ library?

All you have to do is add a dependency of this library: 

dependencies {
    compile project(':networkservices')
}

AAR way

Just like you create Jar for Java, you can also do the same for Android. However, it does not work well when you have resources to share e.g. string.xml

Instead of a Jar, the recommendation is to create an AAR file a.k.a Android Archive.

Why aar?

aar file is developed on top of jar file. It was invented because something Android Library needs to be embedded with some Android-specific files like AndroidManifest.xml, Resources, Assets or JNI which are out of jar file’s standard. So aar was invented to cover all of those things. Basically it is a normal zip file just like jar one but with different file structure. jar file is embedded inside aar file with classes.jar name. And the rest are listed below:

– /AndroidManifest.xml (mandatory)
– /classes.jar (mandatory)
– /res/ (mandatory)
– /R.txt (mandatory)
– /assets/ (optional)
– /libs/*.jar (optional)
– /jni/<abi>/*.so (optional)
– /proguard.txt (optional)
– /lint.jar (optional)

Then when to use to JAR?

If you are planning to provide any res in your common repo then the recommendation is *not* to use JAR.
Otherwise, you may go for Jar.

How to create aar ?

Requirement is it should be a library, and you have a library plugin applied in your library gradle.

apply plugin: 'com.android.library'

There is nothing else that needs to be done. When you will build with the gradle task, go to build/outputs/aar/ folder to copy and share this aar file.

How to use aar in your app or library?

Put the aar file in the libs directory (create it if needed), then, add the following code in your build.gradle :

dependencies {
  compile(name:'nameOfYourAARFileWithNoExtension', ext:'aar')
}
repositories{
  flatDir{
      dirs 'libs'
  }
}