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
485        <!-- read the previous build mode -->
486        <property file="${out.build.prop.file}" />
487        <!-- if empty the props won't be set, meaning it's a new build.
488             To force a build, set the prop to empty values. -->
489        <property name="build.last.target" value="" />
490        <property name="build.last.is.instrumented" value="" />
491        <property name="build.last.is.packaging.debug" value="" />
492        <property name="build.last.is.signing.debug" value="" />
493
494        <!-- If the "debug" build type changed, clear out the compiled code.
495             This is to make sure the new BuildConfig.DEBUG value is picked up
496             as javac can't deal with this type of change in its dependency computation. -->
497        <if>
498            <condition>
499                <and>
500                    <length string="${build.last.is.packaging.debug}" trim="true" when="greater" length="0" />
501                    <not><equals
502                            arg1="${build.is.packaging.debug}"
503                            arg2="${build.last.is.packaging.debug}" /></not>
504                </and>
505            </condition>
506            <then>
507                <echo level="info">Switching between debug and non debug build: Deleting previous compilation output...</echo>
508                <delete dir="${out.classes.absolute.dir}" verbose="${verbose}" />
509            </then>
510            <else>
511                <!-- Else, we may still need to clean the code, for another reason.
512                     special case for instrumented: if the previous build was
513                     instrumented but not this one, clear out the compiled code -->
514                <if>
515                    <condition>
516                        <and>
517                            <istrue value="${build.last.is.instrumented}" />
518                            <isfalse value="${build.is.instrumented}" />
519                        </and>
520                    </condition>
521                    <then>
522                        <echo level="info">Switching from instrumented to non-instrumented build: Deleting previous compilation output...</echo>
523                        <delete dir="${out.classes.absolute.dir}" verbose="${verbose}" />
524                    </then>
525                </if>
526            </else>
527        </if>
528
529        <echo level="info">Resolving Build Target for ${ant.project.name}...</echo>
530        <!-- load project properties, resolve Android target, library dependencies
531             and set some properties with the results.
532             All property names are passed as parameters ending in -Out -->
533        <gettarget
534                androidJarFileOut="project.target.android.jar"
535                androidAidlFileOut="project.target.framework.aidl"
536                bootClassPathOut="project.target.class.path"
537                targetApiOut="project.target.apilevel"
538                minSdkVersionOut="project.minSdkVersion" />
539
540        <!-- Value of the hasCode attribute (Application node) extracted from manifest file -->
541        <xpath input="${manifest.abs.file}" expression="/manifest/application/@android:hasCode"
542                    output="manifest.hasCode" default="true"/>
543
544        <echo level="info">----------</echo>
545        <echo level="info">Creating output directories if needed...</echo>
546        <mkdir dir="${resource.absolute.dir}" />
547        <mkdir dir="${jar.libs.absolute.dir}" />
548        <mkdir dir="${out.absolute.dir}" />
549        <mkdir dir="${out.res.absolute.dir}" />
550        <mkdir dir="${out.rs.obj.absolute.dir}" />
551        <mkdir dir="${out.rs.libs.absolute.dir}" />
552        <do-only-if-manifest-hasCode>
553            <mkdir dir="${gen.absolute.dir}" />
554            <mkdir dir="${out.classes.absolute.dir}" />
555            <mkdir dir="${out.dexed.absolute.dir}" />
556        </do-only-if-manifest-hasCode>
557
558        <echo level="info">----------</echo>
559        <echo level="info">Resolving Dependencies for ${ant.project.name}...</echo>
560        <dependency
561                libraryFolderPathOut="project.library.folder.path"
562                libraryPackagesOut="project.library.packages"
563                libraryManifestFilePathOut="project.library.manifest.file.path"
564                libraryResFolderPathOut="project.library.res.folder.path"
565                libraryBinAidlFolderPathOut="project.library.bin.aidl.folder.path"
566                libraryRFilePathOut="project.library.bin.r.file.path"
567                libraryNativeFolderPathOut="project.library.native.folder.path"
568                jarLibraryPathOut="project.all.jars.path"
569                targetApi="${project.target.apilevel}"
570                renderscriptSupportMode="${renderscript.support.mode}"
571                buildToolsFolder="${android.build.tools.dir}"
572                renderscriptSupportLibsOut="project.rs.support.libs.path"
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
665                    buildToolsRoot="${android.build.tools.dir}"
666                    genFolder="${gen.absolute.dir}"
667                    resFolder="${out.res.absolute.dir}"
668                    rsObjFolder="${out.rs.obj.absolute.dir}"
669                    libsFolder="${out.rs.libs.absolute.dir}"
670                    targetApi="${renderscript.target}"
671                    optLevel="${renderscript.opt.level}"
672                    supportMode="${renderscript.support.mode}"
673                    binFolder="${out.absolute.dir}"
674                    buildType="${build.is.packaging.debug}"
675                    previousBuildType="${build.last.is.packaging.debug}">
676                <source path="${source.absolute.dir}"/>
677            </renderscript>
678
679            <echo level="info">----------</echo>
680            <echo level="info">Handling Resources...</echo>
681            <aapt executable="${aapt}"
682                    command="package"
683                    verbose="${verbose}"
684                    manifest="${out.manifest.abs.file}"
685                    originalManifestPackage="${project.app.package}"
686                    androidjar="${project.target.android.jar}"
687                    rfolder="${gen.absolute.dir}"
688                    nonConstantId="${android.library}"
689                    libraryResFolderPathRefid="project.library.res.folder.path"
690                    libraryPackagesRefid="project.library.packages"
691                    libraryRFileRefid="project.library.bin.r.file.path"
692                    ignoreAssets="${aapt.ignore.assets}"
693                    binFolder="${out.absolute.dir}"
694                    proguardFile="${out.absolute.dir}/proguard.txt">
695                <res path="${out.res.absolute.dir}" />
696                <res path="${resource.absolute.dir}" />
697            </aapt>
698
699            <echo level="info">----------</echo>
700            <echo level="info">Handling BuildConfig class...</echo>
701            <buildconfig
702                    genFolder="${gen.absolute.dir}"
703                    package="${project.app.package}"
704                    buildType="${build.is.packaging.debug}"
705                    previousBuildType="${build.last.is.packaging.debug}"/>
706
707        </do-only-if-manifest-hasCode>
708    </target>
709
710    <!-- empty default pre-compile target. Create a similar target in
711         your build.xml and it'll be called instead of this one. -->
712    <target name="-pre-compile"/>
713
714    <!-- Compiles this project's .java files into .class files. -->
715    <target name="-compile" depends="-pre-build, -build-setup, -code-gen, -pre-compile">
716        <do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">
717            <!-- merge the project's own classpath and the tested project's classpath -->
718            <path id="project.javac.classpath">
719                <path refid="project.all.jars.path" />
720                <path refid="tested.project.classpath" />
721                <path path="${java.compiler.classpath}" />
722            </path>
723            <javac encoding="${java.encoding}"
724                    source="${java.source}" target="${java.target}"
725                    debug="true" extdirs="" includeantruntime="false"
726                    destdir="${out.classes.absolute.dir}"
727                    bootclasspathref="project.target.class.path"
728                    verbose="${verbose}"
729                    classpathref="project.javac.classpath"
730                    fork="${need.javac.fork}">
731                <src path="${source.absolute.dir}" />
732                <src path="${gen.absolute.dir}" />
733                <compilerarg line="${java.compilerargs}" />
734            </javac>
735
736            <!-- if the project is instrumented, intrument the classes -->
737            <if condition="${build.is.instrumented}">
738                <then>
739                    <echo level="info">Instrumenting classes from ${out.absolute.dir}/classes...</echo>
740
741                    <!-- build the filter to remove R, Manifest, BuildConfig -->
742                    <getemmafilter
743                            appPackage="${project.app.package}"
744                            libraryPackagesRefId="project.library.packages"
745                            filterOut="emma.default.filter"/>
746
747                    <!-- define where the .em file is going. This may have been
748                         setup already if this is a library -->
749                    <property name="emma.coverage.absolute.file" location="${out.absolute.dir}/coverage.em" />
750
751                    <!-- It only instruments class files, not any external libs -->
752                    <emma enabled="true">
753                        <instr verbosity="${verbosity}"
754                               mode="overwrite"
755                               instrpath="${out.absolute.dir}/classes"
756                               outdir="${out.absolute.dir}/classes"
757                               metadatafile="${emma.coverage.absolute.file}">
758                            <filter excludes="${emma.default.filter}" />
759                            <filter value="${emma.filter}" />
760                        </instr>
761                    </emma>
762                </then>
763            </if>
764
765            <!-- if the project is a library then we generate a jar file -->
766            <if condition="${project.is.library}">
767                <then>
768                    <echo level="info">Creating library output jar file...</echo>
769                    <property name="out.library.jar.file" location="${out.absolute.dir}/classes.jar" />
770                    <if>
771                        <condition>
772                            <length string="${android.package.excludes}" trim="true" when="greater" length="0" />
773                        </condition>
774                        <then>
775                            <echo level="info">Custom jar packaging exclusion: ${android.package.excludes}</echo>
776                        </then>
777                    </if>
778
779                    <propertybyreplace name="project.app.package.path" input="${project.app.package}" replace="." with="/" />
780
781                    <jar destfile="${out.library.jar.file}">
782                        <fileset dir="${out.classes.absolute.dir}"
783                                includes="**/*.class"
784                                excludes="${project.app.package.path}/R.class ${project.app.package.path}/R$*.class ${project.app.package.path}/BuildConfig.class"/>
785                        <fileset dir="${source.absolute.dir}" excludes="**/*.java ${android.package.excludes}" />
786                    </jar>
787                </then>
788            </if>
789
790        </do-only-if-manifest-hasCode>
791    </target>
792
793    <!-- empty default post-compile target. Create a similar target in
794         your build.xml and it'll be called instead of this one. -->
795    <target name="-post-compile"/>
796
797    <!-- Obfuscate target
798        This is only active in release builds when proguard.config is defined
799        in default.properties.
800
801        To replace Proguard with a different obfuscation engine:
802        Override the following targets in your build.xml, before the call to <setup>
803            -release-obfuscation-check
804                Check whether obfuscation should happen, and put the result in a property.
805            -debug-obfuscation-check
806                Obfuscation should not happen. Set the same property to false.
807            -obfuscate
808                check if the property set in -debug/release-obfuscation-check is set to true.
809                If true:
810                    Perform obfuscation
811                    Set property out.dex.input.absolute.dir to be the output of the obfuscation
812    -->
813    <target name="-obfuscate">
814        <if condition="${proguard.enabled}">
815            <then>
816                <property name="obfuscate.absolute.dir" location="${out.absolute.dir}/proguard" />
817                <property name="preobfuscate.jar.file" value="${obfuscate.absolute.dir}/original.jar" />
818                <property name="obfuscated.jar.file" value="${obfuscate.absolute.dir}/obfuscated.jar" />
819                <!-- input for dex will be proguard's output -->
820                <property name="out.dex.input.absolute.dir" value="${obfuscated.jar.file}" />
821
822                <!-- Add Proguard Tasks -->
823                <property name="proguard.jar" location="${android.tools.dir}/proguard/lib/proguard.jar" />
824                <taskdef name="proguard" classname="proguard.ant.ProGuardTask" classpath="${proguard.jar}" />
825
826                <!-- Set the android classpath Path object into a single property. It'll be
827                     all the jar files separated by a platform path-separator.
828                     Each path must be quoted if it contains spaces.
829                -->
830                <pathconvert property="project.target.classpath.value" refid="project.target.class.path">
831                    <firstmatchmapper>
832                        <regexpmapper from='^([^ ]*)( .*)$$' to='"\1\2"'/>
833                        <identitymapper/>
834                    </firstmatchmapper>
835                </pathconvert>
836
837                <!-- Build a path object with all the jar files that must be obfuscated.
838                     This include the project compiled source code and any 3rd party jar
839                     files. -->
840                <path id="project.all.classes.path">
841                    <pathelement location="${preobfuscate.jar.file}" />
842                    <path refid="project.all.jars.path" />
843                </path>
844                <!-- Set the project jar files Path object into a single property. It'll be
845                     all the jar files separated by a platform path-separator.
846                     Each path must be quoted if it contains spaces.
847                -->
848                <pathconvert property="project.all.classes.value" refid="project.all.classes.path">
849                    <firstmatchmapper>
850                        <regexpmapper from='^([^ ]*)( .*)$$' to='"\1\2"'/>
851                        <identitymapper/>
852                    </firstmatchmapper>
853                </pathconvert>
854
855                <!-- Turn the path property ${proguard.config} from an A:B:C property
856                     into a series of includes: -include A -include B -include C
857                     suitable for processing by the ProGuard task. Note - this does
858                     not include the leading '-include "' or the closing '"'; those
859                     are added under the <proguard> call below.
860                -->
861                <path id="proguard.configpath">
862                    <pathelement path="${proguard.config}"/>
863                </path>
864                <pathconvert pathsep='" -include "' property="proguard.configcmd" refid="proguard.configpath"/>
865
866                <mkdir   dir="${obfuscate.absolute.dir}" />
867                <delete file="${preobfuscate.jar.file}"/>
868                <delete file="${obfuscated.jar.file}"/>
869                <jar basedir="${out.classes.absolute.dir}"
870                    destfile="${preobfuscate.jar.file}" />
871                <proguard>
872                    -include      "${proguard.configcmd}"
873                    -include      "${out.absolute.dir}/proguard.txt"
874                    -injars       ${project.all.classes.value}
875                    -outjars      "${obfuscated.jar.file}"
876                    -libraryjars  ${project.target.classpath.value}
877                    -dump         "${obfuscate.absolute.dir}/dump.txt"
878                    -printseeds   "${obfuscate.absolute.dir}/seeds.txt"
879                    -printusage   "${obfuscate.absolute.dir}/usage.txt"
880                    -printmapping "${obfuscate.absolute.dir}/mapping.txt"
881                </proguard>
882            </then>
883        </if>
884    </target>
885
886    <!-- Converts this project's .class files into .dex files -->
887    <target name="-dex" depends="-compile, -post-compile, -obfuscate">
888        <do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">
889            <!-- only convert to dalvik bytecode is *not* a library -->
890            <do-only-if-not-library elseText="Library project: do not convert bytecode..." >
891                <!-- special case for instrumented builds: need to use no-locals and need
892                     to pass in the emma jar. -->
893                <if condition="${build.is.instrumented}">
894                    <then>
895                        <dex-helper nolocals="true">
896                            <external-libs>
897                                <fileset file="${emma.dir}/emma_device.jar" />
898                            </external-libs>
899                        </dex-helper>
900                    </then>
901                    <else>
902                        <dex-helper />
903                    </else>
904                </if>
905            </do-only-if-not-library>
906        </do-only-if-manifest-hasCode>
907    </target>
908
909<!-- Updates the pre-processed PNG cache -->
910    <target name="-crunch">
911        <exec executable="${aapt}" taskName="crunch">
912            <arg value="crunch" />
913            <arg value="-v" />
914            <arg value="-S" />
915            <arg path="${resource.absolute.dir}" />
916            <arg value="-C" />
917            <arg path="${out.res.absolute.dir}" />
918        </exec>
919    </target>
920
921    <!-- Puts the project's resources into the output package file
922         This actually can create multiple resource package in case
923         Some custom apk with specific configuration have been
924         declared in default.properties.
925         -->
926    <target name="-package-resources" depends="-crunch">
927        <!-- only package resources if *not* a library project -->
928        <do-only-if-not-library elseText="Library project: do not package resources..." >
929            <aapt executable="${aapt}"
930                    command="package"
931                    versioncode="${version.code}"
932                    versionname="${version.name}"
933                    debug="${build.is.packaging.debug}"
934                    manifest="${out.manifest.abs.file}"
935                    assets="${asset.absolute.dir}"
936                    androidjar="${project.target.android.jar}"
937                    apkfolder="${out.absolute.dir}"
938                    nocrunch="${build.packaging.nocrunch}"
939                    resourcefilename="${resource.package.file.name}"
940                    resourcefilter="${aapt.resource.filter}"
941                    libraryResFolderPathRefid="project.library.res.folder.path"
942                    libraryPackagesRefid="project.library.packages"
943                    libraryRFileRefid="project.library.bin.r.file.path"
944                    previousBuildType="${build.last.target}"
945                    buildType="${build.target}"
946                    ignoreAssets="${aapt.ignore.assets}">
947                <res path="${out.res.absolute.dir}" />
948                <res path="${resource.absolute.dir}" />
949                <!-- <nocompress /> forces no compression on any files in assets or res/raw -->
950                <!-- <nocompress extension="xml" /> forces no compression on specific file extensions in assets and res/raw -->
951            </aapt>
952        </do-only-if-not-library>
953    </target>
954
955    <!-- Packages the application. -->
956    <target name="-package" depends="-dex, -package-resources">
957        <!-- only package apk if *not* a library project -->
958        <do-only-if-not-library elseText="Library project: do not package apk..." >
959            <if condition="${build.is.instrumented}">
960                <then>
961                    <package-helper>
962                        <extra-jars>
963                            <!-- Injected from external file -->
964                            <jarfile path="${emma.dir}/emma_device.jar" />
965                        </extra-jars>
966                    </package-helper>
967                </then>
968                <else>
969                    <package-helper />
970                </else>
971            </if>
972        </do-only-if-not-library>
973    </target>
974
975    <target name="-post-package" />
976    <target name="-post-build" />
977
978    <target name="-set-mode-check">
979        <fail if="build.is.mode.set"
980                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." />
981    </target>
982
983    <!-- ******************************************************* -->
984    <!-- **************** Debug specific targets *************** -->
985    <!-- ******************************************************* -->
986
987    <target name="-set-debug-files" depends="-set-mode-check">
988
989        <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-debug-unaligned.apk" />
990        <property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-debug.apk" />
991        <property name="build.is.mode.set" value="true" />
992    </target>
993
994
995    <target name="-set-debug-mode" depends="-setup">
996        <!-- record the current build target -->
997        <property name="build.target" value="debug" />
998
999        <if>
1000            <condition>
1001                <and>
1002                    <istrue value="${project.is.testapp}" />
1003                    <istrue value="${emma.enabled}" />
1004                </and>
1005            </condition>
1006            <then>
1007                <property name="build.is.instrumented" value="true" />
1008            </then>
1009            <else>
1010                <property name="build.is.instrumented" value="false" />
1011            </else>
1012        </if>
1013
1014        <!-- whether the build is a debug build. always set. -->
1015        <property name="build.is.packaging.debug" value="true" />
1016
1017        <!-- signing mode: debug -->
1018        <property name="build.is.signing.debug" value="true" />
1019
1020        <!-- Renderscript optimization level: none -->
1021        <property name="renderscript.opt.level" value="${renderscript.debug.opt.level}" />
1022
1023    </target>
1024
1025    <target name="-debug-obfuscation-check">
1026        <!-- proguard is never enabled in debug mode -->
1027        <property name="proguard.enabled" value="false"/>
1028    </target>
1029
1030    <!-- Builds debug output package -->
1031    <target name="-do-debug" depends="-set-debug-mode, -debug-obfuscation-check, -package, -post-package">
1032        <!-- only create apk if *not* a library project -->
1033        <do-only-if-not-library elseText="Library project: do not create apk..." >
1034            <sequential>
1035                <zipalign-helper in.package="${out.packaged.file}" out.package="${out.final.file}" />
1036                <echo level="info">Debug Package: ${out.final.file}</echo>
1037            </sequential>
1038        </do-only-if-not-library>
1039        <record-build-info />
1040    </target>
1041
1042    <!-- Builds debug output package -->
1043    <target name="debug" depends="-set-debug-files, -do-debug, -post-build"
1044                description="Builds the application and signs it with a debug key.">
1045    </target>
1046
1047
1048    <!-- ******************************************************* -->
1049    <!-- *************** Release specific targets ************** -->
1050    <!-- ******************************************************* -->
1051
1052    <!-- called through target 'release'. Only executed if the keystore and
1053         key alias are known but not their password. -->
1054    <target name="-release-prompt-for-password" if="has.keystore" unless="has.password">
1055        <!-- Gets passwords -->
1056        <input
1057                message="Please enter keystore password (store:${key.store}):"
1058                addproperty="key.store.password" />
1059        <input
1060                message="Please enter password for alias '${key.alias}':"
1061                addproperty="key.alias.password" />
1062    </target>
1063
1064    <!-- called through target 'release'. Only executed if there's no
1065         keystore/key alias set -->
1066    <target name="-release-nosign" unless="has.keystore">
1067        <!-- no release builds for library project -->
1068        <do-only-if-not-library elseText="" >
1069            <sequential>
1070                <echo level="info">No key.store and key.alias properties found in build.properties.</echo>
1071                <echo level="info">Please sign ${out.packaged.file} manually</echo>
1072                <echo level="info">and run zipalign from the Android SDK tools.</echo>
1073            </sequential>
1074        </do-only-if-not-library>
1075        <record-build-info />
1076    </target>
1077
1078    <target name="-release-obfuscation-check">
1079        <echo level="info">proguard.config is ${proguard.config}</echo>
1080        <condition property="proguard.enabled" value="true" else="false">
1081            <and>
1082                <isset property="build.is.mode.release" />
1083                <isset property="proguard.config" />
1084            </and>
1085        </condition>
1086        <if condition="${proguard.enabled}">
1087            <then>
1088                <echo level="info">Proguard.config is enabled</echo>
1089                <!-- Secondary dx input (jar files) is empty since all the
1090                     jar files will be in the obfuscated jar -->
1091                <path id="out.dex.jar.input.ref" />
1092            </then>
1093        </if>
1094    </target>
1095
1096    <target name="-set-release-mode" depends="-set-mode-check">
1097        <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-release-unsigned.apk" />
1098        <property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-release.apk" />
1099        <property name="build.is.mode.set" value="true" />
1100
1101        <!-- record the current build target -->
1102        <property name="build.target" value="release" />
1103
1104        <property name="build.is.instrumented" value="false" />
1105
1106        <!-- release mode is only valid if the manifest does not explicitly
1107             set debuggable to true. default is false. -->
1108        <xpath input="${manifest.abs.file}" expression="/manifest/application/@android:debuggable"
1109                output="build.is.packaging.debug" default="false"/>
1110
1111        <!-- signing mode: release -->
1112        <property name="build.is.signing.debug" value="false" />
1113
1114        <!-- Renderscript optimization level: aggressive -->
1115        <property name="renderscript.opt.level" value="${renderscript.release.opt.level}" />
1116
1117        <if condition="${build.is.packaging.debug}">
1118            <then>
1119                <echo>*************************************************</echo>
1120                <echo>****  Android Manifest has debuggable=true   ****</echo>
1121                <echo>**** Doing DEBUG packaging with RELEASE keys ****</echo>
1122                <echo>*************************************************</echo>
1123            </then>
1124            <else>
1125                <!-- property only set in release mode.
1126                     Useful for if/unless attributes in target node
1127                     when using Ant before 1.8 -->
1128                <property name="build.is.mode.release" value="true"/>
1129            </else>
1130        </if>
1131    </target>
1132
1133    <target name="-release-sign" if="has.keystore" >
1134        <!-- only create apk if *not* a library project -->
1135        <do-only-if-not-library elseText="Library project: do not create apk..." >
1136            <sequential>
1137                <property name="out.unaligned.file" location="${out.absolute.dir}/${ant.project.name}-release-unaligned.apk" />
1138
1139                <!-- Signs the APK -->
1140                <echo level="info">Signing final apk...</echo>
1141                <signapk
1142                        input="${out.packaged.file}"
1143                        output="${out.unaligned.file}"
1144                        keystore="${key.store}"
1145                        storepass="${key.store.password}"
1146                        alias="${key.alias}"
1147                        keypass="${key.alias.password}"/>
1148
1149                <!-- Zip aligns the APK -->
1150                <zipalign-helper
1151                        in.package="${out.unaligned.file}"
1152                        out.package="${out.final.file}" />
1153                <echo level="info">Release Package: ${out.final.file}</echo>
1154            </sequential>
1155        </do-only-if-not-library>
1156        <record-build-info />
1157    </target>
1158
1159    <!-- This runs -package-release and -release-nosign first and then runs
1160         only if release-sign is true (set in -release-check,
1161         called by -release-no-sign)-->
1162    <target name="release"
1163                depends="-set-release-mode, -release-obfuscation-check, -package, -post-package, -release-prompt-for-password, -release-nosign, -release-sign, -post-build"
1164                description="Builds the application in release mode.">
1165    </target>
1166
1167    <!-- ******************************************************* -->
1168    <!-- ************ Instrumented specific targets ************ -->
1169    <!-- ******************************************************* -->
1170
1171    <!-- These targets are specific for the project under test when it
1172         gets compiled by the test projects in a way that will make it
1173         support emma code coverage -->
1174
1175    <target name="-set-instrumented-mode" depends="-set-mode-check">
1176        <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-instrumented-unaligned.apk" />
1177        <property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-instrumented.apk" />
1178        <property name="build.is.mode.set" value="true" />
1179
1180        <!-- whether the build is an instrumented build. -->
1181        <property name="build.is.instrumented" value="true" />
1182    </target>
1183
1184    <!-- Builds instrumented output package -->
1185    <target name="instrument" depends="-set-instrumented-mode, -do-debug"
1186                description="Builds an instrumented packaged.">
1187        <!-- only create apk if *not* a library project -->
1188        <do-only-if-not-library elseText="Library project: do not create apk..." >
1189            <sequential>
1190                <zipalign-helper in.package="${out.packaged.file}" out.package="${out.final.file}" />
1191                <echo level="info">Instrumented Package: ${out.final.file}</echo>
1192            </sequential>
1193        </do-only-if-not-library>
1194        <record-build-info />
1195    </target>
1196
1197    <!-- ******************************************************* -->
1198    <!-- ************ Test project specific targets ************ -->
1199    <!-- ******************************************************* -->
1200
1201    <!-- enable code coverage -->
1202    <target name="emma">
1203        <property name="emma.enabled" value="true" />
1204    </target>
1205
1206    <!-- fails if the project is not a test project -->
1207    <target name="-test-project-check" depends="-setup">
1208        <if>
1209            <condition>
1210                <and>
1211                    <isfalse value="${project.is.test}" />
1212                    <isfalse value="${project.is.testapp}" />
1213                </and>
1214            </condition>
1215            <then>
1216                <fail message="Project is not a test project." />
1217            </then>
1218        </if>
1219    </target>
1220
1221    <target name="test" depends="-test-project-check"
1222                description="Runs tests from the package defined in test.package property">
1223        <property name="test.runner" value="android.test.InstrumentationTestRunner" />
1224
1225        <if condition="${project.is.test}">
1226        <then>
1227            <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
1228
1229            <!-- Application package of the tested project extracted from its manifest file -->
1230            <xpath input="${tested.project.absolute.dir}/AndroidManifest.xml"
1231                    expression="/manifest/@package" output="tested.project.app.package" />
1232
1233            <if condition="${emma.enabled}">
1234                <then>
1235                    <getprojectpaths projectPath="${tested.project.absolute.dir}"
1236                            binOut="tested.project.out.absolute.dir"
1237                            srcOut="tested.project.source.absolute.dir" />
1238
1239                    <getlibpath projectPath="${tested.project.absolute.dir}"
1240                            libraryFolderPathOut="tested.project.lib.source.path"
1241                            leaf="@{source.dir}" />
1242
1243                </then>
1244            </if>
1245
1246        </then>
1247        <else>
1248            <!-- this is a test app, the tested package is the app's own package -->
1249            <property name="tested.project.app.package" value="${project.app.package}" />
1250
1251            <if condition="${emma.enabled}">
1252                <then>
1253                    <property name="tested.project.out.absolute.dir" value="${out.absolute.dir}" />
1254                    <property name="tested.project.source.absolute.dir" value="${source.absolute.dir}" />
1255
1256                    <getlibpath
1257                            libraryFolderPathOut="tested.project.lib.source.path"
1258                            leaf="@{source.dir}" />
1259
1260                </then>
1261            </if>
1262
1263        </else>
1264        </if>
1265
1266        <property name="emma.dump.file"
1267                value="/data/data/${tested.project.app.package}/coverage.ec" />
1268
1269        <if condition="${emma.enabled}">
1270            <then>
1271                <echo>Running tests...</echo>
1272                <run-tests-helper emma.enabled="true">
1273                    <extra-instrument-args>
1274                        <arg value="-e" />
1275                           <arg value="coverageFile" />
1276                           <arg value="${emma.dump.file}" />
1277                    </extra-instrument-args>
1278                </run-tests-helper>
1279
1280                <echo level="info">Setting permission to download the coverage file...</echo>
1281                <exec executable="${adb}" failonerror="true">
1282                    <arg line="${adb.device.arg}" />
1283                    <arg value="shell" />
1284                    <arg value="run-as" />
1285                    <arg value="${tested.project.app.package}" />
1286                    <arg value="chmod" />
1287                    <arg value="644" />
1288                    <arg value="${emma.dump.file}" />
1289                </exec>
1290                <echo level="info">Downloading coverage file into project directory...</echo>
1291                <exec executable="${adb}" failonerror="true">
1292                    <arg line="${adb.device.arg}" />
1293                    <arg value="pull" />
1294                    <arg value="${emma.dump.file}" />
1295                    <arg path="${out.absolute.dir}/coverage.ec" />
1296                </exec>
1297
1298                <pathconvert property="tested.project.lib.source.path.value" refid="tested.project.lib.source.path">
1299                    <firstmatchmapper>
1300                        <regexpmapper from='^([^ ]*)( .*)$$' to='"\1\2"'/>
1301                        <identitymapper/>
1302                    </firstmatchmapper>
1303                </pathconvert>
1304
1305                <echo level="info">Extracting coverage report...</echo>
1306                <emma>
1307                    <property name="report.html.out.encoding" value="UTF-8" />
1308                    <report sourcepath="${tested.project.source.absolute.dir}:${tested.project.lib.source.path.value}"
1309                            verbosity="${verbosity}">
1310                        <!-- TODO: report.dir or something like should be introduced if necessary -->
1311                        <infileset file="${out.absolute.dir}/coverage.ec" />
1312                        <infileset file="${tested.project.out.absolute.dir}/coverage.em" />
1313                        <!-- TODO: reports in other, indicated by user formats -->
1314                        <html outfile="${out.absolute.dir}/coverage.html" />
1315                        <txt outfile="${out.absolute.dir}/coverage.txt" />
1316                        <xml outfile="${out.absolute.dir}/coverage.xml" />
1317                   </report>
1318                </emma>
1319                <echo level="info">Cleaning up temporary files...</echo>
1320                <delete file="${out.absolute.dir}/coverage.ec" />
1321                <delete file="${tested.project.out.absolute.dir}/coverage.em" />
1322                <exec executable="${adb}" failonerror="true">
1323                    <arg line="${adb.device.arg}" />
1324                    <arg value="shell" />
1325                    <arg value="run-as" />
1326                    <arg value="${tested.project.app.package}" />
1327                    <arg value="rm" />
1328                    <arg value="${emma.dump.file}" />
1329                </exec>
1330                <echo level="info">Saving the coverage reports in ${out.absolute.dir}</echo>
1331            </then>
1332            <else>
1333                <run-tests-helper />
1334            </else>
1335        </if>
1336    </target>
1337
1338    <!-- ******************************************************* -->
1339    <!-- **********        Run Lint on the project     ********* -->
1340    <!-- ******************************************************* -->
1341
1342    <target name="lint"
1343            description="Runs lint on the project to look for potential bugs" >
1344        <lint executable="${lint}"
1345              html="${lint.out.html}"
1346              xml="${lint.out.xml}"
1347              src="${source.absolute.dir}:${gen.absolute.dir}"
1348              classpath="${out.classes.absolute.dir}" />
1349    </target>
1350
1351    <!-- ******************************************************* -->
1352    <!-- ********** Install/uninstall specific targets ********* -->
1353    <!-- ******************************************************* -->
1354
1355    <target name="install"
1356                description="Installs the newly build package. Must be used in conjunction with a build target
1357                            (debug/release/instrument). If the application was previously installed, the application
1358                            is reinstalled if the signature matches." >
1359        <!-- only do install if *not* a library project -->
1360        <do-only-if-not-library elseText="Library project: nothing to install!" >
1361            <if>
1362                <condition>
1363                    <isset property="out.final.file" />
1364                </condition>
1365                <then>
1366                    <if>
1367                        <condition>
1368                            <resourceexists>
1369                                <file file="${out.final.file}"/>
1370                            </resourceexists>
1371                        </condition>
1372                        <then>
1373                            <echo level="info">Installing ${out.final.file} onto default emulator or device...</echo>
1374                            <exec executable="${adb}" failonerror="true">
1375                                <arg line="${adb.device.arg}" />
1376                                <arg value="install" />
1377                                <arg value="-r" />
1378                                <arg path="${out.final.file}" />
1379                            </exec>
1380
1381                            <!-- now install the tested project if applicable -->
1382                            <!-- can't use project.is.test since the setup target might not have run -->
1383                            <if>
1384                                <condition>
1385                                    <and>
1386                                        <isset property="tested.project.dir" />
1387                                        <not>
1388                                            <isset property="dont.do.deps" />
1389                                        </not>
1390                                    </and>
1391                                </condition>
1392                                <then>
1393                                    <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
1394
1395                                    <!-- figure out which tested package to install based on emma.enabled -->
1396                                    <condition property="tested.project.install.target" value="installi" else="installd">
1397                                        <isset property="emma.enabled" />
1398                                    </condition>
1399                                    <subant target="${tested.project.install.target}" failonerror="true">
1400                                        <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
1401                                    </subant>
1402                                </then>
1403                            </if>
1404                        </then>
1405                        <else>
1406                            <fail message="File ${out.final.file} does not exist." />
1407                        </else>
1408                    </if>
1409                </then>
1410                <else>
1411                    <echo>Install file not specified.</echo>
1412                    <echo></echo>
1413                    <echo>'ant install' now requires the build target to be specified as well.</echo>
1414                    <echo></echo>
1415                    <echo></echo>
1416                    <echo>    ant debug install</echo>
1417                    <echo>    ant release install</echo>
1418                    <echo>    ant instrument install</echo>
1419                    <echo>This will build the given package and install it.</echo>
1420                    <echo></echo>
1421                    <echo>Alternatively, you can use</echo>
1422                    <echo>    ant installd</echo>
1423                    <echo>    ant installr</echo>
1424                    <echo>    ant installi</echo>
1425                    <echo>    ant installt</echo>
1426                    <echo>to only install an existing package (this will not rebuild the package.)</echo>
1427                    <fail />
1428                </else>
1429            </if>
1430        </do-only-if-not-library>
1431    </target>
1432
1433    <target name="installd" depends="-set-debug-files, install"
1434            description="Installs (only) the debug package." />
1435    <target name="installr" depends="-set-release-mode, install"
1436            description="Installs (only) the release package." />
1437    <target name="installi" depends="-set-instrumented-mode, install"
1438            description="Installs (only) the instrumented package." />
1439    <target name="installt" depends="-test-project-check, installd"
1440            description="Installs (only) the test and tested packages." />
1441
1442
1443    <!-- Uninstalls the package from the default emulator/device -->
1444    <target name="uninstall" depends="-setup"
1445                description="Uninstalls the application from a running emulator or device.">
1446        <if>
1447            <condition>
1448                <isset property="project.app.package" />
1449            </condition>
1450            <then>
1451                <uninstall-helper app.package="${project.app.package}" />
1452            </then>
1453            <else>
1454                <fail message="Could not find application package in manifest. Cannot run 'adb uninstall'." />
1455            </else>
1456        </if>
1457
1458        <!-- Now uninstall the tested project, if applicable -->
1459        <if>
1460            <condition>
1461                <and>
1462                    <istrue value="${project.is.test}" />
1463                    <not>
1464                        <isset property="dont.do.deps" />
1465                    </not>
1466                </and>
1467            </condition>
1468            <then>
1469                <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
1470
1471                <!-- Application package of the tested project extracted from its manifest file -->
1472                <xpath input="${tested.project.absolute.dir}/AndroidManifest.xml"
1473                    expression="/manifest/@package" output="tested.project.app.package" />
1474                <if>
1475                    <condition>
1476                        <isset property="tested.project.app.package" />
1477                    </condition>
1478                    <then>
1479                        <uninstall-helper app.package="${tested.project.app.package}" />
1480                    </then>
1481                    <else>
1482                        <fail message="Could not find tested application package in manifest. Cannot run 'adb uninstall'." />
1483                    </else>
1484                </if>
1485            </then>
1486        </if>
1487
1488    </target>
1489
1490
1491    <!-- ******************************************************* -->
1492    <!-- ************************* Help ************************ -->
1493    <!-- ******************************************************* -->
1494
1495    <target name="help">
1496        <!-- displays starts at col 13
1497              |13                                                              80| -->
1498        <echo>Android Ant Build. Available targets:</echo>
1499        <echo>   help:      Displays this help.</echo>
1500        <echo>   clean:     Removes output files created by other targets.</echo>
1501        <echo>              This calls the same target on all dependent projects.</echo>
1502        <echo>              Use 'ant nodeps clean' to only clean the local project</echo>
1503        <echo>   debug:     Builds the application and signs it with a debug key.</echo>
1504        <echo>              The 'nodeps' target can be used to only build the</echo>
1505        <echo>              current project and ignore the libraries using:</echo>
1506        <echo>              'ant nodeps debug'</echo>
1507        <echo>   release:   Builds the application. The generated apk file must be</echo>
1508        <echo>              signed before it is published.</echo>
1509        <echo>              The 'nodeps' target can be used to only build the</echo>
1510        <echo>              current project and ignore the libraries using:</echo>
1511        <echo>              'ant nodeps release'</echo>
1512        <echo>   instrument:Builds an instrumented package and signs it with a</echo>
1513        <echo>              debug key.</echo>
1514        <echo>   test:      Runs the tests. Project must be a test project and</echo>
1515        <echo>              must have been built. Typical usage would be:</echo>
1516        <echo>                  ant [emma] debug install test</echo>
1517        <echo>   emma:      Transiently enables code coverage for subsequent</echo>
1518        <echo>              targets.</echo>
1519        <echo>   install:   Installs the newly build package. Must either be used</echo>
1520        <echo>              in conjunction with a build target (debug/release/</echo>
1521        <echo>              instrument) or with the proper suffix indicating</echo>
1522        <echo>              which package to install (see below).</echo>
1523        <echo>              If the application was previously installed, the</echo>
1524        <echo>              application is reinstalled if the signature matches.</echo>
1525        <echo>   installd:  Installs (only) the debug package.</echo>
1526        <echo>   installr:  Installs (only) the release package.</echo>
1527        <echo>   installi:  Installs (only) the instrumented package.</echo>
1528        <echo>   installt:  Installs (only) the test and tested packages (unless</echo>
1529        <echo>              nodeps is used as well.</echo>
1530        <echo>   uninstall: Uninstalls the application from a running emulator or</echo>
1531        <echo>              device. Also uninstall tested package if applicable</echo>
1532        <echo>              unless 'nodeps' is used as well.</echo>
1533    </target>
1534</project>
1535