dcsimg

Automating Android with Ant

Eclipse is not the only game in town when it comes to compiling Android applications.

An Alternative to Eclipse

The majority of Android application development takes place within the Eclipse environment.

Eclipse is a proven platform for not only Java application development, but also for Android applications thanks to the flexible and capable “plug-in” environment which provides sophisticated functionality for Android developers.

Two examples of this platform-specific, plug-in functionality include:

  • The layout tools provide a “What You See Is What You Get” (WYSIWYG) experience for creating user interface elements
  • The Dalvik Debugging and Monitoring Server (DDMS) lets you peer into a running device, copy files, and more

Add to this list the build-magic that the Android Developer Tools (ADT) perform behind the scenes:

  • resource compilation
  • Java Bytecode-to-Dalvik conversion
  • application packaging and installation

Working in Eclipse for Android is pretty much a no-brainer, particularly when just starting out.

But what if you want to automate your builds and not have to load Eclipse every time a source file changes? After all, shops with larger projects and multi-developer teams might want to perform nightly, incremental builds.

To automate Android builds, we can use ant. ant is a build tool similar to make. Instead of using makefiles, ant uses a file named build.xml which in turn references any number of external Java properties files. ant’s lineage traces back to the Tomcat application server project — and has since found its way into many other projects, including Android.

Creating a project

This next section assumes that you have the Android SDK installed on your development machine as well as a copy of ant in your execution path.

Create a new directory — I am going to call mine: Antroid.

To create a new project, we will use the android command line program found in the tools directory of the Android SDK. To learn more about this very capable program, type:

android /?

to see a quick display of the command line options available to us.

We want to create a new project, so the base command we want is:

android create project

This command takes a number of options. Here is the usage display:


Action "create project":
  Creates a new Android Project.
Options:
  -t --target   Target id of the new project [required]
  -k --package  Package name [required]
  -a --activity Activity name [required]
  -n --name     Project name
  -p --path     Location path of new project [required]

One of the options we need to supply is which platform we want to target. In order to determine which platforms are available on your system, execute the following command:

android list targets

The output is a list of the available targets we can choose from.

Available Android targets:
id: 1 or "android-3"
     Name: Android 1.5
     Type: Platform
     API level: 3
     Revision: 1
     Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P
id: 2 or "android-6"
     Name: Android 2.0.1
     Type: Platform
     API level: 6
     Revision: 1
     Skins: HVGA (default), QVGA, WQVGA400, WQVGA432, WVGA800, WVGA854
id: 3 or "Google Inc.:Google APIs:6"
     Name: Google APIs
     Type: Add-On
     Vendor: Google Inc.
     Revision: 1
     Description: Android + Google APIs
     Based on Android 2.0.1 (API level 6)
     Libraries:
      * com.google.android.maps (maps.jar)
          API for Google Maps
     Skins: WQVGA400, WVGA800, WQVGA432, HVGA (default), WVGA854, QVGA

Because we may want to use some Google Maps features in our application, we’re going to use a target value of “3″. This “platform” is based on the 2.0.1 Android platform, but is also equipped with the Google Maps functionality. To learn more about installing multiple Android platforms via the Android SDK and AVD Manager, have a look at this Linux Magazine article.

Let’s go ahead and create a new project:

android create project --target 3 --name antroid --package com.linuxmag.antdemo --activity antroid --path .

Here is the output. Notice that all of the scaffolding for our Android project is generated for us!

Created directory /Users/fableson/Documents/Antroid/src/com/linuxmag/antdemo
Added file ./src/com/linuxmag/antdemo/antroid.java
Created directory /Users/fableson/Documents/Antroid/res
Created directory /Users/fableson/Documents/Antroid/bin
Created directory /Users/fableson/Documents/Antroid/libs
Created directory /Users/fableson/Documents/Antroid/res/values
Added file ./res/values/strings.xml
Created directory /Users/fableson/Documents/Antroid/res/layout
Added file ./res/layout/main.xml
Created directory /Users/fableson/Documents/Antroid/res/drawable-hdpi
Created directory /Users/fableson/Documents/Antroid/res/drawable-mdpi
Created directory /Users/fableson/Documents/Antroid/res/drawable-ldpi
Added file ./AndroidManifest.xml
Added file ./build.xml

The last file in the bunch is the key to using ant — build.xml. Let’s take a closer look.

Building with Ant

The syntax of ant’s build.xml is beyond the scope of both this column and at present, this author. The short story is that the build.xml file created in the previous step contains references to three Java properties files which define environmental- and project-specific settings crucial for building this project.

Of even more significance for Android projects such as this one, a number of references are made to Android SDK-supplied ant Jar files (i.e. libraries) which contain the instructions on how to build an Android project with ant. These steps include all of the Android SDK goodies like the resource compiler and packaging tools. The Android specific rules are found in:

/<YourSDKInstallDirectory>/platforms/<target_platform>/templates/android_rules.xml

Just for fun, let’s take a sneak peak at a couple sections taken from android_rules.xml:

   <!-- Generates the R.java file for this project's resources. -->
    <target name="-resource-src" depends="-dirs">
        <echo>Generating R.java / Manifest.java from the resources...</echo>
        <exec executable="${aapt}" failonerror="true">
            <arg value="package" />
            <arg line="${v.option}" />
            <arg value="-m" />
            <arg value="-J" />
            <arg path="${gen.absolute.dir}" />
            <arg value="-M" />
            <arg path="AndroidManifest.xml" />
            <arg value="-S" />
            <arg path="${resource.absolute.dir}" />
            <arg value="-I" />
            <arg path="${android.jar}" />
        </exec>
    </target>

   <!-- Puts the project's resources into the output package file
         This actually can create multiple resource package in case
         Some custom apk with specific configuration have been
         declared in default.properties.
         -->
    <target name="-package-resources">
        <echo>Packaging resources</echo>
        <aaptexec executable="${aapt}"
                command="package"
                manifest="AndroidManifest.xml"
                resources="${resource.absolute.dir}"
                assets="${asset.absolute.dir}"
                androidjar="${android.jar}"
                outfolder="${out.absolute.dir}"
                basename="${ant.project.name}" />
    </target>

