Building Android Application with Gradle
This tutorial describes how build Android Applications with the Gradle build tool.
Table of Contents
The Android SDK provides tooling for the command line to create and build Android projects.
The Android Developer Tools (ADT) offer the same functionality for Eclipse. In Eclipse you can manually export Android applications via the Eclipse wizard.
To build and deploy Android application the Android SDK provide tools which are currently based on Apache Ant.
The project lead of the ADT has recently announced that ADT is moving to a Gradle based build system and that the Eclipse tooling is also going to be based on the same build system.
Tooling for building Android Application with Maven are also available. The move of ADT to Gradle makes the support of Maven easier as Gradle uses Maven repositories to manage its dependencies.
Building Android outside Eclipse involves using the command line or shell. You should be familiar with using a shell in case you want to be able to perform this tutorial.
Make sure that the
tools
and platform-tools
folder of the Android SDK installation directory are part of your PATH
environment variable.The following assumes that you are familiar with Android development. Please see Android Development Tutorial for an introduction.
Download Gradle from the Gradle web site: http://www.gradle.org/downloads
Extract the Gradle distribution to a folder, which we will call
GRADLE_HOME
. Add GRADLE_HOME/bin
to your PATH environment variable.Gradle is an advanced build management system based on Groovy. A project describes its Gradle build in a file called
build.gradle
located in the root folder of the project.Gradle supports the management of project dependencies (libraries). For this it supports existing Maven and Ivy repositories for retrieving dependencies. This allows a reuse of artifacts of other build systems.
Gradle provides a simple way of configuring the build. The following shows a simple build file which maps to the structure of an existing Android project. The ADT team may change the structure of newly generated Android projects at some point to fit better to the Gradle build system.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4+'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
}
android {
compileSdkVersion 17
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aild.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
android {
buildToolsVersion "17.0"
compileSdkVersion 17
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aild.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
To start such a build, use the following command.
command on the command line of the associated project.# build project
gradle build
# build project complete from scratch
gradle clean build
# speedup second grandle build by holding it in memory
gradle build --daemon
Gradle supports a format called Android ARchive (AAR) . An AAR is similar to a JAR file, but it can contain resources as well as compiled bytecode. This allows that an AAR file is included similar to a JAR file
This command creates in the
build
folder the output of the Gradle build. By default the Gradle build creates two .apk
files in the build/apk
folder.To build, deploy and start your tests you the following command.
gradle instrumentTest
For more information on the Android Gradle build system see Android Gradle build systemdocumentation.
No comments:
Post a Comment