My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
AndroidAnt  
Build your Android application with ant.. without <exec>
Featured
Updated Feb 4, 2010 by phil.h.s...@gmail.com

Overview

It's hard to work long with java and not meet ant. Given its wide adoption among java's (and so android's) developer demographic, it is understandable to want to build an android project with it.

However, to do so requires either using <exec> to call the provided tools (which limits platform independence) or a more-than-cursory knowledge of the libraries behind by those tools. Android-ant is an antlib to fill that gap.

It's unfortunate that raw English isn't executable, so beware! This page may get stale. The best reference is of course the source. In particular, a complete copy of the notepad project is available with a working sample build.xml.

A Step at a Time

Let's examine some key bits of the sample build.xml. I strongly suggest you first check out the code with

svn co http://autoandroid.googlecode.com/svn/trunk/samples/notepad

and follow along with it in-hand.

Declaring the antlib

First, declare the namespace in the <project> tag.

<project name="notepad" default="precommit" xmlns:android="antlib:com.googlecode.autoandroid.ant">

Android-ant is distributed as the android-ant.jar file. Assuming that this jar is in the lib directory, this taskdef will load it:

<taskdef uri="antlib:com.googlecode.autoandroid.ant" classpath="lib/android-ant.jar"/>

Finding android.jar

We are now free to call the available <android:XXX> tasks. This one, for example, tries to detect where android's sdk is, and sets the result to the android-home property:

<android:home/> <!-- Set the detected android-home property to the sdk root. -->

Alternatively, if you want to dictate the location of the sdk, set the location attribute (see issue 3)

You can locate android.jar at ${android-home}/android.jar for use in compiling, etc. If it can't be detected the build will fail with some hints on how to fix it:

frontalot:~/Documents/autoandroid/samples/notepad $ ant
Buildfile: build.xml

BUILD FAILED
/Users/philhsmith/Documents/autoandroid/samples/notepad/build.xml:7: Can't find the android sdk home.  Set the android-home system property
or either of the ANDROID_HOME or ANDROID_SDK environment variables to your sdk root.

Total time: 0 seconds

Using the Tools

Calling the android tools is fairly straight-forward. Android-ant does not (yet) provide tasks with attributes and nested elements customized to specific tasks. Instead it just uses ant's facilities for passing command line arguments. For example, to call aapt to generate R.java :

<target name="resource-src" description="Generate the R.java file for this project's resources.">
	<android:aapt>
		<arg value="package"/>
		<arg value="-m"/>
		<arg value="-J"/>
		<arg file="src"/> <!-- Create R.java in the source directory -->
		<arg value="-M"/>
		<arg file="AndroidManifest.xml"/>
		<arg value="-S"/>
		<arg file="res"/>
		<arg value="-I"/>
		<arg file="${android-home}/android.jar"/>
	</android:aapt>
</target>

This has advantages (clarity about what's getting run) and drawbacks. Particularly it is difficult to pass filesets around. So far, the only place this has been an issue is when calling dx with a set of jar dependencies to dex with the compiled code. So, <android:dx> provides an additional inputref attribute:

<target name="dex" depends="compile-main" description="Convert the .class files into .dex files.">
	<property name="classes.dex" location="target/classes.dex"/>
	<android:dx inputref="main.jars">
		<arg value="--dex"/>
		<arg value="--output=${classes.dex}"/>
		<arg path="target/main-classes"/>
	</android:dx>
</target>

inputref is refid to an existing fileset, and indicates which jars to dex with the contents of target/main-classes. Here, the fileset might be

<fileset dir="lib" id="main.jars">
	<include name="positron.jar"/>
</fileset>

Controlling an Emulator

An automated build is pretty useless if it can't run your test suite (you do have a test suite, right? ;-) ). A healthy project will probably need more than vanilla unit tests, which means it will need access to an android device. Android-ant lets you start and stop an emulator to fill this need:

<target name="start-emulator" description="Start an emulator.">
	<android:adb><arg value="start-server"/></android:adb>
	<android:start-emulator>
		<arg line="-skin 320x480 -no-boot-anim"/>
	</android:start-emulator>
</target>

<target name="stop-emulator" description="Stop the emulator we started.">
	<android:stop-emulator/>
</target>

<android:start-emulator> starts an emulator that runs in tandem with your build. The task will block long enough to ensure that, once it returns, you can immediately install an application if wish. Once it does you may do so or fire up any battery of tests you wish. When you're done with the emulator, call <android:stop-emulator/> to turn it off. Should the build fail or otherwise end unexpectedly, the emulator will turn off automatically.

Known Issues

Unfortunately <android:start-emulator> is not yet rock solid. The started emulator occasionally fails to register itself with any running adb daemon, meaning it won't be found by adb when you go to install an application or run tests. It's also difficult to fail early and loudly when this happens: I'm still looking at ways to solve or cope with the problem.

Comment by yute925@gmail.com, Feb 25, 2009

Hi, I read the article and try to run the "notepad" project in Eclipse Ver3.3, but I got those message: ... package-apk:

java? Unable to access jarfile ..\framework\apkbuilder.jar

BUILD FAILED E:\Develop_Tools?\Java\Android_SDK\sample\autoAndroid\samples\notepad\build.xml:78: java.lang.RuntimeException?: Tool return 1 ...

Could you give me some hit for how to fix this problem? Thanks

Best Regards, -Victor

Comment by carl.wha...@gmail.com, Mar 10, 2009

Yes I get this too, a crude workaround just to get the build to work is to create this mysterious ..\framework folder yourself and copy %ANDROID_HOME%\tools\lib to it. Ugh!

Comment by azadbol...@bolour.com, Apr 29, 2009

Hi,

Thanks for the sample notepad application. It worked beautifully out of the box.

I just upgraded to Android 1.5, and tried the ant conversion tool they provide:

android update project --path C:\dev\samples\android\notepad --target 1

Unfortunately, most of what was in your sample build.xml seems to have been ignored by this update command.

Have you by any chance converted your build.xml for the notepad sample to android 1.5 so that it again goes through the entire process of building, installing, and running?

If so, I would really appreciate a copy. If not, any hints on how to convert will be much appreciated.

Many thanks.

Azad

Comment by rishi.ob...@gmail.com, Aug 2, 2009

I am using the start-emulator target(as mentioned above ) in my project. But on running the build, I am getting the following error : "The prefix "android" for element "android:adb" is not bound."

Please suggest if I am missing something here.

Thanks in advance.

Rishi

Comment by plaacebo...@gmail.com, Sep 2, 2009

start-emulator:

java? java.net.SocketTimeoutException?: Accept timed out java? at java.net.PlainSocketImpl?.socketAccept(Native Method) java? at java.net.PlainSocketImpl?.accept(Unknown Source) java? at java.net.ServerSocket?.implAccept(Unknown Source) java? at java.net.ServerSocket?.accept(Unknown Source) java? at startEmulator$1.run(startEmulator.java:61) java? at java.lang.Thread.run(Unknown Source)

Comment by nglauber...@gmail.com, Sep 11, 2009

Hi Guys,

Is this tool compatible with the version 1.5 of Android SDK?

Regards, Glauber

Comment by wlyoum...@gmail.com, Feb 8, 2010

Hi, I'm not sure if anyone monitors this thread, but I have a problem with the app build file. I am using android 2.1 and eclipse for java with the ADT installed:

(Verbatim from build.xml - as above)

	<target name="dex" depends="compile-main" description="Convert the .class files into .dex files.">
	        <property name="classes.dex" location="target/classes.dex"/>
	        <android:dx inputref="main.jars">
	                <arg value="--dex"/>
	                <arg value="--output=${classes.dex}"/>
	                <arg path="target/main-classes"/>
	        </android:dx>
	</target>

This results in the error:

Buildfile: C:\Users\Public\Android\svn\Notepad\build.xml resource-src: compile-main:

javac? Compiling 1 source file to C:\Users\Public\Android\svn\Notepad\target\main-classes
dex:
java? =C:\Users\Public\Android\svn\Notepad\target\classes.dex" "C:\Users\Public\Android\svn\Notepad\target\main-classes" "C:\Users\Public\Android\svn\Notepad\lib\positron.jar"=="" goto endArgs was unexpected at this time.

BUILD FAILED C:\Users\Public\Android\svn\Notepad\build.xml:54: java.lang.RuntimeException?: Tool return 255

Total time: 1 second

The dx target seems to be the only one I am having problems with. Any suggestions would be appreciated.

Comment by petersch...@gmail.com, Feb 21, 2010

Buildfile: C:\Users\peter\androidworkspace\notepad\build.xml clean: resource-src:

BUILD FAILED C:\Users\peter\androidworkspace\notepad\build.xml:27: java.lang.NullPointerException?

Total time: 577 milliseconds

Comment by ganesans...@gmail.com, Mar 8, 2010

Yes I get this same, i got the following error while running the IDE, BUILD FAILED D:\Ganesan Shanmugam\March_WS\notepad\build.xml:27: java.lang.NullPointerException?

Total time: 234 milliseconds


BUILD FAILED java.lang.NullPointerException?

at com.googlecode.autoandroid.lib.AndroidTools?.locateTool(AndroidTools?.java:110) at com.googlecode.autoandroid.lib.AndroidTools?.startTool(AndroidTools?.java:103) at com.googlecode.autoandroid.lib.AndroidTools?.startTool(AndroidTools?.java:91) at com.googlecode.autoandroid.lib.WindowsAndroidTools?.aapt(WindowsAndroidTools?.java:43) at aapt.main(aapt.java:7) at sun.reflect.NativeMethodAccessorImpl?.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl?.invoke(NativeMethodAccessorImpl?.java:39) at sun.reflect.DelegatingMethodAccessorImpl?.invoke(DelegatingMethodAccessorImpl?.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.tools.ant.taskdefs.ExecuteJava?.run(ExecuteJava?.java:217) at org.apache.tools.ant.taskdefs.ExecuteJava?.execute(ExecuteJava?.java:152) at org.apache.tools.ant.taskdefs.Java.run(Java.java:764) at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:218) at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:132) at org.apache.tools.ant.taskdefs.Java.execute(Java.java:105) at org.apache.tools.ant.UnknownElement?.execute(UnknownElement?.java:288) at sun.reflect.NativeMethodAccessorImpl?.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl?.invoke(NativeMethodAccessorImpl?.java:39) at sun.reflect.DelegatingMethodAccessorImpl?.invoke(DelegatingMethodAccessorImpl?.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.tools.ant.dispatch.DispatchUtils?.execute(DispatchUtils?.java:106) at org.apache.tools.ant.Task.perform(Task.java:348) at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:62) at org.apache.tools.ant.UnknownElement?.execute(UnknownElement?.java:288) at sun.reflect.NativeMethodAccessorImpl?.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl?.invoke(NativeMethodAccessorImpl?.java:39) at sun.reflect.DelegatingMethodAccessorImpl?.invoke(DelegatingMethodAccessorImpl?.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.tools.ant.dispatch.DispatchUtils?.execute(DispatchUtils?.java:106) at org.apache.tools.ant.Task.perform(Task.java:348) at org.apache.tools.ant.taskdefs.MacroInstance?.execute(MacroInstance?.java:394) at org.apache.tools.ant.UnknownElement?.execute(UnknownElement?.java:288) at sun.reflect.NativeMethodAccessorImpl?.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl?.invoke(NativeMethodAccessorImpl?.java:39) at sun.reflect.DelegatingMethodAccessorImpl?.invoke(DelegatingMethodAccessorImpl?.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.tools.ant.dispatch.DispatchUtils?.execute(DispatchUtils?.java:106) at org.apache.tools.ant.Task.perform(Task.java:348) at org.apache.tools.ant.Target.execute(Target.java:357) at org.apache.tools.ant.Target.performTasks(Target.java:385) at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337) at org.apache.tools.ant.Project.executeTarget(Project.java:1306) at org.apache.tools.ant.helper.DefaultExecutor?.executeTargets(DefaultExecutor?.java:41) at org.apache.tools.ant.Project.executeTargets(Project.java:1189) at org.apache.tools.ant.Main.runBuild(Main.java:758) at org.apache.tools.ant.Main.startAnt(Main.java:217) at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257) at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)

