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