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