Tutorial details

Building Android Applications - Tutorial | App Code for Sale | Preview

Building Android Applications - Tutorial | Android Tutorial

How create Android Applications with the command line tool and how to build it via Apache Ant or the Gradle build system

Overview PAGE TOP

Lars Vogel

Version 1.2

Copyright © 2011, 2012, 2013 Lars Vogel



Building Android Application with Gradle or Apache Ant

This tutorial describes how create Android Applications with the command line tool and how to build it via Apache Ant or the Gradle build system. Android is currently using Apache Ant as default but is planning to switch to Gradle as default build system.

1.1. Android SDK and ADT PAGE TOP

The Android SDK provides tooling for the command line to create and build Android projects.

The Android Development 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 PAGE TOP

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.1. Android PAGE TOP

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. PAGE TOP

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.2. Create application PAGE TOP

Create a new Android project called with the Eclipse Android project wizard.

3.3. Create Ant build file PAGE TOP

Switch to the command line and into the directory which contains you new Android project. Type the following to create your build.xml file.

android update project -p . 

Now you can build and deploy your project via Apache Ant.

// Clean the project
ant clean

// Build a debug version
ant debug

// Builds the debug version
// and installs it
ant debug install

3.4. Start the application PAGE TOP

You can also start your application via the command line. For this you use adb shell to send an intent to your activity.

adb shell am start -a android.intent.action.MAIN -n de.vogella.android.build.firstant/.MainActivity

4. Running tests with Apache Ant PAGE TOP

To run your tests with Apache Ant you have to create the corresponding build.xml file.

# -p path to the test project
# -m path to the project under test

android update test-project -p . -m ../com.vogella.android.test.simpleactivity 

# Afterwards run the tests
ant test 

The ant debug, ant installt and ant test commands to builds the test and the tested project, install both .apk files, and run the tests.

5. Install Gradle PAGE TOP

Download Gradle from the Gradle web site: http://www.gradle.org/downloads

Extract the Gradle distribution to a folder, which we will call GRADLEHOME. Add GRADLEHOME/bin to your PATH environment variable.

6. Gradle PAGE TOP

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 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 {

    dependencies {
        classpath 'com.android.tools.build:gradle:0.3'

apply plugin: 'android'

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']


To start such a build, use the following command. command on the command line of the associated project.

gradle build 

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 system documentation.

7. Exercise: Building Android applications with Gradle PAGE TOP

Create an Andriod application called com.vogella.android.build.firstgradle

Build this application with Gradle.

Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.