Thursday, October 24, 2013

Building Android applications with Gradle - Tutorial

Building Android Application with Gradle
This tutorial describes how build Android Applications with the Gradle build tool.

1. Building Android Applications

1.1. Android SDK and ADT

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.

1.2. Maintain PATH

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.

2. Prerequisites

2.1. Android

The following assumes that you are familiar with Android development. Please see Android Development Tutorial for an introduction.

2.2. Install Apache Ant

2.3. Define ANDROID_HOME environment variable.

Define the ANDROID_HOME environment variable which points to your Android SDK.
// Unix
export ANDROID_HOME=~/android-sdks

// Windows
set ANDROID_HOME=C:\android-sdks

3. Install Gradle

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.

4. Gradle

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.

5. Exercise: Building Android applications with Gradle

Create an Andriod application called com.vogella.android.build.firstgradle.
Build this application with Gradle.

No comments:

Post a Comment