Android, gradle and sonar analysis - Part I

In this two-part write-up, I capture my experience setting up one of our android projects to use gradle to build as well as do sonar analysis.

With Android Studio, google is moving from ant to gradle to build android projects.  This being the case, I thought it was imperative to get our build working with gradle (as well).

To this extent, when we started a new android native project, I decided to set it up to use gradle, instead of ant, to build in our continuous integration tool jenkins.

Before trying to run in jenkins, it was important to get the build script running on our desktop. 

On my Windows desktop, I downloaded and "installed" (if unzipping can be called that) the latest version of gradle. (1.11 as I write this).  (Gradle needs java to be installed, but then fancy doing android development without java!) 

I added GRADLE_HOME environment variable as well as added GRADLE_HOME/bin to PATH.  If these are done right, you get the following:

gradle -v 
--------------------------Gradle 1.11 -------------------------------------------
Build time: 2014-02-11 11:34:39 UTC
Build number: none 
Revision: a831fa866d46cbee94e61a09af15f9dd95987421 

Groovy: 1.8.6 
Ant: Apache Ant(TM) version 1.9.2 compiled on July 8 2013 
Ivy: 2.2.0 JVM: 1.7.0_51 (Oracle Corporation 24.51-b03) 
OS: Windows 8 6.2 amd64

The easiest way to bootstrap a gradle build script is to open the android project in Eclipse having Android Development Tools plugin and using the Export... option, which allows you to create the gradle build script.

I modified the default script generated by ADT and made the following change:

Updated android build tool version to 0.9.0

dependencies { 
  classpath '' 

Updated buildToolsVersion

buildToolsVersion "19.0.2" 

Since I wanted to generate a release build (which is signed), I added the following:

signingConfigs { 
   release { 
       storeFile file("mykey.keystore") 
       keyAlias "myAlias" 
       storePassword "myStorePassword" 
       keyPassword "myKeyPassword" 

buildTypes { 
   release { 
       signingConfig signingConfigs.release 

Now running

gradle clean build

gave me a debug and release build, including a lint check and report.

The next thing was to set this up to run in jenkins. To achieve this, I did the following:

Step 1: Installed gradle on the system running jenkins
Step 2: Installed the jenkins gradle plugin which allows gradle build script as the main build.
Step 3: Specified gradle install location to jenkins

Step 4: Configured the project in jenkins

That was it.  Now my android project was being run on jenkins using gradle.

The next step was to run a sonar analysis on the project.  


a) There is gradle build plugin
b) There is gradle
b) There is build tools

Each of these keep changing.  Thus, we need to ensure compatible versions of each of these to successfully build the project.

The fun multiplies when you add Android Studio to the equation, since that adds yet another variability.