Advertisement
Guest User

Android build.xml

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