1 <?xml version="1.0" encoding="UTF-8"?>
2 <project name="android_rules" default="debug">
5 This rules file is meant to be imported by the custom Ant task:
6 com.android.ant.SetupTask
8 The following properties are put in place by the importing task:
9 android.jar, android.aidl, aapt, aidl, and dx
11 Additionnaly, the task sets up the following classpath reference:
12 android.target.classpath
13 This is used by the compiler task as the boot classpath.
18 classname="com.android.ant.AaptExecLoopTask"
19 classpathref="android.antlibs" />
22 classname="com.android.ant.AidlExecTask"
23 classpathref="android.antlibs" />
25 <taskdef name="apkbuilder"
26 classname="com.android.ant.ApkBuilderTask"
27 classpathref="android.antlibs" />
30 classname="com.android.ant.XPathTask"
31 classpathref="android.antlibs" />
34 classname="com.android.ant.IfElseTask"
35 classpathref="android.antlibs" />
39 <!-- Tells adb which device to target. You can change this from the command line
40 by invoking "ant -Dadb.device.arg=-d" for device "ant -Dadb.device.arg=-e" for
42 <property name="adb.device.arg" value="" />
44 <property name="android.tools.dir" location="${sdk.dir}/tools" />
45 <!-- Name of the application package extracted from manifest file -->
46 <xpath input="AndroidManifest.xml" expression="/manifest/@package"
47 output="manifest.package" />
48 <!-- Value of the debuggable attribute (Application node) extracted from manifest file -->
49 <xpath input="AndroidManifest.xml" expression="/manifest/application/@android:debuggable"
50 output="manifest.debuggable" default="false"/>
51 <!-- Value of the debuggable attribute (Application node) extracted from manifest file -->
52 <xpath input="AndroidManifest.xml" expression="/manifest/application/@android:hasCode"
53 output="manifest.hasCode" default="true"/>
55 <!-- Input directories -->
56 <property name="source.dir" value="src" />
57 <property name="source.absolute.dir" location="${source.dir}" />
58 <property name="gen.dir" value="gen" />
59 <property name="gen.absolute.dir" location="${gen.dir}" />
60 <property name="resource.dir" value="res" />
61 <property name="resource.absolute.dir" location="${resource.dir}" />
62 <property name="asset.dir" value="assets" />
63 <property name="asset.absolute.dir" location="${asset.dir}" />
65 <!-- Directory for the third party java libraries -->
66 <property name="external.libs.dir" value="libs" />
67 <property name="external.libs.absolute.dir" location="${external.libs.dir}" />
69 <!-- Directory for the native libraries -->
70 <property name="native.libs.dir" value="libs" />
71 <property name="native.libs.absolute.dir" location="${native.libs.dir}" />
73 <!-- Output directories -->
74 <property name="out.dir" value="bin" />
75 <property name="out.absolute.dir" location="${out.dir}" />
76 <property name="out.classes.dir" value="${out.absolute.dir}/classes" />
77 <property name="out.classes.absolute.dir" location="${out.classes.dir}" />
78 <property name="out.dex.input.absolute.dir" value="${out.classes.absolute.dir}" />
80 <!-- Intermediate files -->
81 <property name="dex.file.name" value="classes.dex" />
82 <property name="intermediate.dex.file"
83 location="${out.absolute.dir}/${dex.file.name}" />
84 <property name="resource.package.file.name"
85 value="${ant.project.name}.ap_" />
87 <!-- The final package file to generate
88 These can be overridden by setting them earlier to
90 <property name="out.debug.unaligned.file"
91 location="${out.absolute.dir}/${ant.project.name}-debug-unaligned.apk" />
92 <property name="out.debug.file"
93 location="${out.absolute.dir}/${ant.project.name}-debug.apk" />
95 <property name="out.unsigned.file.name"
96 value="${ant.project.name}-unsigned.apk" />
97 <property name="out.unsigned.file"
98 location="${out.absolute.dir}/${out.unsigned.file.name}" />
100 <property name="out.unaligned.file.name"
101 value="${ant.project.name}-unaligned.apk" />
102 <property name="out.unaligned.file"
103 location="${out.absolute.dir}/${out.unaligned.file.name}" />
105 <property name="out.release.file.name"
106 value="${ant.project.name}-release.apk" />
107 <property name="out.release.file"
108 location="${out.absolute.dir}/${out.release.file.name}" />
110 <!-- set some properties used for filtering/override. If those weren't defined
111 before, then this will create them with empty values, which are then ignored
112 by the custom tasks receiving them. -->
113 <property name="version.code" value="" />
114 <property name="aapt.resource.filter" value="" />
115 <property name="filter.abi" value="" />
118 <property name="verbose" value="false" />
119 <!-- This is needed by emma as it uses multilevel verbosity instead of simple 'true' or 'false'
120 The property 'verbosity' is not user configurable and depends exclusively on 'verbose'
122 <condition property="verbosity" value="verbose" else="quiet">
123 <istrue value="${verbose}" />
125 <!-- This is needed to switch verbosity of zipalign. Depends exclusively on 'verbose'
127 <condition property="v.option" value="-v" else="">
128 <istrue value="${verbose}" />
130 <!-- This is needed to switch verbosity of dx. Depends exclusively on 'verbose' -->
131 <condition property="verbose.option" value="--verbose" else="">
132 <istrue value="${verbose}" />
135 <!-- properties for signing in release mode -->
136 <condition property="has.keystore">
138 <isset property="key.store" />
139 <length string="${key.store}" when="greater" length="0" />
140 <isset property="key.alias" />
143 <condition property="has.password">
145 <isset property="has.keystore" />
146 <isset property="key.store.password" />
147 <isset property="key.alias.password" />
152 <condition property="exe" value=".exe" else=""><os family="windows" /></condition>
153 <property name="adb" location="${android.tools.dir}/adb${exe}" />
154 <property name="zipalign" location="${android.tools.dir}/zipalign${exe}" />
156 <!-- Emma configuration -->
157 <property name="emma.dir" value="${sdk.dir}/tools/lib" />
159 <pathelement location="${emma.dir}/emma.jar" />
160 <pathelement location="${emma.dir}/emma_ant.jar" />
162 <taskdef resource="emma_ant.properties" classpathref="emma.lib" />
163 <!-- End of emma configuration -->
167 <!-- Configurable macro, which allows to pass as parameters output directory,
168 output dex filename and external libraries to dex (optional) -->
169 <macrodef name="dex-helper">
170 <element name="external-libs" optional="yes" />
171 <element name="extra-parameters" optional="yes" />
173 <echo>Converting compiled files and external libraries into ${intermediate.dex.file}...</echo>
174 <apply executable="${dx}" failonerror="true" parallel="true">
175 <arg value="--dex" />
176 <arg value="--output=${intermediate.dex.file}" />
178 <arg line="${verbose.option}" />
179 <arg path="${out.dex.input.absolute.dir}" />
180 <fileset dir="${external.libs.absolute.dir}" includes="*.jar" />
181 <path refid="android.libraries.jars" />
187 <!-- This is macro that enable passing variable list of external jar files to ApkBuilder
189 <package-helper sign.package="true" output.filepath="/path/to/foo.apk">
191 <jarfolder path="my_jars" />
192 <jarfile path="foo/bar.jar" />
193 <jarfolder path="your_jars" />
195 </package-helper> -->
196 <macrodef name="package-helper">
197 <attribute name="sign.package" />
198 <attribute name="output.filepath" />
199 <element name="extra-jars" optional="yes" />
202 outfolder="${out.absolute.dir}"
203 resourcefile="${resource.package.file.name}"
204 apkfilepath="@{output.filepath}"
205 signed="@{sign.package}"
206 debug="${manifest.debuggable}"
207 abifilter="${filter.abi}"
209 hascode="${manifest.hasCode}">
210 <dex path="${intermediate.dex.file}"/>
211 <sourcefolder path="${source.absolute.dir}"/>
212 <sourcefolder refid="android.libraries.src"/>
213 <jarfolder path="${external.libs.absolute.dir}" />
214 <jarfolder refid="android.libraries.libs" />
215 <nativefolder path="${native.libs.absolute.dir}" />
216 <nativefolder refid="android.libraries.libs" />
222 <!-- This is macro which zipaligns in.package and outputs it to out.package. Used by targets
223 debug, -debug-with-emma and release.-->
224 <macrodef name="zipalign-helper">
225 <attribute name="in.package" />
226 <attribute name="out.package" />
228 <echo>Running zip align on final apk...</echo>
229 <exec executable="${zipalign}" failonerror="true">
230 <arg line="${v.option}" />
233 <arg path="@{in.package}" />
234 <arg path="@{out.package}" />
239 <!-- This is macro used only for sharing code among two targets, -install and
240 -install-with-emma which do exactly the same but differ in dependencies -->
241 <macrodef name="install-helper">
243 <echo>Installing ${out.debug.file} onto default emulator or device...</echo>
244 <exec executable="${adb}" failonerror="true">
245 <arg line="${adb.device.arg}" />
246 <arg value="install" />
248 <arg path="${out.debug.file}" />
255 <!-- Creates the output directories if they don't exist yet. -->
256 <target name="-dirs">
257 <echo>Creating output directories if needed...</echo>
258 <mkdir dir="${resource.absolute.dir}" />
259 <mkdir dir="${external.libs.absolute.dir}" />
260 <mkdir dir="${out.absolute.dir}" />
261 <if condition="${manifest.hasCode}">
263 <mkdir dir="${gen.absolute.dir}" />
264 <mkdir dir="${out.classes.absolute.dir}" />
269 <!-- empty default pre-build target. Create a similar target in
270 your build.xml and it'll be called instead of this one. -->
271 <target name="-pre-build"/>
273 <!-- Generates the R.java file for this project's resources. -->
274 <target name="-resource-src" depends="-dirs, -pre-build">
275 <if condition="${manifest.hasCode}">
277 <echo>Generating R.java / Manifest.java from the resources...</echo>
278 <aapt executable="${aapt}"
281 manifest="AndroidManifest.xml"
282 androidjar="${android.jar}"
283 rfolder="${gen.absolute.dir}">
284 <res path="${resource.absolute.dir}" />
288 <echo>hasCode = false. Skipping...</echo>
293 <!-- Generates java classes from .aidl files. -->
294 <target name="-aidl" depends="-dirs">
295 <if condition="${manifest.hasCode}">
297 <echo>Compiling aidl files into Java classes...</echo>
298 <aidl executable="${aidl}" framework="${android.aidl}"
299 genFolder="${gen.absolute.dir}">
300 <source path="${source.absolute.dir}"/>
301 <source refid="android.libraries.src"/>
305 <echo>hasCode = false. Skipping...</echo>
310 <!-- empty default pre-compile target. Create a similar target in
311 your build.xml and it'll be called instead of this one. -->
312 <target name="-pre-compile"/>
314 <!-- Compiles this project's .java files into .class files. -->
315 <target name="compile" depends="-resource-src, -aidl, -pre-compile"
316 description="Compiles project's .java files into .class files">
317 <if condition="${manifest.hasCode}">
319 <!-- If android rules are used for a test project, its classpath should include
320 tested project's location -->
321 <condition property="extensible.classpath"
322 value="${tested.project.absolute.dir}/bin/classes"
324 <isset property="tested.project.absolute.dir" />
326 <condition property="extensible.libs.classpath"
327 value="${tested.project.absolute.dir}/libs"
329 <isset property="tested.project.absolute.dir" />
331 <javac encoding="ascii" target="1.5" debug="true" extdirs=""
332 destdir="${out.classes.absolute.dir}"
333 bootclasspathref="android.target.classpath"
335 classpath="${extensible.classpath}"
336 classpathref="android.libraries.jars">
337 <src path="${source.absolute.dir}" />
338 <src path="${gen.absolute.dir}" />
339 <src refid="android.libraries.src" />
341 <fileset dir="${external.libs.absolute.dir}" includes="*.jar" />
342 <fileset dir="${extensible.libs.classpath}" includes="*.jar" />
347 <echo>hasCode = false. Skipping...</echo>
352 <!-- empty default post-compile target. Create a similar target in
353 your build.xml and it'll be called instead of this one. -->
354 <target name="-post-compile"/>
356 <!-- Converts this project's .class files into .dex files -->
357 <target name="-dex" depends="compile, -post-compile"
358 unless="do.not.compile">
359 <if condition="${manifest.hasCode}">
364 <echo>hasCode = false. Skipping...</echo>
369 <!-- Puts the project's resources into the output package file
370 This actually can create multiple resource package in case
371 Some custom apk with specific configuration have been
372 declared in default.properties.
374 <target name="-package-resources">
375 <echo>Packaging resources</echo>
376 <aapt executable="${aapt}"
378 versioncode="${version.code}"
379 manifest="AndroidManifest.xml"
380 assets="${asset.absolute.dir}"
381 androidjar="${android.jar}"
382 apkfolder="${out.absolute.dir}"
383 resourcefilename="${resource.package.file.name}"
384 resourcefilter="${aapt.resource.filter}">
385 <res path="${resource.absolute.dir}" />
386 <!-- <nocompress /> forces no compression on any files in assets or res/raw -->
387 <!-- <nocompress extension="xml" /> forces no compression on specific file extensions in assets and res/raw -->
391 <!-- Packages the application and sign it with a debug key. -->
392 <target name="-package-debug-sign" depends="-dex, -package-resources">
395 output.filepath="${out.debug.unaligned.file}" />
398 <!-- Packages the application without signing it. -->
399 <target name="-package-release" depends="-dex, -package-resources">
402 output.filepath="${out.unsigned.file}"/>
405 <target name="-compile-tested-if-test" if="tested.project.dir" unless="do.not.compile.again">
406 <subant target="compile">
407 <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
411 <!-- Builds debug output package, provided all the necessary files are already dexed -->
412 <target name="debug" depends="-compile-tested-if-test, -package-debug-sign"
413 description="Builds the application and signs it with a debug key.">
414 <zipalign-helper in.package="${out.debug.unaligned.file}"
415 out.package="${out.debug.file}" />
416 <echo>Debug Package: ${out.debug.file}</echo>
419 <!-- called through target 'release'. Only executed if the keystore and
420 key alias are known but not their password. -->
421 <target name="-release-prompt-for-password" if="has.keystore" unless="has.password">
422 <!-- Gets passwords -->
424 message="Please enter keystore password (store:${key.store}):"
425 addproperty="key.store.password" />
427 message="Please enter password for alias '${key.alias}':"
428 addproperty="key.alias.password" />
431 <!-- called through target 'release'. Only executed if there's no
432 keystore/key alias set -->
433 <target name="-release-nosign" unless="has.keystore">
434 <echo>No key.store and key.alias properties found in build.properties.</echo>
435 <echo>Please sign ${out.unsigned.file} manually</echo>
436 <echo>and run zipalign from the Android SDK tools.</echo>
439 <!-- This runs -package-release and -release-nosign first and then runs
440 only if release-sign is true (set in -release-check,
441 called by -release-no-sign)-->
442 <target name="release"
443 depends="-package-release, -release-prompt-for-password, -release-nosign"
445 description="Builds the application. The generated apk file must be signed before
447 <!-- Signs the APK -->
448 <echo>Signing final apk...</echo>
450 jar="${out.unsigned.file}"
451 signedjar="${out.unaligned.file}"
452 keystore="${key.store}"
453 storepass="${key.store.password}"
455 keypass="${key.alias.password}"
456 verbose="${verbose}" />
458 <!-- Zip aligns the APK -->
459 <zipalign-helper in.package="${out.unaligned.file}"
460 out.package="${out.release.file}" />
461 <echo>Release Package: ${out.release.file}</echo>
464 <target name="install" depends="debug"
465 description="Installs/reinstalls the debug package onto a running
466 emulator or device. If the application was previously installed,
467 the signatures must match." >
471 <target name="-uninstall-check">
472 <condition property="uninstall.run">
473 <isset property="manifest.package" />
477 <target name="-uninstall-error" depends="-uninstall-check" unless="uninstall.run">
478 <echo>Unable to run 'ant uninstall', manifest.package property is not defined.
482 <!-- Uninstalls the package from the default emulator/device -->
483 <target name="uninstall" depends="-uninstall-error" if="uninstall.run"
484 description="Uninstalls the application from a running emulator or device.">
485 <echo>Uninstalling ${manifest.package} from the default emulator or device...</echo>
486 <exec executable="${adb}" failonerror="true">
487 <arg line="${adb.device.arg}" />
488 <arg value="uninstall" />
489 <arg value="${manifest.package}" />
493 <target name="clean" description="Removes output files created by other targets.">
494 <delete dir="${out.absolute.dir}" verbose="${verbose}" />
495 <delete dir="${gen.absolute.dir}" verbose="${verbose}" />
498 <!-- Targets for code-coverage measurement purposes, invoked from external file -->
500 <!-- Emma-instruments tested project classes (compiles the tested project if necessary)
501 and writes instrumented classes to ${instrumentation.absolute.dir}/classes -->
502 <target name="-emma-instrument" depends="compile">
503 <echo>Instrumenting classes from ${out.absolute.dir}/classes...</echo>
504 <!-- It only instruments class files, not any external libs -->
505 <emma enabled="true">
506 <instr verbosity="${verbosity}"
508 instrpath="${out.absolute.dir}/classes"
509 outdir="${out.absolute.dir}/classes">
511 <!-- TODO: exclusion filters on R*.class and allowing custom exclusion from
512 user defined file -->
516 <target name="-dex-instrumented" depends="-emma-instrument">
519 <arg value="--no-locals" />
522 <fileset file="${emma.dir}/emma_device.jar" />
527 <!-- Invoked from external files for code coverage purposes -->
528 <target name="-package-with-emma" depends="-dex-instrumented, -package-resources">
531 output.filepath="${out.debug.unaligned.file}">
533 <!-- Injected from external file -->
534 <jarfile path="${emma.dir}/emma_device.jar" />
539 <target name="-debug-with-emma" depends="-package-with-emma">
540 <zipalign-helper in.package="${out.debug.unaligned.file}"
541 out.package="${out.debug.file}" />
544 <target name="-install-with-emma" depends="-debug-with-emma">
548 <!-- End of targets for code-coverage measurement purposes -->
551 <!-- displays starts at col 13
553 <echo>Android Ant Build. Available targets:</echo>
554 <echo> help: Displays this help.</echo>
555 <echo> clean: Removes output files created by other targets.</echo>
556 <echo> compile: Compiles project's .java files into .class files.</echo>
557 <echo> debug: Builds the application and signs it with a debug key.</echo>
558 <echo> release: Builds the application. The generated apk file must be</echo>
559 <echo> signed before it is published.</echo>
560 <echo> install: Installs/reinstalls the debug package onto a running</echo>
561 <echo> emulator or device.</echo>
562 <echo> If the application was previously installed, the</echo>
563 <echo> signatures must match.</echo>
564 <echo> uninstall: Uninstalls the application from a running emulator or</echo>
565 <echo> device.</echo>