Android Build Automation with Gradle.

Gradle is a build system introduced by google in the 2013 Google I/O to replace the build system inside ADT and ANT. Gradle can automatize the building, testing and deployment process of any Java based applications. Google  has already upgraded its building tools to use Gradle. With the various features that Gradle has, its high time that android developers should try it.  Gradle is a must have for Test Driven Development (TDD) methodologies.

This blog will cover how to use the gradle build system for android.

Step 1: Install Gradle.

  •  Gradle requires Java JDK(1.5 or higher).
  •  Download the gradle distribution from their website.(1.6 and above)

Configure the GRADLE_HOME environment on your system path.

 

Check you working installation of Grails by running:

gradle -v

The output will show info for your current Gradle version.

Step 2: Using Gradle with Android Project.

Generally an Android has three types of dependancies:
  1.  Android SDK
  2. JAR libraries
  3. Android library projects

The first one is easily configured by using  the Eclipse Development Tools. The second one ,JAR libraries, are usually included in your application’s libs folder.The third one , Android library projects, are different from the JAR dependencies. A library project is also an Android application on its own with an added ability to be added as a dependency to another android application. So it has its own  source, resources, assets and a manifest file like any android app. All these dependencies can be managed using Gradle.

To use Gradle in any Android application, we have to create the followign files in our android app’s root directory:

  • build.gradle
  • local.properties
The latest version of eclipse ADT supports generating Gradle support. Follow this blog to export project as Gradle build files, so that the adt will generate the files for you. Reffer the following example files.
Build.gradle:
buildscript {
 repositories {
 mavenCentral()
 }
 dependencies {
 classpath 'com.android.tools.build:gradle:0.6.+'
 }
}

apply plugin: 'android'
dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}
android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

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

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src//... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}
Gradle uses the buildscript to fetch the necessary dependencies to build the android application. Gradle is also able to use Maven Central to look out for dependencies. 

The apply plugin:’android’ will ask the Gradle to use the Android Plugin for Gradle to build the application. The following lines are configurations for the different folders in a usual android application.If you have JAR libraries in your project’s libs folder, you can make Gradle use them by adding in the dependency block.

local.properties:

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

sdk.dir = /your_android_sdk_path

This path is dependent of each environment.

Step 3: Lets get started with Gradle.

Now that we have correctly configured Gradle with our android project. We can now perform some automation:

  • gradle assemble
    • Assembles your Android project including all its dependencies.
  • gradle connectedInstrumentTest
    • Installs and run the tests.
  • gradle build
    • Assembles and tests the project.
  • gradle tasks
    • Shows a list of other available tasks.
We can easily import this gradle build in the Android Studio very easily. Now we have an automated workflow for testing, building and deploying our Android projects.
For more info read:   Gradle Android Plugin user guide.
Sample Project:  Android-Gradle sample.
The following two tabs change content below.

Amal Chandran

Latest posts by Amal Chandran (see all)

Submit a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current month ye@r day *