i set the ANDROID_HOME=.../android-sdk-windows/tools

Comment by librain....@gmail.com, Apr 17, 2010

Hello All,

I am trying to create a new project of Android using Netbeans 6.5. I have installed the neccessary plugins and also download the SDK Starter and have installed the android platform 2.1

Then from the netbeans i simply created a new Android Project and have set up the platform for Android from the netbeans. When i run the project i simply get the following errors: Any help would be be very helpful:

init: deps-jar: Compiling 2 source files to C:\Documents and Settings\Administrator\My Documents\NetBeansProjects?\AndroidApplication?\build\classes compile: Created dir: C:\Documents and Settings\Administrator\My Documents\NetBeansProjects?\AndroidApplication?\dist =C:\Documents was unexpected at this time. C:\Documents and Settings\Administrator\My Documents\NetBeansProjects?\AndroidApplication?\nbproject\build-impl.xml:411: exec returned: 255 BUILD FAILED (total time: 0 seconds)

Comment by ricardoo...@gmail.com, Apr 19, 2010

Hello All,

I have the same problem. build-impl.xml:411: exec returned: 255 BUILD FAILED

Comment by wk300...@gmail.com, May 6, 2010

It seems like it's reading 'C:\Documents ' and stopped I have tried changing the application directory to somewhere with no space and it works.

