Tuesday, May 21, 2013

Gradle - New Android Build System

As I saw keynote on Android Studio (new IDE for Android development which based on IntelliJ IDEA community edition), I start to download the IDE. I'm so enthusiast about what it can help Android developer. However, after testing it awhile, I am not really impressed by it. Actually I feel kind of dumb as I can't build the project which using 3rd party open source library such as ActionBarSherlock.

However, today, after seeing one of the keynote on New Android Build System, I just realized that whatever you add into Android Studio, not directly reflecting the configuration of Gradle build system (but that may change in the future). One of the example is if you adding ActionBarSherlock into AS (Android Studio), and if you build it, there will be Symbol not found errors all over the places (as if ActionBarSherlock is not included in the project. But not according to the IDE as you can still code complete the classes).

So, what can you do to build it successfully? Please consider my project structure:


The red color is where I put the libraries project. The blue color is where my main android project. You may ask, why I put the libraries inside my project directory, it is because easier for gradle to handle it (or more precisely, I don't know yet how to add the external directory to gradle outside main directory).

First thing first, update your settings.gradle:

include ':MahaBarata'
include ':libraries:actionbarsherlock'

As you can see the : symbol will be changed into / (or \ if you are on windows). This to indicate the build path like on eclipse.

The second things is to update your build.gradle inside your main directory (in this case is /Mahabarata dir) to add dependencies on ActionBarSherlock

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile project(':libraries:actionbarsherlock')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 16
    }
} 

You may confused that there is a / and : symbol mixed within the file, but please bear with it as it is standard coming from gradle.

The third thing to do is to create build.gradle inside your ActionBarSherlock directory

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android-library'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile files('libs/android-support-v13.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }


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

        instrumentTest.setRoot('tests')
    }

} 

It is kind of similar with the main project build.gradle file above, only adding *.jar dependencies libraries to it.

After the last file is added, just open your terminal to run

gradlew build
or if you are on windows
gradlew.bat build
in the project directory or just build it right away inside AS. And from my case, it build successfully.

Note: not really without any glitches tho. I ran into out of space error within gradle on merging resources task, which suddenly will spawns many aapt thread all over the memory which I killed them and rerun it with gradlew build --info

Update 1: with the newest gradle plugin (on the time I update this blog), version 0.4.2, out of space error is not going to happen anymore.

Update 2: with the newest gradle plugin keep rolling out. You can simply update your gradle.build file as follow:

Update 3: newest Android Studio (v0.2.0) is out and google dev update the google plugin to 0.5

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