|
AndroidAnt
Build your Android application with ant.. without <exec>
OverviewIt'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 TimeLet'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 antlibFirst, 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.jarWe 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 ToolsCalling 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 EmulatorAn 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 IssuesUnfortunately <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. |
Sign in to add a comment
Hi, I read the article and try to run the "notepad" project in Eclipse Ver3.3, but I got those message: ... package-apk:
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
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!
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
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.
start-emulator:
Hi Guys,
Is this tool compatible with the version 1.5 of Android SDK?
Regards, Glauber