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