1<?xml version="1.0" encoding="UTF-8"?>
2<project name="android_rules" default="debug">
3
4    <!--
5        This build file is imported by the project build file. It contains
6        all the targets and tasks necessary to build Android projects, be they
7        regular projects, library projects, or test projects.
8
9        At the beginning of the file is a list of properties that can be overridden
10        by adding them to your ant.properties (properties are immutable, so their
11        first definition sticks and is never changed).
12
13        Follows:
14        - custom task definitions,
15        - more properties (do not override those unless the whole build system is modified).
16        - macros used throughout the build,
17        - base build targets,
18        - debug-specific build targets,
19        - release-specific build targets,
20        - instrument-specific build targets,
21        - test project-specific build targets,
22        - install targets,
23        - help target
24    -->
25
26    <!-- ******************************************************* -->
27    <!-- **************** Overridable Properties *************** -->
28    <!-- ******************************************************* -->
29
30    <!-- You can override these values in your build.xml or ant.properties.
31         Overriding any other properties may result in broken build. -->
32
33    <!-- Tells adb which device to target. You can change this from the command line
34         by invoking "ant -Dadb.device.arg=-d" for device "ant -Dadb.device.arg=-e" for
35         the emulator. -->
36    <property name="adb.device.arg" value="" />
37
38    <!-- fileset exclude patterns (space separated) to prevent
39         files inside src/ from being packaged. -->
40    <property name="android.package.excludes" value="" />
41
42    <!-- set some properties used for filtering/override. If those weren't defined
43         before, then this will create them with empty values, which are then ignored
44         by the custom tasks receiving them. -->
45    <property name="version.code" value="" />
46    <property name="version.name" value="" />
47    <property name="aapt.resource.filter" value="" />
48    <!-- 'aapt.ignore.assets' is the list of file patterns to ignore under /res and /assets.
49         Default is "!.svn:!.git:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"
50
51         Overall patterns syntax is:
52           [!][<dir>|<file>][*suffix-match|prefix-match*|full-match]:more:patterns...
53
54         - The first character flag ! avoids printing a warning.
55         - Pattern can have the flag "<dir>" to match only directories
56           or "<file>" to match only files. Default is to match both.
57         - Match is not case-sensitive.
58    -->
59    <property name="aapt.ignore.assets" value="" />
60
61    <!-- dex force jumbo options, to be used when dex merging fails with
62         UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dx.util.DexException: Cannot handle conversion to jumbo index!
63           at com.android.dx.merge.InstructionTransformer.jumboCheck(InstructionTransformer.java:103)
64           ...
65    -->
66    <property name="dex.force.jumbo" value="false" />
67    <property name="dex.disable.merger" value="false" />
68
69    <!-- compilation options -->
70    <property name="java.encoding" value="UTF-8" />
71    <property name="java.target" value="1.5" />
72    <property name="java.source" value="1.5" />
73    <property name="java.compilerargs" value="" />
74    <property name="java.compiler.classpath" value="" />
75
76    <!-- Renderscript options -->
77    <property name="renderscript.debug.opt.level" value="O0" />
78    <property name="renderscript.release.opt.level" value="O3" />
79
80    <!-- manifest merger default value -->
81    <property name="manifestmerger.enabled" value="false" />
82
83    <!-- instrumentation options -->
84    <property name="emma.filter" value="" />
85
86    <!-- Verbosity -->
87    <property name="verbose" value="false" />
88
89    <!-- Output location of the HTML report for the "lint" target.
90         Ideally this would be specified as
91            value="${out.dir}/lint-results.html"
92         but we can't make a forward reference to the definition for
93         ${out.dir}, and it is not a configurable property (yet).
94     -->
95    <property name="lint.out.html" value="bin/lint-results.html" />
96
97    <!-- Output location of the XML report for the "lint" target -->
98    <property name="lint.out.xml" value="bin/lint-results.xml" />
99
100    <!-- ******************************************************* -->
101    <!-- ********************* Custom Tasks ******************** -->
102    <!-- ******************************************************* -->
103
104    <!-- jar file from where the tasks are loaded -->
105    <path id="android.antlibs">
106        <pathelement path="${sdk.dir}/tools/lib/ant-tasks.jar" />
107    </path>
108
109    <!-- Custom tasks -->
110    <taskdef resource="anttasks.properties" classpathref="android.antlibs" />
111
112    <!-- Emma configuration -->
113    <property name="emma.dir" value="${sdk.dir}/tools/lib" />
114    <path id="emma.lib">
115        <pathelement location="${emma.dir}/emma.jar" />
116        <pathelement location="${emma.dir}/emma_ant.jar" />
117    </path>
118    <taskdef resource="emma_ant.properties" classpathref="emma.lib" />
119    <!-- End of emma configuration -->
120
121
122    <!-- ******************************************************* -->
123    <!-- ******************* Other Properties ****************** -->
124    <!-- ******************************************************* -->
125    <!-- overriding these properties may break the build
126         unless the whole file is updated -->
127
128    <!-- Input directories -->
129    <property name="source.dir" value="src" />
130    <property name="source.absolute.dir" location="${source.dir}" />
131    <property name="gen.absolute.dir" location="gen" />
132    <property name="resource.absolute.dir" location="res" />
133    <property name="asset.dir" value="assets" />
134    <property name="asset.absolute.dir" location="${asset.dir}" />
135    <property name="jar.libs.dir" value="libs" />
136    <property name="jar.libs.absolute.dir" location="${jar.libs.dir}" />
137    <property name="native.libs.absolute.dir" location="libs" />
138
139    <property name="manifest.file" value="AndroidManifest.xml" />
140    <property name="manifest.abs.file" location="${manifest.file}" />
141
142    <!-- Output directories -->
143    <property name="out.dir" value="bin" />
144    <property name="out.absolute.dir" location="${out.dir}" />
145    <property name="out.classes.absolute.dir" location="${out.dir}/classes" />
146    <property name="out.res.absolute.dir" location="${out.dir}/res" />
147    <property name="out.aidl.absolute.dir" location="${out.dir}/aidl" />
148    <property name="out.dexed.absolute.dir" location="${out.dir}/dexedLibs" />
149    <property name="out.manifest.abs.file" location="${out.dir}/AndroidManifest.xml" />
150
151    <!-- tools location -->
152    <property name="android.tools.dir" location="${sdk.dir}/tools" />
153    <property name="android.platform.tools.dir" location="${sdk.dir}/platform-tools" />
154    <condition property="exe" value=".exe" else=""><os family="windows" /></condition>
155    <condition property="bat" value=".bat" else=""><os family="windows" /></condition>
156    <property name="adb" location="${android.platform.tools.dir}/adb${exe}" />
157    <property name="zipalign" location="${android.tools.dir}/zipalign${exe}" />
158    <property name="lint" location="${android.tools.dir}/lint${bat}" />
159
160    <!-- Intermediate files -->
161    <property name="dex.file.name" value="classes.dex" />
162    <property name="intermediate.dex.file" location="${out.absolute.dir}/${dex.file.name}" />
163    <property name="resource.package.file.name" value="${ant.project.name}.ap_" />
164
165    <!-- Build property file -->
166    <property name="out.build.prop.file" location="${out.absolute.dir}/build.prop" />
167
168
169    <!-- This is needed by emma as it uses multilevel verbosity instead of simple 'true' or 'false'
170         The property 'verbosity' is not user configurable and depends exclusively on 'verbose'
171         value.-->
172    <condition property="verbosity" value="verbose" else="quiet">
173        <istrue value="${verbose}" />
174    </condition>
175
176    <!-- properties for signing in release mode -->
177    <condition property="has.keystore">
178        <and>
179            <isset property="key.store" />
180            <length string="${key.store}" when="greater" length="0" />
181            <isset property="key.alias" />
182        </and>
183    </condition>
184    <condition property="has.password">
185        <and>
186            <isset property="has.keystore" />
187            <isset property="key.store.password" />
188            <isset property="key.alias.password" />
189        </and>
190    </condition>
191
192    <!-- properties for packaging -->
193    <property name="build.packaging.nocrunch" value="true" />
194
195    <!-- whether we need to fork javac.
196         This is only needed on Windows when running Java < 7 -->
197    <condition else="false" property="need.javac.fork">
198        <and>
199            <matches pattern="1\.[56]" string="${java.specification.version}"/>
200            <not>
201                <os family="unix"/>
202            </not>
203        </and>
204    </condition>
205
206    <!-- ******************************************************* -->
207    <!-- ************************ Macros *********************** -->
208    <!-- ******************************************************* -->
209
210    <!-- macro to do a task on if project.is.library is false.
211         elseText attribute is displayed otherwise -->
212    <macrodef name="do-only-if-not-library">
213        <attribute name="elseText" />
214        <element name="task-to-do" implicit="yes" />
215        <sequential>
216        <if condition="${project.is.library}">
217            <else>
218                <task-to-do />
219            </else>
220            <then>
221                <echo level="info">@{elseText}</echo>
222            </then>
223        </if>
224        </sequential>
225    </macrodef>
226
227    <!-- macro to do a task on if manifest.hasCode is true.
228         elseText attribute is displayed otherwise -->
229    <macrodef name="do-only-if-manifest-hasCode">
230        <attribute name="elseText" default=""/>
231        <element name="task-to-do" implicit="yes" />
232        <sequential>
233        <if condition="${manifest.hasCode}">
234            <then>
235                <task-to-do />
236            </then>
237            <else>
238                <if>
239                    <condition>
240                        <length string="@{elseText}" trim="true" when="greater" length="0" />
241                    </condition>
242                    <then>
243                        <echo level="info">@{elseText}</echo>
244                    </then>
245                </if>
246            </else>
247        </if>
248        </sequential>
249    </macrodef>
250
251
252    <!-- Configurable macro, which allows to pass as parameters output directory,
253         output dex filename and external libraries to dex (optional) -->
254    <macrodef name="dex-helper">
255        <element name="external-libs" optional="yes" />
256        <attribute name="nolocals" default="false" />
257        <sequential>
258            <!-- sets the primary input for dex. If a pre-dex task sets it to
259                 something else this has no effect -->
260            <property name="out.dex.input.absolute.dir" value="${out.classes.absolute.dir}" />
261
262            <!-- set the secondary dx input: the project (and library) jar files
263                 If a pre-dex task sets it to something else this has no effect -->
264            <if>
265                <condition>
266                    <isreference refid="out.dex.jar.input.ref" />
267                </condition>
268                <else>
269                    <path id="out.dex.jar.input.ref">
270                        <path refid="project.all.jars.path" />
271                    </path>
272                </else>
273            </if>
274
275            <dex executable="${dx}"
276                    output="${intermediate.dex.file}"
277                    dexedlibs="${out.dexed.absolute.dir}"
278                    nolocals="@{nolocals}"
279                    forceJumbo="${dex.force.jumbo}"
280                    disableDexMerger="${dex.disable.merger}"
281                    verbose="${verbose}">
282                <path path="${out.dex.input.absolute.dir}"/>
283                <path refid="out.dex.jar.input.ref" />
284                <external-libs />
285            </dex>
286        </sequential>
287    </macrodef>
288
289    <!-- This is macro that enable passing variable list of external jar files to ApkBuilder
290         Example of use:
291         <package-helper>
292             <extra-jars>
293                <jarfolder path="my_jars" />
294                <jarfile path="foo/bar.jar" />
295                <jarfolder path="your_jars" />
296             </extra-jars>
297         </package-helper> -->
298    <macrodef name="package-helper">
299        <element name="extra-jars" optional="yes" />
300        <sequential>
301            <apkbuilder
302                    outfolder="${out.absolute.dir}"
303                    resourcefile="${resource.package.file.name}"
304                    apkfilepath="${out.packaged.file}"
305                    debugpackaging="${build.is.packaging.debug}"
306                    debugsigning="${build.is.signing.debug}"
307                    verbose="${verbose}"
308                    hascode="${manifest.hasCode}"
309                    previousBuildType="${build.last.is.packaging.debug}/${build.last.is.signing.debug}"
310                    buildType="${build.is.packaging.debug}/${build.is.signing.debug}">
311                <dex path="${intermediate.dex.file}"/>
312                <sourcefolder path="${source.absolute.dir}"/>
313                <jarfile refid="project.all.jars.path" />
314                <nativefolder path="${native.libs.absolute.dir}" />
315                <nativefolder refid="project.library.native.folder.path" />
316                <extra-jars/>
317            </apkbuilder>
318        </sequential>
319    </macrodef>
320
321    <!-- This is macro which zipaligns in.package and outputs it to out.package. Used by targets
322         debug, -debug-with-emma and release.-->
323    <macrodef name="zipalign-helper">
324        <attribute name="in.package" />
325        <attribute name="out.package" />
326        <sequential>
327            <zipalign
328                    executable="${zipalign}"
329                    input="@{in.package}"
330                    output="@{out.package}"
331                    verbose="${verbose}" />
332        </sequential>
333    </macrodef>
334
335    <macrodef name="run-tests-helper">
336        <attribute name="emma.enabled" default="false" />
337        <element name="extra-instrument-args" optional="yes" />
338        <sequential>
339            <echo level="info">Running tests ...</echo>
340            <exec executable="${adb}" failonerror="true">
341                <arg line="${adb.device.arg}" />
342                <arg value="shell" />
343                <arg value="am" />
344                <arg value="instrument" />
345                <arg value="-w" />
346                <arg value="-e" />
347                <arg value="coverage" />
348                <arg value="@{emma.enabled}" />
349                <extra-instrument-args />
350                <arg value="${project.app.package}/${test.runner}" />
351            </exec>
352        </sequential>
353    </macrodef>
354
355    <macrodef name="record-build-key">
356        <attribute name="key" default="false" />
357        <attribute name="value" default="false" />
358        <sequential>
359            <propertyfile file="${out.build.prop.file}" comment="Last build type">
360                <entry key="@{key}" value="@{value}"/>
361            </propertyfile>
362        </sequential>
363    </macrodef>
364
365    <macrodef name="record-build-info">
366        <sequential>
367            <record-build-key key="build.last.target" value="${build.target}" />
368            <record-build-key key="build.last.is.instrumented" value="${build.is.instrumented}" />
369            <record-build-key key="build.last.is.packaging.debug" value="${build.is.packaging.debug}" />
370            <record-build-key key="build.last.is.signing.debug" value="${build.is.signing.debug}" />
371        </sequential>
372    </macrodef>
373
374    <macrodef name="uninstall-helper">
375        <attribute name="app.package" default="false" />
376        <sequential>
377            <echo level="info">Uninstalling @{app.package} from the default emulator or device...</echo>
378            <exec executable="${adb}" failonerror="true">
379                <arg line="${adb.device.arg}" />
380                <arg value="uninstall" />
381                <arg value="@{app.package}" />
382            </exec>
383        </sequential>
384    </macrodef>
385
386    <!-- ******************************************************* -->
387    <!-- ******************** Build Targets ******************** -->
388    <!-- ******************************************************* -->
389
390    <!-- Basic Ant + SDK check -->
391    <target name="-check-env">
392        <checkenv />
393    </target>
394
395    <!-- target to disable building dependencies -->
396    <target name="nodeps">
397        <property name="dont.do.deps" value="true" />
398    </target>
399
400    <!-- generic setup -->
401    <target name="-setup" depends="-check-env">
402        <echo level="info">Project Name: ${ant.project.name}</echo>
403        <gettype projectTypeOut="project.type" />
404
405        <!-- sets a few boolean based on project.type
406             to make the if task easier -->
407        <condition property="project.is.library" value="true" else="false">
408            <equals arg1="${project.type}" arg2="library" />
409        </condition>
410        <condition property="project.is.test" value="true" else="false">
411            <equals arg1="${project.type}" arg2="test" />
412        </condition>
413        <condition property="project.is.testapp" value="true" else="false">
414            <equals arg1="${project.type}" arg2="test-app" />
415        </condition>
416
417        <!-- If a test project, resolve absolute path to tested project. -->
418        <if condition="${project.is.test}">
419            <then>
420                <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
421            </then>
422        </if>
423
424        <!-- get the project manifest package -->
425        <xpath input="${manifest.abs.file}"
426                expression="/manifest/@package" output="project.app.package" />
427
428    </target>
429
430    <!-- empty default pre-clean target. Create a similar target in
431         your build.xml and it'll be called instead of this one. -->
432    <target name="-pre-clean"/>
433
434    <!-- clean target -->
435    <target name="clean" depends="-setup, -pre-clean"
436            description="Removes output files created by other targets.">
437        <delete dir="${out.absolute.dir}" verbose="${verbose}" />
438        <delete dir="${gen.absolute.dir}" verbose="${verbose}" />
439
440        <!-- if we know about a tested project or libraries, we clean them too. -->
441        <if condition="${project.is.test}">
442            <then>
443                <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
444                <subant failonerror="true">
445                    <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
446                    <target name="clean" />
447                </subant>
448            </then>
449        </if>
450
451        <!-- get all the libraries -->
452        <if>
453            <condition><not><isset property="dont.do.deps" /></not></condition>
454            <then>
455                <getlibpath libraryFolderPathOut="project.library.folder.path" />
456                <if>
457                    <condition>
458                        <isreference refid="project.library.folder.path" />
459                    </condition>
460                    <then>
461                        <!-- clean the libraries with nodeps since we already
462                             know about all the libraries even the indirect one -->
463                        <subant
464                                buildpathref="project.library.folder.path"
465                                antfile="build.xml"
466                                failonerror="true">
467                            <target name="nodeps" />
468                            <target name="clean" />
469                        </subant>
470                    </then>
471                </if>
472            </then>
473        </if>
474    </target>
475
476    <!-- Pre build setup -->
477    <target name="-build-setup" depends="-setup">
478        <!-- find location of build tools -->
479        <getbuildtools name="android.build.tools.dir" />
480        <property name="aidl" location="${android.build.tools.dir}/aidl${exe}" />
481        <property name="aapt" location="${android.build.tools.dir}/aapt${exe}" />
482        <property name="dx" location="${android.build.tools.dir}/dx${bat}" />
483
484        <!-- Renderscript include Path -->
485        <path id="android.renderscript.include.path">
486            <pathelement location="${android.build.tools.dir}/renderscript/include" />
487            <pathelement location="${android.build.tools.dir}/renderscript/clang-include" />
488        </path>
489
490        <!-- read the previous build mode -->
491        <property file="${out.build.prop.file}" />
492        <!-- if empty the props won't be set, meaning it's a new build.
493             To force a build, set the prop to empty values. -->
494        <property name="build.last.target" value="" />
495        <property name="build.last.is.instrumented" value="" />
496        <property name="build.last.is.packaging.debug" value="" />
497        <property name="build.last.is.signing.debug" value="" />
498
499        <!-- If the "debug" build type changed, clear out the compiled code.
500             This is to make sure the new BuildConfig.DEBUG value is picked up
501             as javac can't deal with this type of change in its dependency computation. -->
502        <if>
503            <condition>
504                <and>
505                    <length string="${build.last.is.packaging.debug}" trim="true" when="greater" length="0" />
506                    <not><equals
507                            arg1="${build.is.packaging.debug}"
508                            arg2="${build.last.is.packaging.debug}" /></not>
509                </and>
510            </condition>
511            <then>
512                <echo level="info">Switching between debug and non debug build: Deleting previous compilation output...</echo>
513                <delete dir="${out.classes.absolute.dir}" verbose="${verbose}" />
514            </then>
515            <else>
516                <!-- Else, we may still need to clean the code, for another reason.
517                     special case for instrumented: if the previous build was
518                     instrumented but not this one, clear out the compiled code -->
519                <if>
520                    <condition>
521                        <and>
522                            <istrue value="${build.last.is.instrumented}" />
523                            <isfalse value="${build.is.instrumented}" />
524                        </and>
525                    </condition>
526                    <then>
527                        <echo level="info">Switching from instrumented to non-instrumented build: Deleting previous compilation output...</echo>
528                        <delete dir="${out.classes.absolute.dir}" verbose="${verbose}" />
529                    </then>
530                </if>
531            </else>
532        </if>
533
534        <echo level="info">Resolving Build Target for ${ant.project.name}...</echo>
535        <!-- load project properties, resolve Android target, library dependencies
536             and set some properties with the results.
537             All property names are passed as parameters ending in -Out -->
538        <gettarget
539                androidJarFileOut="project.target.android.jar"
540                androidAidlFileOut="project.target.framework.aidl"
541                bootClassPathOut="project.target.class.path"
542                targetApiOut="project.target.apilevel"
543                minSdkVersionOut="project.minSdkVersion" />
544
545        <!-- Value of the hasCode attribute (Application node) extracted from manifest file -->
546        <xpath input="${manifest.abs.file}" expression="/manifest/application/@android:hasCode"
547                    output="manifest.hasCode" default="true"/>
548
549        <echo level="info">----------</echo>
550        <echo level="info">Creating output directories if needed...</echo>
551        <mkdir dir="${resource.absolute.dir}" />
552        <mkdir dir="${jar.libs.absolute.dir}" />
553        <mkdir dir="${out.absolute.dir}" />
554        <mkdir dir="${out.res.absolute.dir}" />
555        <do-only-if-manifest-hasCode>
556            <mkdir dir="${gen.absolute.dir}" />
557            <mkdir dir="${out.classes.absolute.dir}" />
558            <mkdir dir="${out.dexed.absolute.dir}" />
559        </do-only-if-manifest-hasCode>
560
561        <echo level="info">----------</echo>
562        <echo level="info">Resolving Dependencies for ${ant.project.name}...</echo>
563        <dependency
564                libraryFolderPathOut="project.library.folder.path"
565                libraryPackagesOut="project.library.packages"
566                libraryManifestFilePathOut="project.library.manifest.file.path"
567                libraryResFolderPathOut="project.library.res.folder.path"
568                libraryBinAidlFolderPathOut="project.library.bin.aidl.folder.path"
569                libraryRFilePathOut="project.library.bin.r.file.path"
570                libraryNativeFolderPathOut="project.library.native.folder.path"
571                jarLibraryPathOut="project.all.jars.path"
572                targetApi="${project.target.apilevel}"
573                verbose="${verbose}" />
574
575        <!-- compile the libraries if any -->
576        <if>
577            <condition>
578                <and>
579                    <isreference refid="project.library.folder.path" />
580                    <not><isset property="dont.do.deps" /></not>
581                </and>
582            </condition>
583            <then>
584                <!-- figure out which target must be used to build the library projects.
585                     If emma is enabled, then use 'instrument' otherwise, use 'debug' -->
586                <condition property="project.libraries.target" value="instrument" else="${build.target}">
587                    <istrue value="${build.is.instrumented}" />
588                </condition>
589
590                <echo level="info">----------</echo>
591                <echo level="info">Building Libraries with '${project.libraries.target}'...</echo>
592
593                <!-- no need to build the deps as we have already
594                     the full list of libraries -->
595                <subant failonerror="true"
596                        buildpathref="project.library.folder.path"
597                        antfile="build.xml">
598                    <target name="nodeps" />
599                    <target name="${project.libraries.target}" />
600                    <property name="emma.coverage.absolute.file" location="${out.absolute.dir}/coverage.em" />
601                </subant>
602            </then>
603        </if>
604
605        <!-- compile the main project if this is a test project -->
606        <if condition="${project.is.test}">
607            <then>
608                <!-- figure out which target must be used to build the tested project.
609                     If emma is enabled, then use 'instrument' otherwise, use 'debug' -->
610                <condition property="tested.project.target" value="instrument" else="debug">
611                    <isset property="emma.enabled" />
612                </condition>
613
614                <echo level="info">----------</echo>
615                <echo level="info">Building tested project at ${tested.project.absolute.dir} with '${tested.project.target}'...</echo>
616                <subant target="${tested.project.target}" failonerror="true">
617                    <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
618                </subant>
619
620                <!-- get the tested project full classpath to be able to build
621                     the test project -->
622                <testedprojectclasspath
623                        projectLocation="${tested.project.absolute.dir}"
624                        projectClassPathOut="tested.project.classpath"/>
625            </then>
626            <else>
627                <!-- no tested project, make an empty Path object so that javac doesn't
628                     complain -->
629                <path id="tested.project.classpath" />
630            </else>
631        </if>
632    </target>
633
634    <!-- empty default pre-build target. Create a similar target in
635         your build.xml and it'll be called instead of this one. -->
636    <target name="-pre-build"/>
637
638    <!-- Code Generation: compile resources (aapt -> R.java), aidl, renderscript -->
639    <target name="-code-gen">
640        <!-- always merge manifest -->
641        <mergemanifest
642                appManifest="${manifest.abs.file}"
643                outManifest="${out.manifest.abs.file}"
644                enabled="${manifestmerger.enabled}">
645            <library refid="project.library.manifest.file.path" />
646        </mergemanifest>
647
648        <do-only-if-manifest-hasCode
649                elseText="hasCode = false. Skipping aidl/renderscript/R.java">
650            <echo level="info">Handling aidl files...</echo>
651            <aidl executable="${aidl}"
652                    framework="${project.target.framework.aidl}"
653                    libraryBinAidlFolderPathRefid="project.library.bin.aidl.folder.path"
654                    genFolder="${gen.absolute.dir}"
655                    aidlOutFolder="${out.aidl.absolute.dir}">
656                <source path="${source.absolute.dir}"/>
657            </aidl>
658
659            <!-- renderscript generates resources so it must be called before aapt -->
660            <echo level="info">----------</echo>
661            <echo level="info">Handling RenderScript files...</echo>
662            <!-- set the rs target prop in case it hasn't been set. -->
663            <property name="renderscript.target" value="${project.minSdkVersion}" />
664            <renderscript buildToolsRoot="${android.build.tools.dir}"
665                    includePathRefId="android.renderscript.include.path"
666                    genFolder="${gen.absolute.dir}"
667                    resFolder="${out.res.absolute.dir}/raw"
668                    targetApi="${renderscript.target}"
669                    optLevel="${renderscript.opt.level}"
670                    buildType="${build.is.packaging.debug}"
671                    previousBuildType="${build.last.is.packaging.debug}">
672                <source path="${source.absolute.dir}"/>
673            </renderscript>
674
675            <echo level="info">----------</echo>
676            <echo level="info">Handling Resources...</echo>
677            <aapt executable="${aapt}"
678                    command="package"
679                    verbose="${verbose}"
680                    manifest="${out.manifest.abs.file}"
681                    originalManifestPackage="${project.app.package}"
682                    androidjar="${project.target.android.jar}"
683                    rfolder="${gen.absolute.dir}"
684                    nonConstantId="${android.library}"
685                    libraryResFolderPathRefid="project.library.res.folder.path"
686                    libraryPackagesRefid="project.library.packages"
687                    libraryRFileRefid="project.library.bin.r.file.path"
688                    ignoreAssets="${aapt.ignore.assets}"
689                    binFolder="${out.absolute.dir}"
690                    proguardFile="${out.absolute.dir}/proguard.txt">
691                <res path="${out.res.absolute.dir}" />
692                <res path="${resource.absolute.dir}" />
693            </aapt>
694
695            <echo level="info">----------</echo>
696            <echo level="info">Handling BuildConfig class...</echo>
697            <buildconfig
698                    genFolder="${gen.absolute.dir}"
699                    package="${project.app.package}"
700                    buildType="${build.is.packaging.debug}"
701                    previousBuildType="${build.last.is.packaging.debug}"/>
702
703        </do-only-if-manifest-hasCode>
704    </target>
705
706    <!-- empty default pre-compile target. Create a similar target in
707         your build.xml and it'll be called instead of this one. -->
708    <target name="-pre-compile"/>
709
710    <!-- Compiles this project's .java files into .class files. -->
711    <target name="-compile" depends="-pre-build, -build-setup, -code-gen, -pre-compile">
712        <do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">
713            <!-- merge the project's own classpath and the tested project's classpath -->
714            <path id="project.javac.classpath">
715                <path refid="project.all.jars.path" />
716                <path refid="tested.project.classpath" />
717                <path path="${java.compiler.classpath}" />
718            </path>
719            <javac encoding="${java.encoding}"
720                    source="${java.source}" target="${java.target}"
721                    debug="true" extdirs="" includeantruntime="false"
722                    destdir="${out.classes.absolute.dir}"
723                    bootclasspathref="project.target.class.path"
724                    verbose="${verbose}"
725                    classpathref="project.javac.classpath"
726                    fork="${need.javac.fork}">
727                <src path="${source.absolute.dir}" />
728                <src path="${gen.absolute.dir}" />
729                <compilerarg line="${java.compilerargs}" />
730            </javac>
731
732            <!-- if the project is instrumented, intrument the classes -->
733            <if condition="${build.is.instrumented}">
734                <then>
735                    <echo level="info">Instrumenting classes from ${out.absolute.dir}/classes...</echo>
736
737                    <!-- build the filter to remove R, Manifest, BuildConfig -->
738                    <getemmafilter
739                            appPackage="${project.app.package}"
740                            libraryPackagesRefId="project.library.packages"
741                            filterOut="emma.default.filter"/>
742
743                    <!-- define where the .em file is going. This may have been
744                         setup already if this is a library -->
745                    <property name="emma.coverage.absolute.file" location="${out.absolute.dir}/coverage.em" />
746
747                    <!-- It only instruments class files, not any external libs -->
748                    <emma enabled="true">
749                        <instr verbosity="${verbosity}"
750                               mode="overwrite"
751                               instrpath="${out.absolute.dir}/classes"
752                               outdir="${out.absolute.dir}/classes"
753                               metadatafile="${emma.coverage.absolute.file}">
754                            <filter excludes="${emma.default.filter}" />
755                            <filter value="${emma.filter}" />
756                        </instr>
757                    </emma>
758                </then>
759            </if>
760
761            <!-- if the project is a library then we generate a jar file -->
762            <if condition="${project.is.library}">
763                <then>
764                    <echo level="info">Creating library output jar file...</echo>
765                    <property name="out.library.jar.file" location="${out.absolute.dir}/classes.jar" />
766                    <if>
767                        <condition>
768                            <length string="${android.package.excludes}" trim="true" when="greater" length="0" />
769                        </condition>
770                        <then>
771                            <echo level="info">Custom jar packaging exclusion: ${android.package.excludes}</echo>
772                        </then>
773                    </if>
774
775                    <propertybyreplace name="project.app.package.path" input="${project.app.package}" replace="." with="/" />
776
777                    <jar destfile="${out.library.jar.file}">
778                        <fileset dir="${out.classes.absolute.dir}"
779                                includes="**/*.class"
780                                excludes="${project.app.package.path}/R.class ${project.app.package.path}/R$*.class ${project.app.package.path}/BuildConfig.class"/>
781                        <fileset dir="${source.absolute.dir}" excludes="**/*.java ${android.package.excludes}" />
782                    </jar>
783                </then>
784            </if>
785
786        </do-only-if-manifest-hasCode>
787    </target>
788
789    <!-- empty default post-compile target. Create a similar target in
790         your build.xml and it'll be called instead of this one. -->
791    <target name="-post-compile"/>
792
793    <!-- Obfuscate target
794        This is only active in release builds when proguard.config is defined
795        in default.properties.
796
797        To replace Proguard with a different obfuscation engine:
798        Override the following targets in your build.xml, before the call to <setup>
799            -release-obfuscation-check
800                Check whether obfuscation should happen, and put the result in a property.
801            -debug-obfuscation-check
802                Obfuscation should not happen. Set the same property to false.
803            -obfuscate
804                check if the property set in -debug/release-obfuscation-check is set to true.
805                If true:
806                    Perform obfuscation
807                    Set property out.dex.input.absolute.dir to be the output of the obfuscation
808    -->
809    <target name="-obfuscate">
810        <if condition="${proguard.enabled}">
811            <then>
812                <property name="obfuscate.absolute.dir" location="${out.absolute.dir}/proguard" />
813                <property name="preobfuscate.jar.file" value="${obfuscate.absolute.dir}/original.jar" />
814                <property name="obfuscated.jar.file" value="${obfuscate.absolute.dir}/obfuscated.jar" />
815                <!-- input for dex will be proguard's output -->
816                <property name="out.dex.input.absolute.dir" value="${obfuscated.jar.file}" />
817
818                <!-- Add Proguard Tasks -->
819                <property name="proguard.jar" location="${android.tools.dir}/proguard/lib/proguard.jar" />
820                <taskdef name="proguard" classname="proguard.ant.ProGuardTask" classpath="${proguard.jar}" />
821
822                <!-- Set the android classpath Path object into a single property. It'll be
823                     all the jar files separated by a platform path-separator.
824                     Each path must be quoted if it contains spaces.
825                -->
826                <pathconvert property="project.target.classpath.value" refid="project.target.class.path">
827                    <firstmatchmapper>
828                        <regexpmapper from='^([^ ]*)( .*)$$' to='"\1\2"'/>
829                        <identitymapper/>
830                    </firstmatchmapper>
831                </pathconvert>
832
833                <!-- Build a path object with all the jar files that must be obfuscated.
834                     This include the project compiled source code and any 3rd party jar
835                     files. -->
836                <path id="project.all.classes.path">
837                    <pathelement location="${preobfuscate.jar.file}" />
838                    <path refid="project.all.jars.path" />
839                </path>
840                <!-- Set the project jar files Path object into a single property. It'll be
841                     all the jar files separated by a platform path-separator.
842                     Each path must be quoted if it contains spaces.
843                -->
844                <pathconvert property="project.all.classes.value" refid="project.all.classes.path">
845                    <firstmatchmapper>
846                        <regexpmapper from='^([^ ]*)( .*)$$' to='"\1\2"'/>
847                        <identitymapper/>
848                    </firstmatchmapper>
849                </pathconvert>
850
851                <!-- Turn the path property ${proguard.config} from an A:B:C property
852                     into a series of includes: -include A -include B -include C
853                     suitable for processing by the ProGuard task. Note - this does
854                     not include the leading '-include "' or the closing '"'; those
855                     are added under the <proguard> call below.
856                -->
857                <path id="proguard.configpath">
858                    <pathelement path="${proguard.config}"/>
859                </path>
860                <pathconvert pathsep='" -include "' property="proguard.configcmd" refid="proguard.configpath"/>
861
862                <mkdir   dir="${obfuscate.absolute.dir}" />
863                <delete file="${preobfuscate.jar.file}"/>
864                <delete file="${obfuscated.jar.file}"/>
865                <jar basedir="${out.classes.absolute.dir}"
866                    destfile="${preobfuscate.jar.file}" />
867                <proguard>
868                    -include      "${proguard.configcmd}"
869                    -include      "${out.absolute.dir}/proguard.txt"
870                    -injars       ${project.all.classes.value}
871                    -outjars      "${obfuscated.jar.file}"
872                    -libraryjars  ${project.target.classpath.value}
873                    -dump         "${obfuscate.absolute.dir}/dump.txt"
874                    -printseeds   "${obfuscate.absolute.dir}/seeds.txt"
875                    -printusage   "${obfuscate.absolute.dir}/usage.txt"
876                    -printmapping "${obfuscate.absolute.dir}/mapping.txt"
877                </proguard>
878            </then>
879        </if>
880    </target>
881
882    <!-- Converts this project's .class files into .dex files -->
883    <target name="-dex" depends="-compile, -post-compile, -obfuscate">
884        <do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">
885            <!-- only convert to dalvik bytecode is *not* a library -->
886            <do-only-if-not-library elseText="Library project: do not convert bytecode..." >
887                <!-- special case for instrumented builds: need to use no-locals and need
888                     to pass in the emma jar. -->
889                <if condition="${build.is.instrumented}">
890                    <then>
891                        <dex-helper nolocals="true">
892                            <external-libs>
893                                <fileset file="${emma.dir}/emma_device.jar" />
894                            </external-libs>
895                        </dex-helper>
896                    </then>
897                    <else>
898                        <dex-helper />
899                    </else>
900                </if>
901            </do-only-if-not-library>
902        </do-only-if-manifest-hasCode>
903    </target>
904
905<!-- Updates the pre-processed PNG cache -->
906    <target name="-crunch">
907        <exec executable="${aapt}" taskName="crunch">
908            <arg value="crunch" />
909            <arg value="-v" />
910            <arg value="-S" />
911            <arg path="${resource.absolute.dir}" />
912            <arg value="-C" />
913            <arg path="${out.res.absolute.dir}" />
914        </exec>
915    </target>
916
917    <!-- Puts the project's resources into the output package file
918         This actually can create multiple resource package in case
919         Some custom apk with specific configuration have been
920         declared in default.properties.
921         -->
922    <target name="-package-resources" depends="-crunch">
923        <!-- only package resources if *not* a library project -->
924        <do-only-if-not-library elseText="Library project: do not package resources..." >
925            <aapt executable="${aapt}"
926                    command="package"
927                    versioncode="${version.code}"
928                    versionname="${version.name}"
929                    debug="${build.is.packaging.debug}"
930                    manifest="${out.manifest.abs.file}"
931                    assets="${asset.absolute.dir}"
932                    androidjar="${project.target.android.jar}"
933                    apkfolder="${out.absolute.dir}"
934                    nocrunch="${build.packaging.nocrunch}"
935                    resourcefilename="${resource.package.file.name}"
936                    resourcefilter="${aapt.resource.filter}"
937                    libraryResFolderPathRefid="project.library.res.folder.path"
938                    libraryPackagesRefid="project.library.packages"
939                    libraryRFileRefid="project.library.bin.r.file.path"
940                    previousBuildType="${build.last.target}"
941                    buildType="${build.target}"
942                    ignoreAssets="${aapt.ignore.assets}">
943                <res path="${out.res.absolute.dir}" />
944                <res path="${resource.absolute.dir}" />
945                <!-- <nocompress /> forces no compression on any files in assets or res/raw -->
946                <!-- <nocompress extension="xml" /> forces no compression on specific file extensions in assets and res/raw -->
947            </aapt>
948        </do-only-if-not-library>
949    </target>
950
951    <!-- Packages the application. -->
952    <target name="-package" depends="-dex, -package-resources">
953        <!-- only package apk if *not* a library project -->
954        <do-only-if-not-library elseText="Library project: do not package apk..." >
955            <if condition="${build.is.instrumented}">
956                <then>
957                    <package-helper>
958                        <extra-jars>
959                            <!-- Injected from external file -->
960                            <jarfile path="${emma.dir}/emma_device.jar" />
961                        </extra-jars>
962                    </package-helper>
963                </then>
964                <else>
965                    <package-helper />
966                </else>
967            </if>
968        </do-only-if-not-library>
969    </target>
970
971    <target name="-post-package" />
972    <target name="-post-build" />
973
974    <target name="-set-mode-check">
975        <fail if="build.is.mode.set"
976                message="Cannot run two different modes at the same time. If you are running more than one debug/release/instrument type targets, call them from different Ant calls." />
977    </target>
978
979    <!-- ******************************************************* -->
980    <!-- **************** Debug specific targets *************** -->
981    <!-- ******************************************************* -->
982
983    <target name="-set-debug-files" depends="-set-mode-check">
984
985        <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-debug-unaligned.apk" />
986        <property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-debug.apk" />
987        <property name="build.is.mode.set" value="true" />
988    </target>
989
990
991    <target name="-set-debug-mode" depends="-setup">
992        <!-- record the current build target -->
993        <property name="build.target" value="debug" />
994
995        <if>
996            <condition>
997                <and>
998                    <istrue value="${project.is.testapp}" />
999                    <istrue value="${emma.enabled}" />
1000                </and>
1001            </condition>
1002            <then>
1003                <property name="build.is.instrumented" value="true" />
1004            </then>
1005            <else>
1006                <property name="build.is.instrumented" value="false" />
1007            </else>
1008        </if>
1009
1010        <!-- whether the build is a debug build. always set. -->
1011        <property name="build.is.packaging.debug" value="true" />
1012
1013        <!-- signing mode: debug -->
1014        <property name="build.is.signing.debug" value="true" />
1015
1016        <!-- Renderscript optimization level: none -->
1017        <property name="renderscript.opt.level" value="${renderscript.debug.opt.level}" />
1018
1019    </target>
1020
1021    <target name="-debug-obfuscation-check">
1022        <!-- proguard is never enabled in debug mode -->
1023        <property name="proguard.enabled" value="false"/>
1024    </target>
1025
1026    <!-- Builds debug output package -->
1027    <target name="-do-debug" depends="-set-debug-mode, -debug-obfuscation-check, -package, -post-package">
1028        <!-- only create apk if *not* a library project -->
1029        <do-only-if-not-library elseText="Library project: do not create apk..." >
1030            <sequential>
1031                <zipalign-helper in.package="${out.packaged.file}" out.package="${out.final.file}" />
1032                <echo level="info">Debug Package: ${out.final.file}</echo>
1033            </sequential>
1034        </do-only-if-not-library>
1035        <record-build-info />
1036    </target>
1037
1038    <!-- Builds debug output package -->
1039    <target name="debug" depends="-set-debug-files, -do-debug, -post-build"
1040                description="Builds the application and signs it with a debug key.">
1041    </target>
1042
1043
1044    <!-- ******************************************************* -->
1045    <!-- *************** Release specific targets ************** -->
1046    <!-- ******************************************************* -->
1047
1048    <!-- called through target 'release'. Only executed if the keystore and
1049         key alias are known but not their password. -->
1050    <target name="-release-prompt-for-password" if="has.keystore" unless="has.password">
1051        <!-- Gets passwords -->
1052        <input
1053                message="Please enter keystore password (store:${key.store}):"
1054                addproperty="key.store.password" />
1055        <input
1056                message="Please enter password for alias '${key.alias}':"
1057                addproperty="key.alias.password" />
1058    </target>
1059
1060    <!-- called through target 'release'. Only executed if there's no
1061         keystore/key alias set -->
1062    <target name="-release-nosign" unless="has.keystore">
1063        <!-- no release builds for library project -->
1064        <do-only-if-not-library elseText="" >
1065            <sequential>
1066                <echo level="info">No key.store and key.alias properties found in build.properties.</echo>
1067                <echo level="info">Please sign ${out.packaged.file} manually</echo>
1068                <echo level="info">and run zipalign from the Android SDK tools.</echo>
1069            </sequential>
1070        </do-only-if-not-library>
1071        <record-build-info />
1072    </target>
1073
1074    <target name="-release-obfuscation-check">
1075        <echo level="info">proguard.config is ${proguard.config}</echo>
1076        <condition property="proguard.enabled" value="true" else="false">
1077            <and>
1078                <isset property="build.is.mode.release" />
1079                <isset property="proguard.config" />
1080            </and>
1081        </condition>
1082        <if condition="${proguard.enabled}">
1083            <then>
1084                <echo level="info">Proguard.config is enabled</echo>
1085                <!-- Secondary dx input (jar files) is empty since all the
1086                     jar files will be in the obfuscated jar -->
1087                <path id="out.dex.jar.input.ref" />
1088            </then>
1089        </if>
1090    </target>
1091
1092    <target name="-set-release-mode" depends="-set-mode-check">
1093        <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-release-unsigned.apk" />
1094        <property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-release.apk" />
1095        <property name="build.is.mode.set" value="true" />
1096
1097        <!-- record the current build target -->
1098        <property name="build.target" value="release" />
1099
1100        <property name="build.is.instrumented" value="false" />
1101
1102        <!-- release mode is only valid if the manifest does not explicitly
1103             set debuggable to true. default is false. -->
1104        <xpath input="${manifest.abs.file}" expression="/manifest/application/@android:debuggable"
1105                output="build.is.packaging.debug" default="false"/>
1106
1107        <!-- signing mode: release -->
1108        <property name="build.is.signing.debug" value="false" />
1109
1110        <!-- Renderscript optimization level: aggressive -->
1111        <property name="renderscript.opt.level" value="${renderscript.release.opt.level}" />
1112
1113        <if condition="${build.is.packaging.debug}">
1114            <then>
1115                <echo>*************************************************</echo>
1116                <echo>****  Android Manifest has debuggable=true   ****</echo>
1117                <echo>**** Doing DEBUG packaging with RELEASE keys ****</echo>
1118                <echo>*************************************************</echo>
1119            </then>
1120            <else>
1121                <!-- property only set in release mode.
1122                     Useful for if/unless attributes in target node
1123                     when using Ant before 1.8 -->
1124                <property name="build.is.mode.release" value="true"/>
1125            </else>
1126        </if>
1127    </target>
1128
1129    <target name="-release-sign" if="has.keystore" >
1130        <!-- only create apk if *not* a library project -->
1131        <do-only-if-not-library elseText="Library project: do not create apk..." >
1132            <sequential>
1133                <property name="out.unaligned.file" location="${out.absolute.dir}/${ant.project.name}-release-unaligned.apk" />
1134
1135                <!-- Signs the APK -->
1136                <echo level="info">Signing final apk...</echo>
1137                <signapk
1138                        input="${out.packaged.file}"
1139                        output="${out.unaligned.file}"
1140                        keystore="${key.store}"
1141                        storepass="${key.store.password}"
1142                        alias="${key.alias}"
1143                        keypass="${key.alias.password}"/>
1144
1145                <!-- Zip aligns the APK -->
1146                <zipalign-helper
1147                        in.package="${out.unaligned.file}"
1148                        out.package="${out.final.file}" />
1149                <echo level="info">Release Package: ${out.final.file}</echo>
1150            </sequential>
1151        </do-only-if-not-library>
1152        <record-build-info />
1153    </target>
1154
1155    <!-- This runs -package-release and -release-nosign first and then runs
1156         only if release-sign is true (set in -release-check,
1157         called by -release-no-sign)-->
1158    <target name="release"
1159                depends="-set-release-mode, -release-obfuscation-check, -package, -post-package, -release-prompt-for-password, -release-nosign, -release-sign, -post-build"
1160                description="Builds the application in release mode.">
1161    </target>
1162
1163    <!-- ******************************************************* -->
1164    <!-- ************ Instrumented specific targets ************ -->
1165    <!-- ******************************************************* -->
1166
1167    <!-- These targets are specific for the project under test when it
1168         gets compiled by the test projects in a way that will make it
1169         support emma code coverage -->
1170
1171    <target name="-set-instrumented-mode" depends="-set-mode-check">
1172        <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-instrumented-unaligned.apk" />
1173        <property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-instrumented.apk" />
1174        <property name="build.is.mode.set" value="true" />
1175
1176        <!-- whether the build is an instrumented build. -->
1177        <property name="build.is.instrumented" value="true" />
1178    </target>
1179
1180    <!-- Builds instrumented output package -->
1181    <target name="instrument" depends="-set-instrumented-mode, -do-debug"
1182                description="Builds an instrumented packaged.">
1183        <!-- only create apk if *not* a library project -->
1184        <do-only-if-not-library elseText="Library project: do not create apk..." >
1185            <sequential>
1186                <zipalign-helper in.package="${out.packaged.file}" out.package="${out.final.file}" />
1187                <echo level="info">Instrumented Package: ${out.final.file}</echo>
1188            </sequential>
1189        </do-only-if-not-library>
1190        <record-build-info />
1191    </target>
1192
1193    <!-- ******************************************************* -->
1194    <!-- ************ Test project specific targets ************ -->
1195    <!-- ******************************************************* -->
1196
1197    <!-- enable code coverage -->
1198    <target name="emma">
1199        <property name="emma.enabled" value="true" />
1200    </target>
1201
1202    <!-- fails if the project is not a test project -->
1203    <target name="-test-project-check" depends="-setup">
1204        <if>
1205            <condition>
1206                <and>
1207                    <isfalse value="${project.is.test}" />
1208                    <isfalse value="${project.is.testapp}" />
1209                </and>
1210            </condition>
1211            <then>
1212                <fail message="Project is not a test project." />
1213            </then>
1214        </if>
1215    </target>
1216
1217    <target name="test" depends="-test-project-check"
1218                description="Runs tests from the package defined in test.package property">
1219        <property name="test.runner" value="android.test.InstrumentationTestRunner" />
1220
1221        <if condition="${project.is.test}">
1222        <then>
1223            <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
1224
1225            <!-- Application package of the tested project extracted from its manifest file -->
1226            <xpath input="${tested.project.absolute.dir}/AndroidManifest.xml"
1227                    expression="/manifest/@package" output="tested.project.app.package" />
1228
1229            <if condition="${emma.enabled}">
1230                <then>
1231                    <getprojectpaths projectPath="${tested.project.absolute.dir}"
1232                            binOut="tested.project.out.absolute.dir"
1233                            srcOut="tested.project.source.absolute.dir" />
1234
1235                    <getlibpath projectPath="${tested.project.absolute.dir}"
1236                            libraryFolderPathOut="tested.project.lib.source.path"
1237                            leaf="@{source.dir}" />
1238
1239                </then>
1240            </if>
1241
1242        </then>
1243        <else>
1244            <!-- this is a test app, the tested package is the app's own package -->
1245            <property name="tested.project.app.package" value="${project.app.package}" />
1246
1247            <if condition="${emma.enabled}">
1248                <then>
1249                    <property name="tested.project.out.absolute.dir" value="${out.absolute.dir}" />
1250                    <property name="tested.project.source.absolute.dir" value="${source.absolute.dir}" />
1251
1252                    <getlibpath
1253                            libraryFolderPathOut="tested.project.lib.source.path"
1254                            leaf="@{source.dir}" />
1255
1256                </then>
1257            </if>
1258
1259        </else>
1260        </if>
1261
1262        <property name="emma.dump.file"
1263                value="/data/data/${tested.project.app.package}/coverage.ec" />
1264
1265        <if condition="${emma.enabled}">
1266            <then>
1267                <echo>Running tests...</echo>
1268                <run-tests-helper emma.enabled="true">
1269                    <extra-instrument-args>
1270                        <arg value="-e" />
1271                           <arg value="coverageFile" />
1272                           <arg value="${emma.dump.file}" />
1273                    </extra-instrument-args>
1274                </run-tests-helper>
1275
1276                <echo level="info">Setting permission to download the coverage file...</echo>
1277                <exec executable="${adb}" failonerror="true">
1278                    <arg line="${adb.device.arg}" />
1279                    <arg value="shell" />
1280                    <arg value="run-as" />
1281                    <arg value="${tested.project.app.package}" />
1282                    <arg value="chmod" />
1283                    <arg value="644" />
1284                    <arg value="${emma.dump.file}" />
1285                </exec>
1286                <echo level="info">Downloading coverage file into project directory...</echo>
1287                <exec executable="${adb}" failonerror="true">
1288                    <arg line="${adb.device.arg}" />
1289                    <arg value="pull" />
1290                    <arg value="${emma.dump.file}" />
1291                    <arg path="${out.absolute.dir}/coverage.ec" />
1292                </exec>
1293
1294                <pathconvert property="tested.project.lib.source.path.value" refid="tested.project.lib.source.path">
1295                    <firstmatchmapper>
1296                        <regexpmapper from='^([^ ]*)( .*)$$' to='"\1\2"'/>
1297                        <identitymapper/>
1298                    </firstmatchmapper>
1299                </pathconvert>
1300
1301                <echo level="info">Extracting coverage report...</echo>
1302                <emma>
1303                    <property name="report.html.out.encoding" value="UTF-8" />
1304                    <report sourcepath="${tested.project.source.absolute.dir}:${tested.project.lib.source.path.value}"
1305                            verbosity="${verbosity}">
1306                        <!-- TODO: report.dir or something like should be introduced if necessary -->
1307                        <infileset file="${out.absolute.dir}/coverage.ec" />
1308                        <infileset file="${tested.project.out.absolute.dir}/coverage.em" />
1309                        <!-- TODO: reports in other, indicated by user formats -->
1310                        <html outfile="${out.absolute.dir}/coverage.html" />
1311                        <txt outfile="${out.absolute.dir}/coverage.txt" />
1312                        <xml outfile="${out.absolute.dir}/coverage.xml" />
1313                   </report>
1314                </emma>
1315                <echo level="info">Cleaning up temporary files...</echo>
1316                <delete file="${out.absolute.dir}/coverage.ec" />
1317                <delete file="${tested.project.out.absolute.dir}/coverage.em" />
1318                <exec executable="${adb}" failonerror="true">
1319                    <arg line="${adb.device.arg}" />
1320                    <arg value="shell" />
1321                    <arg value="run-as" />
1322                    <arg value="${tested.project.app.package}" />
1323                    <arg value="rm" />
1324                    <arg value="${emma.dump.file}" />
1325                </exec>
1326                <echo level="info">Saving the coverage reports in ${out.absolute.dir}</echo>
1327            </then>
1328            <else>
1329                <run-tests-helper />
1330            </else>
1331        </if>
1332    </target>
1333
1334    <!-- ******************************************************* -->
1335    <!-- **********        Run Lint on the project     ********* -->
1336    <!-- ******************************************************* -->
1337
1338    <target name="lint"
1339            description="Runs lint on the project to look for potential bugs" >
1340        <lint executable="${lint}"
1341              html="${lint.out.html}"
1342              xml="${lint.out.xml}"
1343              src="${source.absolute.dir}:${gen.absolute.dir}"
1344              classpath="${out.classes.absolute.dir}" />
1345    </target>
1346
1347    <!-- ******************************************************* -->
1348    <!-- ********** Install/uninstall specific targets ********* -->
1349    <!-- ******************************************************* -->
1350
1351    <target name="install"
1352                description="Installs the newly build package. Must be used in conjunction with a build target
1353                            (debug/release/instrument). If the application was previously installed, the application
1354                            is reinstalled if the signature matches." >
1355        <!-- only do install if *not* a library project -->
1356        <do-only-if-not-library elseText="Library project: nothing to install!" >
1357            <if>
1358                <condition>
1359                    <isset property="out.final.file" />
1360                </condition>
1361                <then>
1362                    <if>
1363                        <condition>
1364                            <resourceexists>
1365                                <file file="${out.final.file}"/>
1366                            </resourceexists>
1367                        </condition>
1368                        <then>
1369                            <echo level="info">Installing ${out.final.file} onto default emulator or device...</echo>
1370                            <exec executable="${adb}" failonerror="true">
1371                                <arg line="${adb.device.arg}" />
1372                                <arg value="install" />
1373                                <arg value="-r" />
1374                                <arg path="${out.final.file}" />
1375                            </exec>
1376
1377                            <!-- now install the tested project if applicable -->
1378                            <!-- can't use project.is.test since the setup target might not have run -->
1379                            <if>
1380                                <condition>
1381                                    <and>
1382                                        <isset property="tested.project.dir" />
1383                                        <not>
1384                                            <isset property="dont.do.deps" />
1385                                        </not>
1386                                    </and>
1387                                </condition>
1388                                <then>
1389                                    <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
1390
1391                                    <!-- figure out which tested package to install based on emma.enabled -->
1392                                    <condition property="tested.project.install.target" value="installi" else="installd">
1393                                        <isset property="emma.enabled" />
1394                                    </condition>
1395                                    <subant target="${tested.project.install.target}" failonerror="true">
1396                                        <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
1397                                    </subant>
1398                                </then>
1399                            </if>
1400                        </then>
1401                        <else>
1402                            <fail message="File ${out.final.file} does not exist." />
1403                        </else>
1404                    </if>
1405                </then>
1406                <else>
1407                    <echo>Install file not specified.</echo>
1408                    <echo></echo>
1409                    <echo>'ant install' now requires the build target to be specified as well.</echo>
1410                    <echo></echo>
1411                    <echo></echo>
1412                    <echo>    ant debug install</echo>
1413                    <echo>    ant release install</echo>
1414                    <echo>    ant instrument install</echo>
1415                    <echo>This will build the given package and install it.</echo>
1416                    <echo></echo>
1417                    <echo>Alternatively, you can use</echo>
1418                    <echo>    ant installd</echo>
1419                    <echo>    ant installr</echo>
1420                    <echo>    ant installi</echo>
1421                    <echo>    ant installt</echo>
1422                    <echo>to only install an existing package (this will not rebuild the package.)</echo>
1423                    <fail />
1424                </else>
1425            </if>
1426        </do-only-if-not-library>
1427    </target>
1428
1429    <target name="installd" depends="-set-debug-files, install"
1430            description="Installs (only) the debug package." />
1431    <target name="installr" depends="-set-release-mode, install"
1432            description="Installs (only) the release package." />
1433    <target name="installi" depends="-set-instrumented-mode, install"
1434            description="Installs (only) the instrumented package." />
1435    <target name="installt" depends="-test-project-check, installd"
1436            description="Installs (only) the test and tested packages." />
1437
1438
1439    <!-- Uninstalls the package from the default emulator/device -->
1440    <target name="uninstall" depends="-setup"
1441                description="Uninstalls the application from a running emulator or device.">
1442        <if>
1443            <condition>
1444                <isset property="project.app.package" />
1445            </condition>
1446            <then>
1447                <uninstall-helper app.package="${project.app.package}" />
1448            </then>
1449            <else>
1450                <fail message="Could not find application package in manifest. Cannot run 'adb uninstall'." />
1451            </else>
1452        </if>
1453
1454        <!-- Now uninstall the tested project, if applicable -->
1455        <if>
1456            <condition>
1457                <and>
1458                    <istrue value="${project.is.test}" />
1459                    <not>
1460                        <isset property="dont.do.deps" />
1461                    </not>
1462                </and>
1463            </condition>
1464            <then>
1465                <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
1466
1467                <!-- Application package of the tested project extracted from its manifest file -->
1468                <xpath input="${tested.project.absolute.dir}/AndroidManifest.xml"
1469                    expression="/manifest/@package" output="tested.project.app.package" />
1470                <if>
1471                    <condition>
1472                        <isset property="tested.project.app.package" />
1473                    </condition>
1474                    <then>
1475                        <uninstall-helper app.package="${tested.project.app.package}" />
1476                    </then>
1477                    <else>
1478                        <fail message="Could not find tested application package in manifest. Cannot run 'adb uninstall'." />
1479                    </else>
1480                </if>
1481            </then>
1482        </if>
1483
1484    </target>
1485
1486
1487    <!-- ******************************************************* -->
1488    <!-- ************************* Help ************************ -->
1489    <!-- ******************************************************* -->
1490
1491    <target name="help">
1492        <!-- displays starts at col 13
1493              |13                                                              80| -->
1494        <echo>Android Ant Build. Available targets:</echo>
1495        <echo>   help:      Displays this help.</echo>
1496        <echo>   clean:     Removes output files created by other targets.</echo>
1497        <echo>              This calls the same target on all dependent projects.</echo>
1498        <echo>              Use 'ant nodeps clean' to only clean the local project</echo>
1499        <echo>   debug:     Builds the application and signs it with a debug key.</echo>
1500        <echo>              The 'nodeps' target can be used to only build the</echo>
1501        <echo>              current project and ignore the libraries using:</echo>
1502        <echo>              'ant nodeps debug'</echo>
1503        <echo>   release:   Builds the application. The generated apk file must be</echo>
1504        <echo>              signed before it is published.</echo>
1505        <echo>              The 'nodeps' target can be used to only build the</echo>
1506        <echo>              current project and ignore the libraries using:</echo>
1507        <echo>              'ant nodeps release'</echo>
1508        <echo>   instrument:Builds an instrumented package and signs it with a</echo>
1509        <echo>              debug key.</echo>
1510        <echo>   test:      Runs the tests. Project must be a test project and</echo>
1511        <echo>              must have been built. Typical usage would be:</echo>
1512        <echo>                  ant [emma] debug install test</echo>
1513        <echo>   emma:      Transiently enables code coverage for subsequent</echo>
1514        <echo>              targets.</echo>
1515        <echo>   install:   Installs the newly build package. Must either be used</echo>
1516        <echo>              in conjunction with a build target (debug/release/</echo>
1517        <echo>              instrument) or with the proper suffix indicating</echo>
1518        <echo>              which package to install (see below).</echo>
1519        <echo>              If the application was previously installed, the</echo>
1520        <echo>              application is reinstalled if the signature matches.</echo>
1521        <echo>   installd:  Installs (only) the debug package.</echo>
1522        <echo>   installr:  Installs (only) the release package.</echo>
1523        <echo>   installi:  Installs (only) the instrumented package.</echo>
1524        <echo>   installt:  Installs (only) the test and tested packages (unless</echo>
1525        <echo>              nodeps is used as well.</echo>
1526        <echo>   uninstall: Uninstalls the application from a running emulator or</echo>
1527        <echo>              device. Also uninstall tested package if applicable</echo>
1528        <echo>              unless 'nodeps' is used as well.</echo>
1529    </target>
1530</project>
1531