Comment by forraj...@gmail.com, Sep 6, 2010

Thanks ...this trick even worked for me also:)

Comment by shehzadh...@gmail.com, Sep 13, 2010

I cannot change the application directory to no space. Any other solution to this?

Comment by tylercol...@gmail.com, Sep 28, 2010

I too had to put my project into a path with no spaces before the project would compile.

Comment by aleri...@gmail.com, Oct 1, 2010

I am creating a project in Android 2.2. When I run android:aapt I get the error "Could not find aapt. Make sure you have it in your classpath". It seems pretty obvious that this is due to aapt being moved out of <androidSDK>/tools and into <androidSDK>/platforms/<platform>/tools. Part of my reason for switching to ant is to be able to create separate 2.1 & 2.2 versions. How do recommend I address this effectively?

Comment by inbox.si...@gmail.com, Oct 9, 2010
"=="" goto endArgs was unexpected at this
BUILD FAILED C:\Users\Public\Android\svn\Notepad\build.xml:54: java.lang.RuntimeException??: Tool return 255

I have suffered from the same issue but found out that I can if not remove it can atlease bypass over it. You can do the same by trying using the following code in your dx.bat file in the {android-home}/tools directory. Open the file with notepad and add the following lines :

REM Added as bug fix for the ant build
echo SKIP check the parameter set 
args=%params% goto endArgs

