/ Gradle

Learn Gradle Episode 1

Why Gradle?

The official developer website for Android recommend use Android Studio as develop IDE. It based on IntelliJ IDEA. This tool is using Gradle-based build system. A lot of java developer use Eclipse which is use Maven and Ant as default build system. This build script is based on xml that is becoming more and more out of date. Also you can check the difference between Gradle and Maven here.Therefore, learn some Gradle knowledge are necessary.


Gradle is a very strong build script system, it can support a lot of platform. Java, Python, JavaScript, Scala, C++, Android, iOS, Hadoop and many many more. The very important is it have a very friendly documentation for developer to learn and use it. As you know, I came Gradle cause the Android Studio. Therefore, I will focus on Android platform building for this blog.

Getting Stared

When you create a new application project from Android Studio, you will get the following structure in file explorer.
File Explorer
These files are generated automatically, so don't worry about them. We will explain something for it.

build.gradle (Project)

This script is the root and entrance for this GradleDemo project.

 buildscript {
     repositories {
     dependencies {
         classpath 'com.android.tools.build:gradle:2.0.0-beta6'

         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files

 allprojects {
     repositories {

 task clean(type: Delete) {
     delete rootProject.buildDir

You can also add mavenCentral() or any other repository to the property 'repositories'.

'dependencies' is describe the dependency for this project. Here is point to android build tool which is 'gradle:2.0.0-beta6'.

Note: Everytime when you edit your Gradle file, Android Studio will ask you sync your project. After you press 'Sync Now'. Project will show you the result for your sync build process. Normally, no need to edit this file.`

build.gradle (Module:app)

This script is describe for module:app. If you have more than one module, we will have build.gradle file more.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.a31mins.gradledemo"
        minSdkVersion 23
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.0'

apply plugin tell Gradle which type of project you want to compile. Here we use Android plugin. When you point you plugin as android, then you can use the definition like android block. It add some DSL for Gradle in nature. If you want to something more, please learn from here.

In 'android' block, you can special your compile sdk version, your android build tool version. The defaultConfig property has some definition same as AndroidManifest.xml.

When you run your build, Gradle will use these settings to replace your AndroidManifest setting.

'buildTypes' tell the build system which build types are supported.
dependencies are the most efficient for integrate other third party libraries. Same like CocoaPods for iOS, npm for nodejs. You can just describe which library you want to integrate. After you sync you project, Gradle will handle the dependency for you automatically. e.g. If you want to integrate android-aysnc-http, you just need to add one line compile 'com.loopj.android:android-async-http:1.4.9'.

gradle-wrapper.properties (Gradle Version)

Android projects are configured to use the Gradle Wrapper by default. Open your file explorer you will find gradlew/gradlew.bat which are support command line tool for Linux/Unix/Windows. We can just use $./gradlew build to build whole project. If your just copy your project to other place without the hidden folder .gradle, then you use gradlew to compile, it will download the gradle wrapper from distributionUrl that your defined in gradle-wrapper.properties.

To avoid download the gradle everytime, you can download the zip package from website. Then change distributionUrl like this. distributionUrl=file://<path-to-gradle>


This file is ProGuard rule for your application. Detail please check here.


Gradle provides several options that make it easy to configure the Java process that will be used to execute your build. You can special some environment for the build process. e.g.
When you set this property to true, The developer environment is optimized for speed and feedback so we nearly always run Gradle jobs with the daemon.
Specifies the Java home for the Gradle build process. E.g. when you install sever JDK, that you can use the one which you need.


The settings file is executed during the initialization phase. When you have several project, you will need to change it here.


This file is automatically generated by Android Studio. It point your to your sdk location, when you are using command line tool. You need to change it here. Otherwise, please do not modify this file.

With these files, we can use Android Studio to compile or just use command line tool ./gradlew build to compile the whole project.