Setting up a Jenkins continuous integration environment for Android

The aim of this article will be to run through the steps needed to setup a Jenkins based CI environment to build and Android project, run unit tests, integration tests, etc. It will run through all of the tasks assuming that you have a clean Windows installation without even Java being installed.

  • Install JDK
  • Install SCM (Git/SVN)
  • Install Jenkins
  • Install the Android SDK
  • Install any useful Jenkins plugins
  • Create your first build target

Install JDK

At the time of writing this, JDK 1.8 is the latest version so I would recommend installing that on your server, however, if for any reason you cannot run with this JDK and then install the version that your project requires. Just head over to Oracle’s site, download and install it in the default location.

Install SCM


Our project used git and initially I came from an SVN background so I found TortoiseGIT a useful client. This can be found here. It’s not the best client because it encourages you to use git in an SVN way but it does the job for us.

You will also need to install the git client application which you can get from here. However, if you start up TortoiseGIT you will be prompted almost straight away to do this. Download it and install it on your machine.

This is useful to have these tools on your machine to manually test the cloning of your builds etc. if you have issues when configuring the server.

Set the location of git in your system path

Jenkins needs to know where the git application is installed. The easiest and most useful way to do this is to add it to the system path. Click the start menu and type environment then select system environment variables from the search results. Then click Environment Variables and find Path and edit it. Assuming you installed git in the default location you should add ;C:\Program Files (x86)\Git\bin to the end of the path.


If you use SVN still, then it install your preferred SVN tools at this point.

Install Jenkins

So this is the main reason for this article, time to install Jenkins. You can find it here. Jenkins is one of the most popular CI servers for Java at the moment. It is a fork of the Hudson CI project so, if you are unfamiliar with these servers and you see some people talking about Hudson and some about Jenkins, these are effectively the same thing. The plugins between the servers are all compatible with both servers.

Screenshot 2015-04-22 15.41.15

This article is for Windows and therefore we’ll be installing the native package. This has the benefit that it comes with Windows Service wrappers so when the server restarts then Jenkins will automatically restart as well.

Now is the best time to update all of the plugins that come with Jenkins. To do this click on the Manage Jenkins link on the left hand side of http://localhost:8080/ then click Manage Plugins. Click Select All at the bottom of the screen and then click Download Now and install after restart. While it is downloading the plugins, check the Restart Jenkins when installation is complete and no jobs are running checkbox so that the plugins are updated.

Install Jenkins Plugins

There are a number of plugins that we’ll need for Android or are just recommended because they are really helpful in my opinion. To select these plugins:

Click Manage Jenkins

Click Manage Plugins

Click the available tab

Search for each of these plugins, click select and then install without restart. Some of the plugins will already be installed.

  • Android Emulator Plugin – This is required to be able to run the android emulator and run instrumented automated tests.
  • Git Client Plugin
  • Git Plugin
  • JaCoCo Plugin – This is useful for capturing code coverage information when running unit tests and any other automated tests.
  • JUnit Plugin – When running any JUnit tests in your build this plugin will allow Jenkins to report the results in a useful way.
  • Gradle Plugin – This allows Jenkins to run Gradle based builds which on modern Android projects will be a requirement.
  • JavaDoc Plugin – Use this plugin to provide JavaDoc output in your build of your source.

Install Android SDK

The next step we need to take before attempting to build anything is to download and install the android SDK and point Jenkins at it. Head over to the Android SDK site here and download the stand alone SDK. Install it using the default settings apart from make sure that it is available for all users so the service can access it.

Start up the SDK Manager and download the SDK that you need and any Extras that you might need. If you’re already building from an IDE like Android Studio you’ll already know what you need.

Once this is done you need to tell the OS where Android SDK is so that Jenkins can use the right location for building. To do this open up the Windows System Environment variables and create a new variable called ANDROID_HOME and set it to C:\Program Files (x86)\Android\android-sdk assuming that is where you installed it to.

Create your first build target

  • In the top level screen, click on New Item then enter the name of your project, select Freestyle Project and click OK.
  • Enter a description for the target.
  • Then enter the details for your SCM. I’m using git so I select git and enter the URL.
  • We also connect using a specific build account and password so we add that by clicking on the add button below the URL.
  • If you get the error Could not init C:\Windows\TEMP\hudson this is most likely because you haven’t added git to the system path for Jenkins to find it. Once you do this and restart the Jenkins service it will work.
  • Next you’ll need to setup up what to build. Under the Build panel, click the Add Build Step button.
  • Select Invoke Gradle Script, click on Use Gradle Wrapper
  • Enter  clean build into the Tasks field.

You’ll likely want to archive the artifacts of the build, i.e. keep the APK files for future use so

  • Click on Add Post Build Action
  • Enter **/*.apk into the

That’s it. You can of course create other targets in Jenkins to build JavaDoc, run unit tests and integration tests on code commit etc. but this will get you the base system working.

Adding Jenkins build number into your Android application

Today I was trying to get the Jenkins build number into my Android app for display, however, I couldn’t find any tutorials on how to do this. I have pieced together a few things and put them together below.

Getting the Jenkins build number into Gradle build system

So, Android uses two fields for versioning, these appear in the app’s build.gradle file.

versionCode – The unique version that android uses to distinguish between versions of an app.

versionName – This is more of a human readable and flexible build number that is what I’d like to display. By default this is hard coded to “1.0” and I’d like to make it “1.0.” + JENKINS_BUILD_NUMBER.

Jenkins exposes the current build number as a system environment variable so we need to access this in the build script using the following code. Put this in the defaultConfig section for now (I’m sure there might be a better place but this works for me):

ext.buildNumber = System.getenv("BUILD_NUMBER") ?: "dev"

This code above sets the buildNumber variable to the jenkins build number if it is not null, otherwise it sets it to “dev”. I did this so that we can distinguish between development builds and continuous integration builds.

I then updated versionNumber with the following code:

versionName "0.1.$buildNumber"

This now makes the Jenkins build number and our application version accessible to our application.

Getting the version name from the build system into my app
If you are using the Gradle build system there is an addition class available to your application that allows you to get the configured version number.

Add BuildConfig.VERSION_NAME to where ever you need to retrieve the current version name from. For example, I have a TextView in the settings screen of my application and I set the version name with the code below.

final TextView versionName = (TextView) findViewById(;

That’s it.

Kicking off a Jenkins/Hudson build on SVN commit

This is something that a lot of people out there like to do when you’re code contains automated tests. There are two options available to you which are:

1) Get Jenkins/Hudson to poll the SVN server to look for changes.
2) Notify the build server when changes are made on the SVN server.

Option 1 is a lot easier to do than option 2, mainly because it can be done out of the box. This works fine if you are happy for the server to poll SVN every few minutes and you can wait.

– If you want to poll SVN every 2 minutes say, then go to the configuration page for your build task in Jenkins.
– Scroll down until you get to the “Build Triggers” section.
– Tick the Poll SCM and enter “H/2 * * * *” into the Schedule section.
– Click Save.

Option 2 is required if you want the server to be more responsive to changes in SVN. There are instructions on how to do this here, look for the Post Commit Hooks section.