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.
The main features of the Android Gradle Plugin are:
In theory you can install gradle directly, build the configuration files and directory structure by yourself. In practice no-one does that.
A project folder structure typically look like this:
android-gradle
PluginA 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 {
jcenter()
}
// build script dependencies
dependencies {
// this is the dependency to the android build tools
classpath 'com.android.tools.build:gradle:2.1.2'
}
}
allprojects {
// maven repositories for all sub-project / modules
repositories {
jcenter()
}
}
In this example the android-gradle
plugin version is 2.1.2
as you can see from this line:
classpath 'com.android.tools.build:gradle:2.1.2'
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: 'com.android.application'
For an Android Library:
apply plugin: 'com.android.library'
And then configured in it's android
tag:
android {
// gradle-android plugin configuration
}
The build.gradle
generated by Android Studio for an application looks like this:
apply plugin: 'com.android.application'
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'), 'proguard-rules.pro'
}
}
}
// 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 'com.android.support:appcompat-v7:24.0.0'
}
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
.
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
Windows:
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 itgradle-wrapper.properties
define which gradle version the wrapper should downloadAs described in the remarks section the Android build system uses the Android Plugin for Gradle to support building Android applications with Gradle.
You can specify the Android Plugin for Gradle version in the top-level build.gradle
file. The plugin version applies to all modules built in that Android Studio project.
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
}
}
As described in the remarks section you can specify the Gradle version used by each project editing the Gradle distribution reference in the gradle/wrapper/gradle-wrapper.properties
file.
For example:
...
distributionUrl = https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
...
To setup for using Android Gradle Plugin you need many things:
The easiest way to get all of them is to follow these steps:
Check Remarks section for more informations.