Getting Ant to Work with Android

This tutorial will help you create a custom Ant build script which can be used to build an Android application.  I was very interested in taking an existing Java application and porting it to Android.  I searched the entire interweb and couldn’t find much documentation on the subject.  The example build scripts from the Android Subversion repository were out of date.  I hope this helps someone out there.  I’m guessing this will only be useful until Google cleans up their Ant Tasks (don’t hold your breath).  For this tutorial, I was using Android SDK Release 5.

This tutorial was written for 64-bit Linux, but similar steps could be taken for a Windows environment.  For this tutorial, the following variables will be used in the description:
<sample_app> – the name of the sample application you built using the Android Eclipse plugin, so you could borrow the basic configuration files.
<custom_app> – the name of the custom application need a Android Ant build script for.
<sdk_install_dir> – the directory where the Android SDK was extracted
<android_platform> – the directory where the Android 2.1 Platform is installed (e.g. /home/mike/bin/android-sdk-linux_86/platforms/android-7)

1) Make a backup of the <custom_app>.  I would recommend using a Configuration Management tool such as Subversion.

2) Use the following tutorial to install the Android 2.1 “Package” and create a “Virtual Device”:

3) Add the SDK tools directory to your path:

export PATH=${PATH}:/home/mike/bin/android-sdk-linux_86/tools

4) Create a sample application with the Android Eclipse plugin called <sample_app>.

5) Copy the configuration files from the sample application to the directory of <custom_app>.

6) Create a file called “” which has the following line, pointing to the Android SDK:


7) Create a file called “” which has the following lines.  Replace “<your_custom_path>” with your own custom path details:
source.dir=<your_custom_path>/src # points to the android source code

source.dir=<your_custom_path>/src # points to the android source code
gen.dir=<your_custom_path>/gen # points to the android gen directory
resource.dir=<your_custom_path>/res # points to the res directory 
asset.dir=<your_custom_path>/assets # points to the assets directory
external.libs.dir=<your_custom_path>/lib # points to the lib directory
native.libs.dir=<your_custom_path>/lib # points to the native libs
out.dir=<your_custom_path>/out # points to a place where your classes are compiled and the Android packages are placed

8 ) Make a backup of <android_platform>/templates/android_rules.xml.

9) Edit android_rules.xml and comment out lines 17-27.  We need to do this because there is no way to pass in the android.antlibs via an Ant parameter.  Ant parameters passed between build files are only visible within a target.

    <!-- <taskdef name="aaptexec"
        classpathref="android.antlibs" />
    <taskdef name="apkbuilder"
        classpathref="android.antlibs" />
    <taskdef name="xpath"
        classpathref="android.antlibs" /> -->

10) In your build.xml file, add the following lines:

    <property file="" />
    <property file="" />
    <property file="" />
    <path id="android.antlibs">
        <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
        <pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
        <pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
        <pathelement path="${sdk.dir}/tools/lib/apkbuilder.jar" />
        <pathelement path="${sdk.dir}/tools/lib/jarutils.jar" />
    <taskdef name="setup"
        classpathref="android.antlibs" />
    <setup import="false" />
    <taskdef name="aaptexec"
        classpathref="android.antlibs" />
    <taskdef name="apkbuilder"
        classpathref="android.antlibs" />
    <taskdef name="xpath"
        classpathref="android.antlibs" />

11) Somewhere farther down in your build.xml file, add the following.  Make sure you customize the path to android_rules.xml (if necessary).  This will run the “debug” target in android_rules.xml.  You need to do further customization for signing your application.  We’ll cover that in a later article.

    <target name="dist-android">
        <ant antfile="${sdk.dir}/platforms/android-7/templates/android_rules.xml"
            target="debug" inheritall="true" inheritrefs="true" />

12) I made some further changes to my Ant script to copy the “.apk” file to a “target/android” directory.

13) Navigate to the SDK tools directory:

cd ~/bin/android-sdk-linux_86/tools

13) Next, run the emulator with the AVD you built using the Google tutorials:

./emulator -avd android_2-1

14) Start the ADB server:

adb start-server

15) Locate your “.apk” file.  The path to it was defined in the “” file under the “out.dir” property.

16) Install your Android package using the adb install command.  Note that I moved my “.apk” file to fit my build standards.  Also note the “-r” parameter which will overwrite your package if it is already installed:

adb install -r target/android/phantom-debug.apk

Related Posts


  1. Just an update for those who are still reading this article. I made a sample package of this entire environment here:

  2. avatar Jason V.

    a very useful article, thank you, Michael!
    some more info about building Android apps with ant – hope it could be helpful

  3. avatar Shane

    It appears if in the later versions of the sdk the android_rules.xml no longer exists. Any idea why this is or what replaced it? Or perhaps a better question, what to do if one wants to build on the latest sdk? Thanks!

  4. Hi there! I know this is kinda off topic but I was wondering if you knew where I could locate a captcha plugin
    for my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one?
    Thanks a lot!

  5. Most spammers put links back to their sites. This plugin eliminates most of those: “Simple Trackback Validation”.

  6. Here is the captcha plugin: “SI CAPTCHA Anti-Spam”.