Just after the line that says:

:nextArg

I hope that helped you out too.

Thanks

Comment by 186...@gmail.com, Dec 8, 2010

hi,i have a issue now: i cannot find how can i put a libs/.so file into apk while packaging with ANT, any suggestions?

Comment by asif.rul...@gmail.com, Mar 1, 2011

The build unsuccessful error got solved by moving project to a location where there is no space in file path

Comment by talktoma...@gmail.com, Apr 18, 2011

Hi, I getting Can't find aapt.exe inside the sdk. Reason may be it was earlier in tools folder but now it is in platform-tools.

Comment by philip.j...@gmail.com, Apr 25, 2011

Hi, I am trying to build my android project on a linux box using ant but it fails giving me this:

Buildfile: build.xml
BUILD FAILED file:/var/www/html/agora_proto/Paketo/players/TestApp?/build.xml:37: Unexpected element "import"
Total time: 0 seconds

I have ant version 1.5.2-26 installed. Does anyone know why this is failing? Import is a fairly standard element in ant files, or so I thought.

Thanks!

Comment by azhaoxu...@gmail.com, Dec 7, 2011

how about the .so file just in libs/armeabi dir? apkbuilder -nl .libs/armeabi ,but it does not in the apk.Can you give some suggestion?

Comment by Anastasi...@gmail.com, Jan 12, 2012

Good article, thanx! http://www.enterra-inc.com/techzone/ - may be useful,too.


Sign in to add a comment
Powered by Google Project Hosting