It is possible to override the behavior of these build rules taken from the SDK templates.

OK, now for the hard part, let’s build our project with ant:

ant

OK, so that wasn’t too difficult, was it?

Here is a really neat thing about using ant — we can actually perform a build and install operation in one command:

ant install

This will initiate the build and then copy the application to a connected device or running emulator. Here is the output running on my Macbook and connecting to an instance of the Android emulator:

Buildfile: build.xml
    [setup] Project Target: Google APIs
    [setup] Vendor: Google Inc.
    [setup] Platform Version: 2.0.1
    [setup] API level: 6
    [setup] WARNING: No minSdkVersion value set. Application will install on all Android versions.

-compile-tested-if-test:

-dirs:
     [echo] Creating output directories if needed...

-resource-src:
     [echo] Generating R.java / Manifest.java from the resources...

-aidl:
     [echo] Compiling aidl files into Java classes...

compile:
    [javac] Compiling 1 source file to /Users/fableson/Documents/Antroid/bin/classes

-dex:
     [echo] Converting compiled files and external libraries into /Users/fableson/Documents/Antroid/bin/classes.dex...
     [echo]          

-package-resources:
     [echo] Packaging resources
 [aaptexec] Creating full resource package...

-package-debug-sign:
[apkbuilder] Creating antroid-debug-unaligned.apk and signing it with a debug key...
[apkbuilder] Using keystore: /Users/fableson/.android/debug.keystore

debug:
     [echo] Running zip align on final apk...
     [echo] Debug Package: /Users/fableson/Documents/Antroid/bin/antroid-debug.apk

install:
     [echo] Installing /Users/fableson/Documents/Antroid/bin/antroid-debug.apk onto default emulator or device...
     [exec] 458 KB/s (13187 bytes in 0.028s)
     [exec] 	pkg: /data/local/tmp/antroid-debug.apk
     [exec] Success

BUILD SUCCESSFUL
Total time: 6 seconds

That’s pretty much it — add this to the list of things that take a moment to learn and quite longer to become a “master”. I’d love to continue, but I just learned that Google released version 2.1 of the SDK — which is what the Nexus One is using — so I’m going to run and start downloading the new SDK.

Comments on "Automating Android with Ant"

mojavelinux

I\’ve got to barge in here. Ant needs to die. It has already produced way too many chaotic builds.

I do agree with your point that there needs to be another build in place that works outside the IDE. This best practice takes us back to the beginning of Java development, when projects were locked-in to the build mechanism a single IDE.

I\’m simply suggesting that we look for modern solutions such as Gradle or Maven (personally I think Gradle is the better fit).

Reply
fableson

@mojavelinux
thanks for your thoughts — I am hopeful that the topic of automating android builds will pick up speed now that Android is gaining more momentum in many quarters. I have seen some preliminary stuff done by Charlie Collins (one of my co-authors on Unlocking Android) a long while ago with Maven. I\’ll have to check out Gradle and follow up in a future column.

Thanks!

Frank

Reply
dragonwisard

@mojavelinux
Chaotic builds isn\’t a problem in Ant. It\’s just a case of \”Garbage in; Garbage out\”. I\’ve seen horrific or incomprehensible builds written for Make or Maven too.

Your reasoning is suspect. It\’s like saying, \”I don\’t like the applications in Gnome so GTK+ must be a crappy toolkit.\” Or, \”I\’ve seen KDE apps crash so Qt must be garbage.\”

Or my favorite, \”This one Java app I ran back in 1996 was slow and used a lot of memory so Java must be the worst language in the world, I\’m going to use VB instead.\”

Reply
stosss

I agree with dragonwisard. Just because mojavelinux apparently does not like Ant does not make it bad.

Reply
fableson

good or bad, ant is what the Android team chose to ship with the SDK.

And no, VB isn\’t an option for building Android apps, though I am sure some enterprising programmer will try to create the next best development environment for those who are afraid to learn Java. OK, confession, I haven\’t actually searched for VB/Android tools — makes me uncomfortable just writing it…

Reply
jvoegele

For those that feel that Ant needs to die (or at least be buried alive by a more powerful system built on top of it): you can now use Gradle to build Android applications. I have released the first version of the Android plugin for Gradle. Features include:

* Compile, package, and install Android applications. (Including handling of Android resource files.)
* Sign application packages using the default debug key, or with a release key for publication to Android Market.
* Incorporation of ProGuard to ensure that applications have minimal memory footprint.
* Create Android applications in Scala (and possibly Groovy or Clojure).

Full details at http://wiki.github.com/jvoegele/gradle-android-plugin/

Reply

Thanks, I know this a linux mag, but, does work fine on windows 7,64bit (which is a surprise – most stuff abends). Had to fool around with path setting. Once I changed to the project directory level above bin worked fine. a lot of the commands changed between writting and now, but the hrlp screem pointed out what had changed.

Reply

hello there and thank you for your information – I have definitely picked up something new from right here. I did however expertise a few technical issues using this website, since I experienced to reload the website lots of times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I am complaining, but sluggish loading instances times will very frequently affect your placement in google and can damage your quality score if advertising and marketing with Adwords. Anyway I’m adding this RSS to my e-mail and can look out for a lot more of your respective interesting content. Make sure you update this again very soon..

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>