android-gradle Tutorial

What is android-gradle

android-gradle is a gradle plugin officially maintained by Google Tools developer team and is the official build tool since the announcement in May 16, 2013 at the Google I/O.

Learn the basic by reading Configure your build with Gradle.

Main features

The main features of the Android Gradle Plugin are:


  1. Download and install Android Studio
  2. open it and create a new project with all default settings

In theory you can install gradle directly, build the configuration files and directory structure by yourself. In practice no-one does that.

Project Structure

A project folder structure typically look like this:

Android Project folder structure

android-gradle Plugin

A gradle project is usually divided in sub-project or modules each containing a dedicated build script.

The plugin dependency is usually declared in the main / top level build.gradle file:

buildscript {
    // maven repositories for dependencies
    repositories {
    // build script dependencies
    dependencies {
        // this is the dependency to the android build tools
        classpath ''

allprojects {
    // maven repositories for all sub-project / modules
    repositories {

In this example the android-gradle plugin version is 2.1.2 as you can see from this line:

classpath ''


The Project is divided into modules each containing a dedicated build.gradle script. The settings.gradle file list these modules:

include ':app'

The colon : is used somewhat as a folder delimiter.

To use the plugin it has to be applied at the top of the build.gradle file of each module (app in the example).

For an Android Application:

apply plugin: ''

For an Android Library:

apply plugin: ''

And then configured in it’s android tag:

android {
  // gradle-android plugin configuration

Basic Android application Configuration

The build.gradle generated by Android Studio for an application looks like this:

apply plugin: ''

android {
    // setup which version of the SDK to build against and
    // with what version of the build tools
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    // default app configurations
    defaultConfig {
        // this is your app unique ID
        applicationId "com.example.myapp"
        // devices with lower SDK version can't install the app
        minSdkVersion 14
        // target SDK version, should be the last available one and
        // match the compile one
        targetSdkVersion 23
        // integer and string version of your app
        versionCode 1
        versionName "1.0"
    // default build types are "debug" and "release"
    buildTypes {
        release {
            // enable / disable proguard optimization
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

// app dependencies
dependencies {
    // any jar file in the libs folder
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // test dependency
    testCompile 'junit:junit:4.12'
    // runtime dependency on the support library
    compile ''

Configure your build with Gradle teach you more advanced Android Gradle Plugin settings and options and go deeper in the meaning of this setting.

The defaultConfig is called like that because it can be overridden with Product Flavors.

The buildTypes tag allow you to setup how to build your app enabling optimization (like proguard), you can learn more reading Build Types. It can also be used to setup signing of your app.

You should also learn more on how to Declare Dependencies. As you see the dependencies tag is outside the android one: this means it’s not defined by the Android plugin but it’s standard gradle.

The Gradle Wrapper

Android Studio will also, by default, install a gradle wrapper. This is a tool you can execute directly from the command line and it will download a local specific version of gradle the first time you execute it.

To launch compile the app you can then launch the gradle wrapper

Linux / Mac:

./gradlew assemble


gradlew assemble

The script launch the wrapper, contained in a gradle folder in the root directory of your project:

  • gradle-wrapper.jar: the code of the wrapper to download gradle and execute it
  • define which gradle version the wrapper should download

External Links: