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