Automating Android builds with Gradle

Gradle is a new build automation tool intended to replace older technologies such as Maven and Ant. With that purpose, Gradle can automate the building, testing and deployment process of your Java based applications. The Android project has already upgraded its building tools to use Gradle. Thus, you should really try this if you are developing Android applications. Moreover, if you are following the Test Driven Development (TDD) methodology, Gradle is a must.

Installing Gradle

First, make sure you have installed a Java JDK (Gradle requires JDK 1.5 or higher).
Then, you need to download a distribution from the Gradle website (the current supported version is Gradle 1.6) and uncompress the file into your desired directory.

Afterwards, you have to configure the GRADLE_HOME environment variable on your path:

export GRADLE_HOME=/your_gradle_directory
export PATH=$PATH:$GRADLE_HOME/bin
source ~/.bashrc

Finally, you can check your working installation by running:

gradle -v

The output shows your Gradle version and other local environment configuration.


Using Gradle with an Android Project

An Android application can have 3 types of dependencies:
  1. The Android SDK
  2. JAR libraries
  3. Android library projects
The first is easily configured by installing the Android SDK and using the Eclipse Development Tools. JAR libraries are typically included in your application's libs folder.

Android library projects are different, as they are not JARs. A library project is actually an Android application on its own with the ability to be included as a dependency in another application. Thus, a library project contains its own source code, resources, assets and a manifest file. All these dependencies can be managed with Gradle.

To use Gradle in your Android application, you have to create 2 files in your application's root directory:

build.gradle
local.properties

Below I describe a sample of both files with the minimal configuration to get you started.

build.gradle


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
buildscript {
  repositories {
    mavenCentral()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:0.4'
  }
}

apply plugin: 'android'

android {
  compileSdkVersion 16
  buildToolsVersion '17'

  sourceSets {
    main {
      manifest.srcFile 'AndroidManifest.xml'
      java.srcDirs = ['src']
      resources.srcDirs = ['src']
      renderscript.srcDirs = ['src']
      res.srcDirs = ['res']
      assets.srcDirs = ['assets']
    }

    instrumentTest.setRoot('tests')
    instrumentTest {
      java.srcDirs = ['tests/src']
      res.srcDirs = ['tests/res']
      assets.srcDirs = ['tests/assets']
      resources.srcDirs = ['tests/src']
    }
  }

  dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
  }
}

The buildscript is used to retrieve the necessary dependencies to build the application. As you can see, Gradle can also use Maven Central to look for dependencies.

Then, in line 11, we use the Android Plugin for Gradle and the next lines are configurations for the different folders of a typical Android application.

If you have JAR libraries in your project's libs folder, you can make Gradle use them rather than trying to search for them on a remote repository. This is done in lines 36-37.

local.properties

This file contains only a reference to your Android SDK folder, that is used by Gradle to look for the Android dependencies:

sdk.dir = /your_android_sdk_path

If you are using a version control system (e.g. Git), it is probably a good idea to ignore this file, as this path is dependent of each environment.


Gradle Tasks

Once you have correctly configured your Android project with Gradle, it is time to perform some automation tasks, you can run on your terminal any of the following commands:
  • gradle assemble
    • Assembles your Android project including all its dependencies
  • gradle connectedInstrumentTest
    • Installs and run the tests
  • gradle build
    • Assembles and tests the projeect
  • gradle tasks
    • Shows a list of other available tasks
Now you have an automated workflow for testing, building and deploying your Android projects. Don't forget to read the Gradle Android Plugin user guide for additional information.

Also, you can have a look to a working build of an Android-Gradle sample project at Github. The build and instrumentation tests are running on Travis CI using Gradle.

Comments

  1. Nice blog, very informative content.Thanks for sharing, waiting for the next update…

    c++ courses in Chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

Android Testing in Headless Emulator

Heroic Web Applications with AngularJS