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
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.
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.
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.
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.