zoukankan      html  css  js  c++  java
  • (转)通过ant脚本,编译打包android工程

    通过ant脚本,编译打包android工程


    1.Android程序编译、打包、签名、发布的三种方式: 
    方式一:命令行手动编译打包 
    方式二:使用ant自动编译打包 
    方式三:使用eclipse+ADT编译打包 

    2.Android编译、打包的步骤: 
    2.1第一步 生成R.java类文件: 
    Eclipse中会自动生成R.java,ant和命令行使用android SDK提供的aapt.ext程序生成R.java。 

    2.2第二步 将.aidl文件生成.java类文件: 
    Eclipse中自动生成,ant和命令行使用android SDK提供的aidl.exe生成.java文件。 

    2.3第三步 编译.java类文件生成class文件: 
    Eclipse中自动生成,ant和命令行使用jdk的javac编译java类文件生成class文件。 

    2.4第四步 将class文件打包生成classes.dex文件: 
    Eclipse中自动生成,ant和命令行使用android SDK提供的dx.bat命令行脚本生成classes.dex文件。 

    2.5第五步 打包资源文件(包括res、assets、androidmanifest.xml等): 
    Eclipse中自动生成,ant和命令行使用Android SDK提供的aapt.exe生成资源包文件。 

    2.6第六步 生成未签名的apk安装文件: 
    Eclipse中自动生成debug签名文件存放在bin目录中,ant和命令行使用android SDK提供的apkbuilder.bat命令脚本生成未签名的apk安装文件。 

    2.7第七步 对未签名的apk进行签名生成签名后的android文件: 

    Eclipse中使用Android Tools进行签名,ant和命令行使用jdk的jarsigner对未签名的包进行apk签名。 

    通过命令打包的脚本:见附件bulid0.xml,这个ant脚本只能编译打包一个单独的android工程或依赖一个library 的android工程


    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <project name="ant" default="release">  
    3.     <!-- ANT环境变量 -->  
    4.     <property environment="env" />  
    5.     <!-- 应用名称 -->  
    6.   
    7.     <property name="appName" value="TestPack" />  
    8.     <property name="basedir" value="" />  
    9.     <property name="library-dir" value="">  
    10.     </property>  
    11.     <!-- SDK目录(获取操作系统环境变量ANDROID_SDK_HOME的值) -->  
    12.     <!-- <property name="sdk-folder" value="${env.ANDROID_SDK_HOME}" /> -->  
    13.     <property name="sdk-folder" value="" />  
    14.     <!-- SDK指定平台目录 -->  
    15.     <property name="sdk-platform-folder" value="${sdk-folder}/platforms/android-4" />  
    16.     <!-- SDK中tools目录 -->  
    17.     <property name="sdk-tools" value="${sdk-folder}/tools" />  
    18.     <!-- SDK指定平台中tools目录 -->  
    19.     <property name="sdk-platform-tools" value="${sdk-folder}/platform-tools" />  
    20.   
    21.     <!-- 使用到的命令(当前系统为windows,如果系统为linux,可将.bat文件替换成相对应的命令) -->  
    22.     <property name="aapt" value="${sdk-platform-tools}/aapt.exe" />  
    23.     <property name="aidl" value="${sdk-platform-tools}/aidl.exe" />  
    24.     <property name="dx" value="${sdk-platform-tools}/dx.bat" />  
    25.     <property name="apkbuilder" value="${sdk-tools}/apkbuilder.bat" />  
    26.     <property name="jarsigner" value="${env.JAVA_HOME}/bin/jarsigner" />  
    27.   
    28.     <!-- 编译需要的jar; 如果项目使用到地图服务则需要maps.jar -->  
    29.     <property name="android-jar" value="${sdk-platform-folder}/android.jar" />  
    30.   
    31.     <!-- 编译aidl文件所需的预处理框架文件framework.aidl -->  
    32.     <property name="framework-aidl" value="${sdk-platform-folder}/framework.aidl" />  
    33.   
    34.     <!-- 生成R文件的相对目录 -->  
    35.     <property name="outdir-gen" value="gen" />  
    36.     <!-- 编译后的文件放置目录 -->  
    37.     <property name="outdir-bin" value="out" />  
    38.   
    39.     <!-- 清单文件 -->  
    40.     <property name="manifest-xml" value="AndroidManifest.xml" />  
    41.     <!-- 源文件目录 -->  
    42.     <property name="resource-dir" value="res" />  
    43.     <property name="asset-dir" value="assets" />  
    44.     <!-- java源文件目录 -->  
    45.     <property name="srcdir" value="src" />  
    46.     <property name="srcdir-ospath" value="${basedir}/${srcdir}" />  
    47.     <!-- 外部类库所在目录 -->  
    48.     <property name="external-lib" value="lib" />  
    49.     <property name="external-lib-ospath" value="${basedir}/${external-lib}" />  
    50.   
    51.     <!-- 生成class目录 -->  
    52.     <property name="outdir-classes" value="${outdir-bin}" />  
    53.     <property name="outdir-classes-ospath" value="${basedir}/${outdir-classes}" />  
    54.   
    55.     <!-- classes.dex相关变量 -->  
    56.     <property name="dex-file" value="classes.dex" />  
    57.     <property name="dex-path" value="${outdir-bin}/${dex-file}" />  
    58.     <property name="dex-ospath" value="${basedir}/${dex-path}" />  
    59.   
    60.     <!-- 经过aapt生成的资源包文件 -->  
    61.     <property name="resources-package" value="${outdir-bin}/resources.ap_" />  
    62.     <property name="resources-package-ospath" value="${basedir}/${resources-package}" />  
    63.   
    64.     <!-- 未认证apk包 -->  
    65.     <property name="out-unsigned-package" value="${outdir-bin}/${appName}-unsigned.apk" />  
    66.     <property name="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" />  
    67.   
    68.     <!-- 证书文件 -->  
    69.     <property name="keystore-file" value="${basedir}/release.keystore" />  
    70.   
    71.     <!-- 已认证apk包 -->  
    72.     <property name="out-signed-package" value="${outdir-bin}/${appName}.apk" />  
    73.     <property name="out-signed-package-ospath" value="${basedir}/${out-signed-package}" />  
    74.   
    75.   
    76.     <!-- 初始化工作 -->  
    77.     <target name="init">  
    78.         <echo>Initializing all output directories...</echo>  
    79.         <delete dir="${outdir-bin}" />  
    80.         <mkdir dir="${outdir-bin}" />  
    81.         <mkdir dir="${outdir-classes}" />  
    82.     </target>  
    83.   
    84.     <!-- 根据工程中的资源文件生成R.java文件 -->  
    85.     <target name="gen-R" depends="init">  
    86.         <echo>Generating R.java from the resources...</echo>  
    87.         <!--<exec executable="${aapt}" failonerror="true">  
    88.             <arg value="package" />  
    89.             <arg value="-f" />  
    90.             <arg value="-m" />  
    91.             <arg value="-J" />  
    92.             <arg value="${outdir-gen}" />  
    93.             <arg value="-S" />  
    94.             <arg value="${resource-dir}" />  
    95.             <arg value="-M" />  
    96.             <arg value="${manifest-xml}" />  
    97.             <arg value="-I" />  
    98.             <arg value="${android-jar}" />  
    99.         </exec>-->  
    100.   
    101.   
    102.         <exec executable="${aapt}" failonerror="true">  
    103.   
    104.             <arg value="package" />  
    105.   
    106.             <arg value="-m" />  
    107.   
    108.             <arg value="--auto-add-overlay" />  
    109.   
    110.             <arg value="-J" />  
    111.   
    112.             <arg value="${outdir-gen}" />  
    113.   
    114.             <arg value="-M" />  
    115.   
    116.             <arg value="${manifest-xml}" />  
    117.   
    118.             <arg value="-S" />  
    119.   
    120.             <arg value="${resource-dir}" />  
    121.   
    122.             <arg value="-S" />  
    123.   
    124.             <arg value="${library-dir}/${resource-dir}" />  
    125.   
    126.             <arg value="--extra-packages" />  
    127.   
    128.             <arg value="com.mobcent.share.android" />  
    129.   
    130.             <arg value="-A" />  
    131.   
    132.             <arg value="${asset-dir}" />  
    133.   
    134.             <arg value="-I" />  
    135.   
    136.             <arg value="${android-jar}" />  
    137.   
    138.         </exec>  
    139.     </target>  
    140.   
    141.   
    142.     <!-- 编译aidl文件 -->  
    143.     <target name="aidl" depends="gen-R">  
    144.         <echo>Compiling .aidl into java files...</echo>  
    145.         <apply executable="${aidl}" failonerror="true">  
    146.             <!-- 指定预处理文件 -->  
    147.             <arg value="-p${framework-aidl}" />  
    148.             <!-- aidl声明的目录 -->  
    149.             <arg value="-I${srcdir}" />  
    150.             <!-- 目标文件目录 -->  
    151.             <arg value="-o${outdir-gen}" />  
    152.             <!-- 指定哪些文件需要编译 -->  
    153.             <fileset dir="${srcdir}">  
    154.                 <include name="**/*.aidl" />  
    155.             </fileset>  
    156.         </apply>  
    157.     </target>  
    158.   
    159.     <!-- 将工程中的java源文件编译成class文件 -->  
    160.     <target name="compile" depends="aidl">  
    161.         <echo>Compiling java source code...</echo>  
    162.         <javac encoding="utf-8" target="1.6" destdir="${outdir-classes}" bootclasspath="${android-jar}">  
    163.             <src path="src" />  
    164.             <src path="gen" />  
    165.             <src path="${library-dir}/src" />  
    166.             <classpath>  
    167.                 <fileset dir="${external-lib}" includes="*.jar" />  
    168.                 <fileset dir="${library-dir}/libs" includes="*.jar" />  
    169.                 <filelist>  
    170.                     <file name="${android-maps-jar}" />  
    171.                 </filelist>  
    172.             </classpath>  
    173.         </javac>  
    174.     </target>  
    175.   
    176.     <!-- 将.class文件转化成.dex文件 -->  
    177.     <target name="dex" depends="compile">  
    178.         <echo>Converting compiled files and external libraries into a .dex  
    179.             file...  
    180.         </echo>  
    181.         <exec executable="${dx}" failonerror="true">  
    182.             <arg value="--dex" />  
    183.             <!-- 输出文件 -->  
    184.             <arg value="--output=${dex-ospath}" />  
    185.             <!-- 要生成.dex文件的源classes和libraries -->  
    186.             <arg value="${outdir-classes-ospath}" />  
    187.             <arg value="${external-lib-ospath}" />  
    188.             <arg value="${library-dir}/libs" />  
    189.         </exec>  
    190.     </target>  
    191.   
    192.   
    193.     <!-- 将资源文件放进输出目录 -->  
    194.     <!--在这截断-->  
    195.     <target name="package-res-and-assets" depends="dex">  
    196.         <echo>Packaging resources and assets...</echo>  
    197.         <exec executable="${aapt}" failonerror="true">  
    198.             <arg value="package" />  
    199.             <arg value="-f" />  
    200.             <arg value="-M" />  
    201.             <arg value="${manifest-xml}" />  
    202.             <arg value="-S" />  
    203.             <arg value="${resource-dir}" />  
    204.   
    205.             <arg value="-A" />  
    206.             <arg value="${asset-dir}" />  
    207.   
    208.             <arg value="-S" />  
    209.             <arg value="${library-dir}/${resource-dir}" />  
    210.   
    211.             <arg value="-A" />  
    212.             <arg value="${library-dir}/${asset-dir}" />  
    213.   
    214.             <arg value="-I" />  
    215.             <arg value="${android-jar}" />  
    216.             <arg value="-F" />  
    217.             <arg value="${resources-package}" />  
    218.   
    219.             <arg value="--auto-add-overlay" />  
    220.         </exec>  
    221.     </target>  
    222.   
    223.   
    224.     <!-- 打包成未签证的apk -->  
    225.     <target name="package" depends="dex, package-res-and-assets">  
    226.         <echo>Packaging unsigned apk for release...</echo>  
    227.         <exec executable="${apkbuilder}" failonerror="true">  
    228.             <arg value="${out-unsigned-package-ospath}" />  
    229.             <arg value="-u" />  
    230.               
    231.             <arg value="-z" />  
    232.             <arg value="${resources-package-ospath}" />  
    233.             <arg value="-f" />  
    234.             <arg value="${dex-ospath}" />  
    235.             <arg value="-rf" />  
    236.             <arg value="${srcdir-ospath}" />  
    237.             <arg value="-nf"/>  
    238.             <arg value="${library-dir}/libs"/>  
    239.         </exec>  
    240.         <echo>It will need to be signed with jarsigner before being published.  
    241.         </echo>  
    242.     </target>  
    243.   
    244.   
    245.     <!-- 对apk进行签证 -->  
    246.     <target name="jarsigner" depends="package">  
    247.         <echo>Packaging signed apk for release...</echo>  
    248.         <exec executable="${jarsigner}" failonerror="true">  
    249.             <arg value="-keystore" />  
    250.             <arg value="${keystore-file}" />  
    251.             <arg value="-storepass" />  
    252.             <arg value="" />  
    253.             <arg value="-keypass" />  
    254.             <arg value="" />  
    255.             <arg value="-signedjar" />  
    256.             <arg value="${out-signed-package-ospath}" />  
    257.             <arg value="${out-unsigned-package-ospath}" />  
    258.             <!-- 不要忘了证书的别名 -->  
    259.             <arg value="" />  
    260.         </exec>  
    261.     </target>  
    262.   
    263.     <!-- 发布 -->  
    264.     <target name="release" depends="jarsigner">  
    265.         <!-- 删除未签证apk -->  
    266.         <delete file="${out-unsigned-package-ospath}" />  
    267.         <echo>APK is released. path:${out-signed-package-ospath}</echo>  
    268.     </target>  
    269.   
    270.   
    271. </project>  

    Android官方提供的打包脚本: 1400多行,我加了中文注释,希望能看懂。

    详见build.xml

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <project name="pet_dog_base_forum" default="release">  
    3.     <!--自己需要添加的属性 -->  
    4.     <property name="sdk.dir" value="C:/Program Files/android-sdk_r15-windows/android-sdk-windows" />  
    5.     <!--导入project.properties 文件,设置了编译的target 和相关的library工程-->  
    6.     <property file="project.properties" />  
    7.     <!--导入build.properties文件,设置了android的目录和key-->  
    8.     <property file="ant.properties" />  
    9.     <!--  
    10.         This build file is imported by the project build file. It contains  
    11.         all the targets and tasks necessary to build Android projects, be they  
    12.         regular projects, library projects, or test projects.  
    13.   
    14.         At the beginning of the file is a list of properties that can be overridden  
    15.         by adding them to your build.properties (properties are immutable, so their  
    16.         first definition sticks and is never changed).  
    17.   
    18.         Follows:   
    19.         - custom task definitions,  
    20.         - more properties (do not override those unless the whole build system is modified).  
    21.         - macros used throughout the build,  
    22.         - base build targets,  
    23.         - debug-specific build targets,  
    24.         - release-specific build targets,  
    25.         - instrument-specific build targets,  
    26.         - test project-specific build targets,  
    27.         - install targets,  
    28.         - help target  
    29.           
    30.                        步骤如下:  
    31.         —— 自定义task  
    32.         —— 设置相关属性  
    33.         —— 全局的使用整个构建  
    34.         —— 基本bulid的targets    
    35.         —— debug使用的targets  
    36.         —— release使用的targets         
    37.         —— 特定仪器使用的targets  
    38.         —— 测试使用的targets  
    39.         —— 安装的targets  
    40.         —— 帮助的targets                
    41.     -->  
    42.   
    43.     <!-- ********** Overrideable Properties ********** -->  
    44.     <!-- ********** 可重写的属性 ********** -->  
    45.   
    46.     <!-- You can override these values in your build.xml or build.properties.  
    47.          Overriding any other properties may result in broken build. -->  
    48.     <!-- 你可以覆盖build.xml 或者 build.properties 文件中的任何属性,覆盖任何一个属性,都可能导致build出错,慎用 -->  
    49.   
    50.     <!-- Tells adb which device to target. You can change this from the command line  
    51.          by invoking "ant -Dadb.device.arg=-d" for device "ant -Dadb.device.arg=-e" for  
    52.          the emulator. -->  
    53.     <!-- 设置链接的机器,  
    54.     ant -Dadb.device.arg=-d 使用链接当前的设备  
    55.     ant -Dadb.device.arg=-e 使用模拟器  
    56.     -->  
    57.   
    58.     <property name="adb.device.arg" value="" />  
    59.   
    60.   
    61.     <!-- fileset exclude patterns (space separated) to prevent  
    62.          files inside src/ from being packaged. -->  
    63.     <!-- 设置改属性可以排除编译一部分代码 
    64.     -->  
    65.     <property name="android.package.excludes" value="" />  
    66.   
    67.     <!-- set some properties used for filtering/override. If those weren't defined  
    68.          before, then this will create them with empty values, which are then ignored  
    69.          by the custom tasks receiving them. -->  
    70.     <!-- 
    71.     version.code,version.name可以替換AndroidManifest.xml中的android:versionCode和android:versionName 
    72.     -->  
    73.     <property name="version.code" value="11" />  
    74.     <property name="version.name" value="111" />  
    75.     <property name="aapt.resource.filter" value="" />  
    76.   
    77.     <!-- compilation options -->  
    78.     <property name="java.encoding" value="UTF-8" />  
    79.     <property name="java.target" value="1.6" />  
    80.     <property name="java.source" value="1.6" />  
    81.   
    82.     <!-- Verbosity -->  
    83.     <property name="verbose" value="false" />  
    84.   
    85.     <!-- ********** Custom Tasks ********** -->  
    86.     <!-- ********** 自定义Tasks ********** -->  
    87.       
    88.     <!-- 导入自定义Task是需要的文件 -->  
    89.     <path id="android.antlibs">  
    90.         <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />  
    91.     </path>  
    92.   
    93.     <!-- Custom tasks -->  
    94.     <taskdef name="setup" classname="com.android.ant.NewSetupTask" classpathref="android.antlibs" />  
    95.   
    96.     <taskdef name="aapt" classname="com.android.ant.AaptExecTask" classpathref="android.antlibs" />  
    97.   
    98.     <taskdef name="aidl" classname="com.android.ant.AidlExecTask" classpathref="android.antlibs" />  
    99.   
    100.     <taskdef name="renderscript" classname="com.android.ant.RenderScriptTask" classpathref="android.antlibs" />  
    101.   
    102.     <taskdef name="dex" classname="com.android.ant.DexExecTask" classpathref="android.antlibs" />  
    103.   
    104.     <taskdef name="apkbuilder" classname="com.android.ant.ApkBuilderTask" classpathref="android.antlibs" />  
    105.   
    106.     <taskdef name="zipalign" classname="com.android.ant.ZipAlignTask" classpathref="android.antlibs" />  
    107.   
    108.     <taskdef name="xpath" classname="com.android.ant.XPathTask" classpathref="android.antlibs" />  
    109.   
    110.     <taskdef name="if" classname="com.android.ant.IfElseTask" classpathref="android.antlibs" />  
    111.   
    112.     <!-- Emma configuration   
    113.     EMMA 是一种快速的,基于字节码指令的Java 代码覆盖工具。  
    114.     -->  
    115.     <property name="emma.dir" value="${sdk.dir}/tools/lib" />  
    116.     <path id="emma.lib">  
    117.         <pathelement location="${emma.dir}/emma.jar" />  
    118.         <pathelement location="${emma.dir}/emma_ant.jar" />  
    119.     </path>  
    120.     <taskdef resource="emma_ant.properties" classpathref="emma.lib" />  
    121.     <!-- End of emma configuration -->  
    122.   
    123.   
    124.     <!-- ********** Other Properties ********** -->  
    125.     <!-- overriding these properties may break the build  
    126.          unless the whole file is updated -->  
    127.   
    128.     <!-- 输入文件 -->  
    129.     <property name="source.dir" value="src" />  
    130.     <property name="source.absolute.dir" location="${source.dir}" />  
    131.     <property name="gen.absolute.dir" location="gen" />  
    132.     <property name="resource.absolute.dir" location="res" />  
    133.     <property name="asset.absolute.dir" location="assets" />  
    134.     <property name="jar.libs.dir" value="libs" />  
    135.     <property name="jar.libs.absolute.dir" location="${jar.libs.dir}" />  
    136.     <property name="native.libs.absolute.dir" location="libs" />  
    137.   
    138.     <!-- 输出文件 -->  
    139.     <property name="out.dir" value="bin" />  
    140.     <property name="out.absolute.dir" location="${out.dir}" />  
    141.     <property name="out.classes.absolute.dir" location="${out.dir}/classes" />  
    142.     <property name="out.res.absolute.dir" location="${out.dir}/res" />  
    143.   
    144.     <!-- tools location 编译所需要用到的工具 -->  
    145.     <property name="android.tools.dir" location="${sdk.dir}/tools" />  
    146.     <property name="android.platform.tools.dir" location="${sdk.dir}/platform-tools" />  
    147.     <condition property="exe" value=".exe" else="">  
    148.         <os family="windows" />  
    149.     </condition>  
    150.     <condition property="bat" value=".bat" else="">  
    151.         <os family="windows" />  
    152.     </condition>  
    153.     <property name="adb" location="${android.platform.tools.dir}/adb${exe}" />  
    154.     <property name="zipalign" location="${android.tools.dir}/zipalign${exe}" />  
    155.     <property name="aidl" location="${android.platform.tools.dir}/aidl${exe}" />  
    156.     <property name="aapt" location="${android.platform.tools.dir}/aapt${exe}" />  
    157.     <property name="dx" location="${android.platform.tools.dir}/dx${bat}" />  
    158.     <!-- renderscript location is set by NewSetupTask since we have a choice of  
    159.          several executables based on minSdkVersion -->  
    160.   
    161.     <!-- Intermediate files 中间文件 -->  
    162.     <property name="dex.file.name" value="classes.dex" />  
    163.     <property name="intermediate.dex.file" location="${out.absolute.dir}/${dex.file.name}" />  
    164.     <property name="resource.package.file.name" value="${ant.project.name}.ap_" />  
    165.   
    166.     <!-- Build property file build的属性文件 -->  
    167.     <property name="out.build.prop.file" location="${out.absolute.dir}/build.prop" />  
    168.   
    169.   
    170.     <!-- This is needed by emma as it uses multilevel verbosity instead of simple 'true' or 'false'  
    171.          The property 'verbosity' is not user configurable and depends exclusively on 'verbose'  
    172.          value.  
    173.          这是需要通过艾玛,因为它使用多级verbosity不是简单的“true”或“false”。属性“冗长”不是用户可配置的,只取决于verbose”值。  
    174.          -->  
    175.     <condition property="verbosity" value="verbose" else="quiet">  
    176.         <istrue value="${verbose}" />  
    177.     </condition>  
    178.   
    179.     <!-- properties for signing in release mode -->  
    180.     <!-- 签名所需要的文件 -->  
    181.     <condition property="has.keystore">  
    182.         <and>  
    183.             <isset property="key.store" />  
    184.             <length string="${key.store}" when="greater" length="0" />  
    185.             <isset property="key.alias" />  
    186.         </and>  
    187.     </condition>  
    188.     <condition property="has.password">  
    189.         <and>  
    190.             <isset property="has.keystore" />  
    191.             <isset property="key.store.password" />  
    192.             <isset property="key.alias.password" />  
    193.         </and>  
    194.     </condition>  
    195.   
    196.     <!-- properties for packaging -->  
    197.     <property name="build.packaging.nocrunch" value="true" />  
    198.   
    199.     <!-- ********** Macros ********** -->  
    200.     <!-- ********** 宏定义         ********** -->  
    201.   
    202.     <!-- macro to do a task on if project.is.library is false.  
    203.          elseText attribute is displayed otherwise -->  
    204.     <!-- 定义了没有关联library工程时,将会打印elseText -->  
    205.     <macrodef name="do-only-if-not-library">  
    206.         <attribute name="elseText" />  
    207.         <element name="task-to-do" implicit="yes" />  
    208.         <sequential>  
    209.             <if condition="${project.is.library}">  
    210.                 <else>  
    211.                     <task-to-do />  
    212.                 </else>  
    213.                 <then>  
    214.                     <echo>@{elseText}</echo>  
    215.                 </then>  
    216.             </if>  
    217.         </sequential>  
    218.     </macrodef>  
    219.   
    220.     <!-- macro to do a task on if manifest.hasCode is true.  
    221.          elseText attribute is displayed otherwise -->  
    222.     <macrodef name="do-only-if-manifest-hasCode">  
    223.         <attribute name="elseText" default="" />  
    224.         <element name="task-to-do" implicit="yes" />  
    225.         <sequential>  
    226.             <if condition="${manifest.hasCode}">  
    227.                 <then>  
    228.                     <task-to-do />  
    229.                 </then>  
    230.                 <else>  
    231.                     <if>  
    232.                         <condition>  
    233.                             <length string="@{elseText}" trim="true" when="greater" length="0" />  
    234.                         </condition>  
    235.                         <then>  
    236.                             <echo>@{elseText}</echo>  
    237.                         </then>  
    238.                     </if>  
    239.                 </else>  
    240.             </if>  
    241.         </sequential>  
    242.     </macrodef>  
    243.   
    244.   
    245.     <!-- Configurable macro, which allows to pass as parameters output directory,  
    246.          output dex filename and external libraries to dex (optional)  
    247.          配置宏,允许通过参数设置输出的目录,dex文件和dex额外的libraries  
    248.           -->  
    249.     <macrodef name="dex-helper">  
    250.         <element name="external-libs" optional="yes" />  
    251.         <attribute name="nolocals" default="false" />  
    252.         <sequential>  
    253.             <!-- sets the primary input for dex. If a pre-dex task sets it to  
    254.                  something else this has no effect -->  
    255.             <property name="out.dex.input.absolute.dir" value="${out.classes.absolute.dir}" />  
    256.   
    257.             <!-- set the secondary dx input: the project (and library) jar files  
    258.                  If a pre-dex task sets it to something else this has no effect -->  
    259.             <if>  
    260.                 <condition>  
    261.                     <isreference refid="out.dex.jar.input.ref" />  
    262.                 </condition>  
    263.                 <else>  
    264.                     <path id="out.dex.jar.input.ref">  
    265.                         <path refid="jar.libs.ref" />  
    266.                     </path>  
    267.                 </else>  
    268.             </if>  
    269.   
    270.             <dex executable="${dx}" output="${intermediate.dex.file}" nolocals="@{nolocals}" verbose="${verbose}" previousBuildType="${build.last.target}" buildType="${build.target}">  
    271.                 <path path="${out.dex.input.absolute.dir}" />  
    272.                 <path refid="out.dex.jar.input.ref" />  
    273.                 <external-libs />  
    274.             </dex>  
    275.         </sequential>  
    276.     </macrodef>  
    277.   
    278.     <!-- This is macro that enable passing variable list of external jar files to ApkBuilder  
    279.     设置ApkBuilder 是额外的jar文件  
    280.     默认把工程下libs中的jar文件打到APK里  
    281.          Example of use:  
    282.          <package-helper>  
    283.              <extra-jars>  
    284.                 <jarfolder path="my_jars" />  
    285.                 <jarfile path="foo/bar.jar" />  
    286.                 <jarfolder path="your_jars" />  
    287.              </extra-jars>  
    288.          </package-helper> -->  
    289.     <macrodef name="package-helper">  
    290.         <element name="extra-jars" optional="yes" />  
    291.         <sequential>  
    292.             <apkbuilder outfolder="${out.absolute.dir}" resourcefile="${resource.package.file.name}" apkfilepath="${out.packaged.file}" debugpackaging="${build.is.packaging.debug}" debugsigning="${build.is.signing.debug}" verbose="${verbose}" hascode="${manifest.hasCode}" previousBuildType="${build.last.is.packaging.debug}/${build.last.is.signing.debug}" buildType="${build.is.packaging.debug}/${build.is.signing.debug}">  
    293.                 <dex path="${intermediate.dex.file}" />  
    294.                 <sourcefolder path="${source.absolute.dir}" />  
    295.                 <jarfile refid="jar.libs.ref" />  
    296.                 <nativefolder path="${native.libs.absolute.dir}" />  
    297.                 <nativefolder refid="project.libraries.libs" />  
    298.                 <extra-jars />  
    299.             </apkbuilder>  
    300.         </sequential>  
    301.     </macrodef>  
    302.   
    303.     <!-- This is macro which zipaligns in.package and outputs it to out.package. Used by targets  
    304.          debug, -debug-with-emma and release.  
    305.          通过zipaligns 对APK进行优化  
    306.          -->  
    307.     <macrodef name="zipalign-helper">  
    308.         <attribute name="in.package" />  
    309.         <attribute name="out.package" />  
    310.         <sequential>  
    311.             <zipalign executable="${zipalign}" input="@{in.package}" output="@{out.package}" verbose="${verbose}" />  
    312.         </sequential>  
    313.     </macrodef>  
    314.   
    315.     <macrodef name="run-tests-helper">  
    316.         <attribute name="emma.enabled" default="false" />  
    317.         <element name="extra-instrument-args" optional="yes" />  
    318.         <sequential>  
    319.             <echo>Running tests ...</echo>  
    320.             <exec executable="${adb}" failonerror="true">  
    321.                 <arg line="${adb.device.arg}" />  
    322.                 <arg value="shell" />  
    323.                 <arg value="am" />  
    324.                 <arg value="instrument" />  
    325.                 <arg value="-w" />  
    326.                 <arg value="-e" />  
    327.                 <arg value="coverage" />  
    328.                 <arg value="@{emma.enabled}" />  
    329.                 <extra-instrument-args />  
    330.                 <arg value="${manifest.package}/${test.runner}" />  
    331.             </exec>  
    332.         </sequential>  
    333.     </macrodef>  
    334.   
    335.     <macrodef name="record-build-key">  
    336.         <attribute name="key" default="false" />  
    337.         <attribute name="value" default="false" />  
    338.         <sequential>  
    339.             <propertyfile file="${out.build.prop.file}" comment="Last build type">  
    340.                 <entry key="@{key}" value="@{value}" />  
    341.             </propertyfile>  
    342.         </sequential>  
    343.     </macrodef>  
    344.   
    345.     <macrodef name="record-build-info">  
    346.         <sequential>  
    347.             <record-build-key key="build.last.target" value="${build.target}" />  
    348.             <record-build-key key="build.last.is.instrumented" value="${build.is.instrumented}" />  
    349.             <record-build-key key="build.last.is.packaging.debug" value="${build.is.packaging.debug}" />  
    350.             <record-build-key key="build.last.is.signing.debug" value="${build.is.signing.debug}" />  
    351.         </sequential>  
    352.     </macrodef>  
    353.   
    354.     <macrodef name="uninstall-helper">  
    355.         <attribute name="app.package" default="false" />  
    356.         <sequential>  
    357.             <echo>Uninstalling @{app.package} from the default emulator or device...</echo>  
    358.             <exec executable="${adb}" failonerror="true">  
    359.                 <arg line="${adb.device.arg}" />  
    360.                 <arg value="uninstall" />  
    361.                 <arg value="@{app.package}" />  
    362.             </exec>  
    363.         </sequential>  
    364.     </macrodef>  
    365.   
    366.     <!-- ********** Build Targets ********** -->  
    367.   
    368.     <!-- this target simply force running -setup making  
    369.          the project info be read. To be used as  
    370.              ant all clean  
    371.          to clean the main project as well as the libraries and tested project  
    372.          运行-setup,在此之前必须运行clean,  
    373.           -->  
    374.     <target name="all" depends="-setup" />  
    375.   
    376.     <!-- clean target -->  
    377.     <target name="clean" description="Removes output files created by other targets.">  
    378.         <delete dir="${out.absolute.dir}" verbose="${verbose}" />  
    379.         <delete dir="${gen.absolute.dir}" verbose="${verbose}" />  
    380.   
    381.         <!-- if we know about a tested project or libraries, we clean them too. This  
    382.              will only work if the target 'all' was called first -->  
    383.         <if condition="${project.is.test}">  
    384.             <then>  
    385.                 <property name="tested.project.absolute.dir" location="${tested.project.dir}" />  
    386.                 <subant failonerror="true">  
    387.                     <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />  
    388.                     <target name="all" />  
    389.                     <target name="clean" />  
    390.                 </subant>  
    391.             </then>  
    392.         </if>  
    393.   
    394.         <if>  
    395.             <condition>  
    396.                 <isreference refid="project.libraries" />  
    397.             </condition>  
    398.             <then>  
    399.                 <!-- 有libraries关联工程的时候,调用libraries工程中build.xml -->  
    400.                 <subant buildpathref="project.libraries" antfile="build.xml" failonerror="true">  
    401.                     <target name="all" />  
    402.                     <target name="clean" />  
    403.                 </subant>  
    404.             </then>  
    405.         </if>  
    406.     </target>  
    407.   
    408.     <!-- generic setup 初始化-->  
    409.     <target name="-setup">  
    410.         <if>  
    411.             <condition>  
    412.                 <not>  
    413.                     <isset property="setup.done" />  
    414.                 </not>  
    415.             </condition>  
    416.             <then>  
    417.                 <property name="setup.done" value="true" />  
    418.                 <echo>Gathering info for ${ant.project.name}...</echo>  
    419.                 <!-- load project properties, resolve Android target, library dependencies  
    420.                      and set some properties with the results.  
    421.                      All property names are passed as parameters ending in -Out  
    422.                                                             加载project properties,设置 Android target,依赖的library工程和一些其他的属性  
    423.                       -->  
    424.                 <setup projectTypeOut="android.project.type" androidJarFileOut="android.jar" androidAidlFileOut="android.aidl" renderScriptExeOut="renderscript" renderScriptIncludeDirOut="android.rs" bootclasspathrefOut="android.target.classpath" projectLibrariesRootOut="project.libraries" projectLibrariesJarsOut="project.libraries.jars" projectLibrariesResOut="project.libraries.res" projectLibrariesPackageOut="project.libraries.package" projectLibrariesLibsOut="project.libraries.libs" targetApiOut="target.api" />  
    425.   
    426.                 <!-- sets a few boolean based on android.project.type  
    427.                      to make the if task easier -->  
    428.                 <condition property="project.is.library" else="false">  
    429.                     <equals arg1="${android.project.type}" arg2="library" />  
    430.                 </condition>  
    431.                 <condition property="project.is.test" else="false">  
    432.                     <equals arg1="${android.project.type}" arg2="test" />  
    433.                 </condition>  
    434.   
    435.                 <!-- If a test project, resolve absolute path to tested project. -->  
    436.                 <if condition="${project.is.test}">  
    437.                     <then>  
    438.                         <property name="tested.project.absolute.dir" location="${tested.project.dir}" />  
    439.                     </then>  
    440.                 </if>  
    441.             </then>  
    442.         </if>  
    443.     </target>  
    444.   
    445.     <!-- Pre build setup   
    446.     预编译  
    447.     -->  
    448.     <target name="-build-setup" depends="-setup">  
    449.   
    450.         <!-- read the previous build mode -->  
    451.         <property file="${out.build.prop.file}" />  
    452.         <!-- if empty the prop won't be set, so set it to the current target  
    453.              to provide a default value equal to the current build -->  
    454.         <property name="build.last.target" value="${build.target}" />  
    455.         <!-- also set the default value for whether the build is instrumented -->  
    456.         <property name="build.last.is.instrumented" value="${build.is.instrumented}" />  
    457.         <property name="build.last.is.packaging.debug" value="${build.is.packaging.debug}" />  
    458.         <property name="build.last.is.signing.debug" value="${build.is.signing.debug}" />  
    459.   
    460.         <!-- compile the libraries if any  
    461.         编译libraries  
    462.          -->  
    463.         <if>  
    464.             <condition>  
    465.                 <isreference refid="project.libraries" />  
    466.             </condition>  
    467.             <then>  
    468.                 <echo>Building Libraries</echo>  
    469.                 <subant buildpathref="project.libraries" antfile="build.xml" target="${build.target}" failonerror="true" />  
    470.                 <echo>  
    471.                 </echo>  
    472.                 <echo>############################################</echo>  
    473.                 <echo>**** Back to project ${ant.project.name} ****</echo>  
    474.                 <echo>############################################</echo>  
    475.             </then>  
    476.         </if>  
    477.   
    478.         <!-- compile the main project if this is a test project  
    479.         编译主工程,如果这是测试工程  
    480.          -->  
    481.         <if condition="${project.is.test}">  
    482.             <then>  
    483.                 <!-- figure out which target must be used to build the tested project.  
    484.                      If emma is enabled, then use 'instrument' otherwise, use 'debug' -->  
    485.                 <condition property="tested.project.target" value="instrument" else="debug">  
    486.                     <isset property="emma.enabled" />  
    487.                 </condition>  
    488.   
    489.                 <echo>Building tested project at ${tested.project.absolute.dir}</echo>  
    490.                 <subant target="${tested.project.target}" failonerror="true">  
    491.                     <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />  
    492.                 </subant>  
    493.                 <echo>  
    494.                 </echo>  
    495.                 <echo>############################################</echo>  
    496.                 <echo>**** Back to project ${ant.project.name} ****</echo>  
    497.                 <echo>############################################</echo>  
    498.             </then>  
    499.         </if>  
    500.   
    501.         <!-- Value of the hasCode attribute (Application node) extracted from manifest file -->  
    502.         <xpath input="AndroidManifest.xml" expression="/manifest/application/@android:hasCode" output="manifest.hasCode" default="true" />  
    503.   
    504.         <!-- create a path with all the jar files, from the main project and the  
    505.              libraries   
    506.                                        创建一个path,关联所有的jar文件。每个工程下的libs下的jar文件  
    507.              -->  
    508.         <path id="jar.libs.ref">  
    509.             <fileset dir="${jar.libs.absolute.dir}" includes="*.jar" />  
    510.             <path refid="project.libraries.jars" />  
    511.         </path>  
    512.   
    513.         <!-- special case for instrumented: if the previous build was  
    514.              instrumented but not this one, clear out the compiled code   
    515.              特殊情况被打断,清除已编译的代码  
    516.              -->  
    517.         <if>  
    518.             <condition>  
    519.                 <and>  
    520.                     <istrue value="${build.last.is.instrumented}" />  
    521.                     <isfalse value="${build.is.instrumented}" />  
    522.                 </and>  
    523.             </condition>  
    524.             <then>  
    525.                 <echo>Switching from instrumented to non-instrumented build.</echo>  
    526.                 <echo>Deleting previous compilation output:</echo>  
    527.                 <delete dir="${out.classes.absolute.dir}" verbose="${verbose}" />  
    528.             </then>  
    529.         </if>  
    530.   
    531.         <echo>Creating output directories if needed...</echo>  
    532.         <mkdir dir="${resource.absolute.dir}" />  
    533.         <mkdir dir="${jar.libs.absolute.dir}" />  
    534.         <mkdir dir="${out.absolute.dir}" />  
    535.         <mkdir dir="${out.res.absolute.dir}" />  
    536.         <do-only-if-manifest-hasCode>  
    537.             <mkdir dir="${gen.absolute.dir}" />  
    538.             <mkdir dir="${out.classes.absolute.dir}" />  
    539.         </do-only-if-manifest-hasCode>  
    540.     </target>  
    541.   
    542.     <!-- empty default pre-build target. Create a similar target in  
    543.          your build.xml and it'll be called instead of this one. -->  
    544.     <target name="-pre-build" />  
    545.   
    546.     <!-- Code Generation: compile resources (aapt -> R.java), aidl, renderscript  
    547.     通过appt 生成R.jar文件  
    548.      -->  
    549.     <target name="-code-gen">  
    550.         <do-only-if-manifest-hasCode elseText="hasCode = false. Skipping aidl/renderscript/R.java">  
    551.             <echo>----------</echo>  
    552.             <echo>Handling aidl files...</echo>  
    553.             <aidl executable="${aidl}" framework="${android.aidl}" genFolder="${gen.absolute.dir}">  
    554.                 <source path="${source.absolute.dir}" />  
    555.             </aidl>  
    556.   
    557.             <!-- renderscript generates resources so it must be called before aapt -->  
    558.             <echo>----------</echo>  
    559.             <echo>Handling RenderScript files...</echo>  
    560.             <renderscript executable="${renderscript}" framework="${android.rs}" genFolder="${gen.absolute.dir}" resFolder="${resource.absolute.dir}/raw" targetApi="${target.api}">  
    561.                 <source path="${source.absolute.dir}" />  
    562.             </renderscript>  
    563.   
    564.             <echo>----------</echo>  
    565.             <echo>Handling Resources...</echo>  
    566.             <aapt executable="${aapt}" command="package" verbose="${verbose}" manifest="AndroidManifest.xml" androidjar="${android.jar}" rfolder="${gen.absolute.dir}" nonConstantId="${android.library}" projectLibrariesResName="project.libraries.res" projectLibrariesPackageName="project.libraries.package">  
    567.                 <res path="${resource.absolute.dir}" />  
    568.             </aapt>  
    569.         </do-only-if-manifest-hasCode>  
    570.     </target>  
    571.   
    572.     <!-- empty default pre-compile target. Create a similar target in  
    573.          your build.xml and it'll be called instead of this one. -->  
    574.     <target name="-pre-compile" />  
    575.   
    576.     <!-- Compiles this project's .java files into .class files.   
    577.     编译  
    578.     -->  
    579.     <target name="-compile" depends="-build-setup, -pre-build, -code-gen, -pre-compile">  
    580.         <do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">  
    581.             <!-- If android rules are used for a test project, its classpath should include  
    582.                  tested project's location   
    583.                  如果是测试工程,classpath应该包括test的位置  
    584.                  -->  
    585.             <condition property="extensible.classpath" value="${tested.project.absolute.dir}/bin/classes" else=".">  
    586.                 <isset property="tested.project.absolute.dir" />  
    587.             </condition>  
    588.             <condition property="extensible.libs.classpath" value="${tested.project.absolute.dir}/${jar.libs.dir}" else="${jar.libs.dir}">  
    589.                 <isset property="tested.project.absolute.dir" />  
    590.             </condition>  
    591.             <javac encoding="${java.encoding}" source="${java.source}" target="${java.target}" debug="true" extdirs="" destdir="${out.classes.absolute.dir}" bootclasspathref="android.target.classpath" verbose="${verbose}" classpath="${extensible.classpath}" classpathref="jar.libs.ref">  
    592.                 <src path="${source.absolute.dir}" />  
    593.                 <src path="${gen.absolute.dir}" />  
    594.                 <classpath>  
    595.                     <fileset dir="${extensible.libs.classpath}" includes="*.jar" />  
    596.                 </classpath>  
    597.             </javac>  
    598.             <!-- if the project is a library then we generate a jar file   
    599.             如果工程是library工程,则生成jar文件  
    600.             -->  
    601.             <if condition="${project.is.library}">  
    602.                 <then>  
    603.                     <echo>Creating library output jar file...</echo>  
    604.                     <property name="out.library.jar.file" location="${out.absolute.dir}/classes.jar" />  
    605.                     <if>  
    606.                         <condition>  
    607.                             <length string="${android.package.excludes}" trim="true" when="greater" length="0" />  
    608.                         </condition>  
    609.                         <then>  
    610.                             <echo>Custom jar packaging exclusion: ${android.package.excludes}</echo>  
    611.                         </then>  
    612.                     </if>  
    613.                     <jar destfile="${out.library.jar.file}">  
    614.                         <fileset dir="${out.classes.absolute.dir}" excludes="**/R.class **/R$*.class" />  
    615.                         <fileset dir="${source.absolute.dir}" excludes="**/*.java ${android.package.excludes}" />  
    616.                     </jar>  
    617.                 </then>  
    618.             </if>  
    619.   
    620.             <!-- if the project is instrumented, intrument the classes   
    621.             如果工程被打断,插入相关的class  
    622.             -->  
    623.             <if condition="${build.is.instrumented}">  
    624.                 <then>  
    625.                     <echo>Instrumenting classes from ${out.absolute.dir}/classes...</echo>  
    626.                     <!-- It only instruments class files, not any external libs -->  
    627.                     <emma enabled="true">  
    628.                         <instr verbosity="${verbosity}" mode="overwrite" instrpath="${out.absolute.dir}/classes" outdir="${out.absolute.dir}/classes">  
    629.                         </instr>  
    630.                         <!-- TODO: exclusion filters on R*.class and allowing custom exclusion from  
    631.                              user defined file -->  
    632.                     </emma>  
    633.                 </then>  
    634.             </if>  
    635.         </do-only-if-manifest-hasCode>  
    636.     </target>  
    637.   
    638.     <!-- empty default post-compile target. Create a similar target in  
    639.          your build.xml and it'll be called instead of this one. -->  
    640.     <target name="-post-compile" />  
    641.   
    642.     <!-- Obfuscate target  
    643.         This is only active in release builds when proguard.config is defined  
    644.         in default.properties.  
    645.   
    646.         To replace Proguard with a different obfuscation engine:  
    647.         Override the following targets in your build.xml, before the call to <setup>  
    648.             -release-obfuscation-check  
    649.                 Check whether obfuscation should happen, and put the result in a property.  
    650.             -debug-obfuscation-check  
    651.                 Obfuscation should not happen. Set the same property to false.  
    652.             -obfuscate  
    653.                 check if the property set in -debug/release-obfuscation-check is set to true.  
    654.                 If true:  
    655.                     Perform obfuscation  
    656.                     Set property out.dex.input.absolute.dir to be the output of the obfuscation  
    657.                     混淆代码  
    658.     -->  
    659.     <target name="-obfuscate">  
    660.         <if condition="${proguard.enabled}">  
    661.             <then>  
    662.                 <property name="obfuscate.absolute.dir" location="${out.absolute.dir}/proguard" />  
    663.                 <property name="preobfuscate.jar.file" value="${obfuscate.absolute.dir}/original.jar" />  
    664.                 <property name="obfuscated.jar.file" value="${obfuscate.absolute.dir}/obfuscated.jar" />  
    665.                 <!-- input for dex will be proguard's output -->  
    666.                 <property name="out.dex.input.absolute.dir" value="${obfuscated.jar.file}" />  
    667.   
    668.                 <!-- Add Proguard Tasks -->  
    669.                 <property name="proguard.jar" location="${android.tools.dir}/proguard/lib/proguard.jar" />  
    670.                 <taskdef name="proguard" classname="proguard.ant.ProGuardTask" classpath="${proguard.jar}" />  
    671.   
    672.                 <!-- Set the android classpath Path object into a single property. It'll be  
    673.                      all the jar files separated by a platform path-separator.  
    674.                      Each path must be quoted if it contains spaces.  
    675.                 -->  
    676.                 <pathconvert property="android.libraryjars" refid="android.target.classpath">  
    677.                     <firstmatchmapper>  
    678.                         <regexpmapper from='^([^ ]*)( .*)$$' to='"\1\2"' />  
    679.                         <identitymapper />  
    680.                     </firstmatchmapper>  
    681.                 </pathconvert>  
    682.   
    683.                 <!-- Build a path object with all the jar files that must be obfuscated.  
    684.                      This include the project compiled source code and any 3rd party jar  
    685.                      files. -->  
    686.                 <path id="project.jars.ref">  
    687.                     <pathelement location="${preobfuscate.jar.file}" />  
    688.                     <path refid="jar.libs.ref" />  
    689.                 </path>  
    690.                 <!-- Set the project jar files Path object into a single property. It'll be  
    691.                      all the jar files separated by a platform path-separator.  
    692.                      Each path must be quoted if it contains spaces.  
    693.                 -->  
    694.                 <pathconvert property="project.jars" refid="project.jars.ref">  
    695.                     <firstmatchmapper>  
    696.                         <regexpmapper from='^([^ ]*)( .*)$$' to='"\1\2"' />  
    697.                         <identitymapper />  
    698.                     </firstmatchmapper>  
    699.                 </pathconvert>  
    700.   
    701.                 <mkdir dir="${obfuscate.absolute.dir}" />  
    702.                 <delete file="${preobfuscate.jar.file}" />  
    703.                 <delete file="${obfuscated.jar.file}" />  
    704.                 <jar basedir="${out.classes.absolute.dir}" destfile="${preobfuscate.jar.file}" />  
    705.                 <proguard>  
    706.                     @${proguard.config}  
    707.                     -injars       ${project.jars}  
    708.                     -outjars      "${obfuscated.jar.file}"  
    709.                     -libraryjars  ${android.libraryjars}  
    710.                     -dump         "${obfuscate.absolute.dir}/dump.txt"  
    711.                     -printseeds   "${obfuscate.absolute.dir}/seeds.txt"  
    712.                     -printusage   "${obfuscate.absolute.dir}/usage.txt"  
    713.                     -printmapping "${obfuscate.absolute.dir}/mapping.txt"  
    714.                 </proguard>  
    715.             </then>  
    716.         </if>  
    717.     </target>  
    718.   
    719.     <!-- Converts this project's .class files into .dex files   
    720.     将.class 打包成.dex文件  
    721.     -->  
    722.     <target name="-dex" depends="-compile, -post-compile, -obfuscate">  
    723.         <do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">  
    724.             <!-- only convert to dalvik bytecode is *not* a library -->  
    725.             <do-only-if-not-library elseText="Library project: do not convert bytecode...">  
    726.                 <!-- special case for instrumented builds: need to use no-locals and need  
    727.                      to pass in the emma jar.   
    728.                       特殊情况下,检测build,需要通过emma  
    729.                      -->  
    730.                 <if condition="${build.is.instrumented}">  
    731.                     <then>  
    732.                         <dex-helper nolocals="true">  
    733.                             <external-libs>  
    734.                                 <fileset file="${emma.dir}/emma_device.jar" />  
    735.                             </external-libs>  
    736.                         </dex-helper>  
    737.                     </then>  
    738.                     <else>  
    739.                         <dex-helper />  
    740.                     </else>  
    741.                 </if>  
    742.             </do-only-if-not-library>  
    743.         </do-only-if-manifest-hasCode>  
    744.     </target>  
    745.   
    746.     <!-- Updates the pre-processed PNG cache 处理png 图片-->  
    747.     <target name="-crunch">  
    748.         <exec executable="${aapt}" taskName="crunch">  
    749.             <arg value="crunch" />  
    750.             <arg value="-v" />  
    751.             <arg value="-S" />  
    752.             <arg path="${resource.absolute.dir}" />  
    753.             <arg value="-C" />  
    754.             <arg path="${out.res.absolute.dir}" />  
    755.         </exec>  
    756.     </target>  
    757.   
    758.     <!-- Puts the project's resources into the output package file  
    759.          This actually can create multiple resource package in case  
    760.          Some custom apk with specific configuration have been  
    761.          declared in default.properties.  
    762.          打包资源文件  
    763.          -->  
    764.     <target name="-package-resources" depends="-crunch">  
    765.         <!-- only package resources if *not* a library project -->  
    766.         <do-only-if-not-library elseText="Library project: do not package resources...">  
    767.             <aapt executable="${aapt}" command="package" versioncode="${version.code}" versionname="${version.name}" debug="${build.is.packaging.debug}" manifest="AndroidManifest.xml" assets="${asset.absolute.dir}" androidjar="${android.jar}" apkfolder="${out.absolute.dir}" nocrunch="${build.packaging.nocrunch}" resourcefilename="${resource.package.file.name}" resourcefilter="${aapt.resource.filter}" projectLibrariesResName="project.libraries.res" projectLibrariesPackageName="project.libraries.package" previousBuildType="${build.last.target}" buildType="${build.target}">  
    768.                 <res path="${out.res.absolute.dir}" />  
    769.                 <res path="${resource.absolute.dir}" />  
    770.                 <!-- <nocompress /> forces no compression on any files in assets or res/raw -->  
    771.                 <!-- <nocompress extension="xml" /> forces no compression on specific file extensions in assets and res/raw -->  
    772.             </aapt>  
    773.         </do-only-if-not-library>  
    774.     </target>  
    775.   
    776.     <!-- Packages the application. 打包-->  
    777.     <target name="-package" depends="-dex, -package-resources">  
    778.         <!-- only package apk if *not* a library project -->  
    779.         <do-only-if-not-library elseText="Library project: do not package apk...">  
    780.             <if condition="${build.is.instrumented}">  
    781.                 <then>  
    782.                     <package-helper>  
    783.                         <extra-jars>  
    784.                             <!-- Injected from external file -->  
    785.                             <jarfile path="${emma.dir}/emma_device.jar" />  
    786.                         </extra-jars>  
    787.                     </package-helper>  
    788.                 </then>  
    789.                 <else>  
    790.                     <package-helper />  
    791.                 </else>  
    792.             </if>  
    793.         </do-only-if-not-library>  
    794.     </target>  
    795.   
    796.     <target name="-set-mode-check">  
    797.         <fail if="out.final.file" 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." />  
    798.     </target>  
    799.   
    800.     <!-- ********** Debug specific targets ********** -->  
    801.   
    802.     <!-- 设置debug-->  
    803.       
    804.     <target name="-set-debug-files" depends="-set-mode-check">  
    805.   
    806.         <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-debug-unaligned.apk" />  
    807.         <property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-debug.apk" />  
    808.     </target>  
    809.   
    810.     <target name="-set-debug-mode">  
    811.         <!-- record the current build target -->  
    812.         <property name="build.target" value="debug" />  
    813.   
    814.         <property name="build.is.instrumented" value="false" />  
    815.   
    816.         <!-- whether the build is a debug build. always set. -->  
    817.         <property name="build.is.packaging.debug" value="true" />  
    818.   
    819.         <!-- signing mode: debug -->  
    820.         <property name="build.is.signing.debug" value="true" />  
    821.   
    822.     </target>  
    823.   
    824.     <!--debug 模式下,不使用混淆-->  
    825.     <target name="-debug-obfuscation-check">  
    826.         <!-- proguard is never enabled in debug mode -->  
    827.         <property name="proguard.enabled" value="false" />  
    828.     </target>  
    829.   
    830.     <!-- Builds debug output package -->  
    831.     <target name="-do-debug" depends="-set-debug-mode, -debug-obfuscation-check, -package">  
    832.         <!-- only create apk if *not* a library project -->  
    833.         <do-only-if-not-library elseText="Library project: do not create apk...">  
    834.             <sequential>  
    835.                 <zipalign-helper in.package="${out.packaged.file}" out.package="${out.final.file}" />  
    836.                 <echo>Debug Package: ${out.final.file}</echo>  
    837.             </sequential>  
    838.         </do-only-if-not-library>  
    839.     </target>  
    840.   
    841.     <!-- Builds debug output package -->  
    842.     <target name="debug" depends="-set-debug-files, -do-debug" description="Builds the application and signs it with a debug key.">  
    843.         <record-build-info />  
    844.     </target>  
    845.   
    846.   
    847.     <!-- ********** Release specific targets ********** -->  
    848.     <!-- 发布的targets -->  
    849.   
    850.     <!-- called through target 'release'. Only executed if the keystore and  
    851.          key alias are known but not their password.   
    852.                          需要输入key.alias.password,key.store.password  
    853.          -->  
    854.     <target name="-release-prompt-for-password" if="has.keystore" unless="has.password">  
    855.         <!-- Gets passwords -->  
    856.         <input message="Please enter keystore password (store:${key.store}):" addproperty="key.store.password" />  
    857.         <input message="Please enter password for alias '${key.alias}':" addproperty="key.alias.password" />  
    858.     </target>  
    859.   
    860.     <!-- called through target 'release'. Only executed if there's no  
    861.          keystore/key alias set -->  
    862.     <target name="-release-nosign" unless="has.keystore">  
    863.         <!-- no release builds for library project -->  
    864.         <do-only-if-not-library elseText="">  
    865.             <sequential>  
    866.                 <echo>No key.store and key.alias properties found in build.properties.</echo>  
    867.                 <echo>Please sign ${out.packaged.file} manually</echo>  
    868.                 <echo>and run zipalign from the Android SDK tools.</echo>  
    869.             </sequential>  
    870.         </do-only-if-not-library>  
    871.         <record-build-info />  
    872.     </target>  
    873.   
    874.   
    875.     <!-- 检查是否混淆代码 -->  
    876.     <target name="-release-obfuscation-check">  
    877.         <condition property="proguard.enabled" value="true" else="false">  
    878.             <and>  
    879.                 <isset property="build.is.mode.release" />  
    880.                 <isset property="proguard.config" />  
    881.             </and>  
    882.         </condition>  
    883.         <if condition="${proguard.enabled}">  
    884.             <then>  
    885.                 <!-- Secondary dx input (jar files) is empty since all the  
    886.                      jar files will be in the obfuscated jar -->  
    887.                 <path id="out.dex.jar.input.ref" />  
    888.             </then>  
    889.         </if>  
    890.     </target>  
    891.   
    892.     <target name="-set-release-mode" depends="-set-mode-check">  
    893.         <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-release-unsigned.apk" />  
    894.         <property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-release.apk" />  
    895.   
    896.         <!-- record the current build target -->  
    897.         <property name="build.target" value="release" />  
    898.   
    899.         <property name="build.is.instrumented" value="false" />  
    900.   
    901.         <!-- release mode is only valid if the manifest does not explicitly  
    902.              set debuggable to true. default is false. -->  
    903.         <xpath input="AndroidManifest.xml" expression="/manifest/application/@android:debuggable" output="build.is.packaging.debug" default="false" />  
    904.   
    905.         <!-- signing mode: release -->  
    906.         <property name="build.is.signing.debug" value="false" />  
    907.   
    908.         <if condition="${build.is.packaging.debug}">  
    909.             <then>  
    910.                 <echo>*************************************************</echo>  
    911.                 <echo>****  Android Manifest has debuggable=true   ****</echo>  
    912.                 <echo>**** Doing DEBUG packaging with RELEASE keys ****</echo>  
    913.                 <echo>*************************************************</echo>  
    914.             </then>  
    915.             <else>  
    916.                 <!-- property only set in release mode.  
    917.                      Useful for if/unless attributes in target node  
    918.                      when using Ant before 1.8 -->  
    919.                 <property name="build.is.mode.release" value="true" />  
    920.             </else>  
    921.         </if>  
    922.     </target>  
    923.   
    924.     <!-- This runs -package-release and -release-nosign first and then runs  
    925.          only if release-sign is true (set in -release-check,  
    926.          called by -release-no-sign)-->  
    927.     <target name="release" depends="-set-release-mode, -release-obfuscation-check, -package, -release-prompt-for-password, -release-nosign" if="has.keystore" description="Builds the application. The generated apk file must be signed before  
    928.                             it is published.">  
    929.   
    930.         <!-- only create apk if *not* a library project -->  
    931.         <do-only-if-not-library elseText="Library project: do not create apk...">  
    932.             <sequential>  
    933.                 <property name="out.unaligned.file" location="${out.absolute.dir}/${ant.project.name}-release-unaligned.apk" />  
    934.   
    935.                 <!-- Signs the APK -->  
    936.                 <echo>Signing final apk...</echo>  
    937.                 <signjar jar="${out.packaged.file}" signedjar="${out.unaligned.file}" keystore="${key.store}" storepass="${key.store.password}" alias="${key.alias}" keypass="${key.alias.password}" verbose="${verbose}" />  
    938.   
    939.                 <!-- Zip aligns the APK -->  
    940.                 <zipalign-helper in.package="${out.unaligned.file}" out.package="${out.final.file}" />  
    941.                 <echo>Release Package: ${out.final.file}</echo>  
    942.             </sequential>  
    943.         </do-only-if-not-library>  
    944.         <record-build-info />  
    945.     </target>  
    946.   
    947.     <!-- ********** Instrumented specific targets ********** -->  
    948.     <!--需要插入的特殊 targets-->  
    949.   
    950.     <!-- These targets are specific for the project under test when it  
    951.          gets compiled by the test projects in a way that will make it  
    952.          support emma code coverage -->  
    953.   
    954.     <target name="-set-instrumented-mode" depends="-set-mode-check">  
    955.         <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-instrumented-unaligned.apk" />  
    956.         <property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-instrumented.apk" />  
    957.   
    958.         <!-- whether the build is an instrumented build. -->  
    959.         <property name="build.is.instrumented" value="true" />  
    960.     </target>  
    961.   
    962.     <!-- Builds instrumented output package -->  
    963.     <target name="instrument" depends="-set-instrumented-mode, -do-debug" description="Builds an instrumented packaged.">  
    964.         <!-- only create apk if *not* a library project -->  
    965.         <do-only-if-not-library elseText="Library project: do not create apk...">  
    966.             <sequential>  
    967.                 <zipalign-helper in.package="${out.packaged.file}" out.package="${out.final.file}" />  
    968.                 <echo>Instrumented Package: ${out.final.file}</echo>  
    969.             </sequential>  
    970.         </do-only-if-not-library>  
    971.         <record-build-info />  
    972.     </target>  
    973.   
    974.     <!-- ********** Test project specific targets ********** -->  
    975.     <!-- 测试工程特殊的targets -->  
    976.   
    977.     <!-- enable code coverage -->  
    978.     <target name="emma">  
    979.         <property name="emma.enabled" value="true" />  
    980.     </target>  
    981.   
    982.     <!-- fails if the project is not a test project -->  
    983.     <target name="-test-project-check">  
    984.         <!-- can't use project.is.test since the setup target is not run -->  
    985.         <if>  
    986.             <condition>  
    987.                 <isset property="tested.project.dir" />  
    988.             </condition>  
    989.             <else>  
    990.                 <fail message="Project is not a test project." />  
    991.             </else>  
    992.         </if>  
    993.     </target>  
    994.   
    995.     <target name="test" depends="-test-project-check" description="Runs tests from the package defined in test.package property">  
    996.   
    997.         <property name="tested.project.absolute.dir" location="${tested.project.dir}" />  
    998.   
    999.         <property name="test.runner" value="android.test.InstrumentationTestRunner" />  
    1000.   
    1001.         <!-- Application package of the tested project extracted from its manifest file -->  
    1002.         <xpath input="${tested.project.absolute.dir}/AndroidManifest.xml" expression="/manifest/@package" output="tested.manifest.package" />  
    1003.         <xpath input="AndroidManifest.xml" expression="/manifest/@package" output="manifest.package" />  
    1004.   
    1005.         <property name="emma.dump.file" value="/data/data/${tested.manifest.package}/coverage.ec" />  
    1006.   
    1007.         <if condition="${emma.enabled}">  
    1008.             <then>  
    1009.                 <echo>WARNING: Code Coverage is currently only supported on the emulator and rooted devices.</echo>  
    1010.                 <run-tests-helper emma.enabled="true">  
    1011.                     <extra-instrument-args>  
    1012.                         <arg value="-e" />  
    1013.                         <arg value="coverageFile" />  
    1014.                         <arg value="${emma.dump.file}" />  
    1015.                     </extra-instrument-args>  
    1016.                 </run-tests-helper>  
    1017.                 <echo>Downloading coverage file into project directory...</echo>  
    1018.                 <exec executable="${adb}" failonerror="true">  
    1019.                     <arg line="${adb.device.arg}" />  
    1020.                     <arg value="pull" />  
    1021.                     <arg value="${emma.dump.file}" />  
    1022.                     <arg value="coverage.ec" />  
    1023.                 </exec>  
    1024.                 <echo>Extracting coverage report...</echo>  
    1025.                 <emma>  
    1026.                     <report sourcepath="${tested.project.absolute.dir}/${source.dir}" verbosity="${verbosity}">  
    1027.                         <!-- TODO: report.dir or something like should be introduced if necessary -->  
    1028.                         <infileset dir=".">  
    1029.                             <include name="coverage.ec" />  
    1030.                             <include name="coverage.em" />  
    1031.                         </infileset>  
    1032.                         <!-- TODO: reports in other, indicated by user formats -->  
    1033.                         <html outfile="coverage.html" />  
    1034.                     </report>  
    1035.                 </emma>  
    1036.                 <echo>Cleaning up temporary files...</echo>  
    1037.                 <delete file="coverage.ec" />  
    1038.                 <delete file="coverage.em" />  
    1039.                 <echo>Saving the report file in ${basedir}/coverage/coverage.html</echo>  
    1040.             </then>  
    1041.             <else>  
    1042.                 <run-tests-helper />  
    1043.             </else>  
    1044.         </if>  
    1045.     </target>  
    1046.   
    1047.   
    1048.     <!-- ********** Install/uninstall specific targets ********** -->  
    1049.     <!-- 安装和卸载 -->  
    1050.   
    1051.     <target name="install" description="Installs the newly build package. Must be used in conjunction with a build target  
    1052.                             (debug/release/instrument). If the application was previously installed, the application  
    1053.                             is reinstalled if the signature matches.">  
    1054.         <!-- only do install if *not* a library project -->  
    1055.         <do-only-if-not-library elseText="Library project: nothing to install!">  
    1056.             <if>  
    1057.                 <condition>  
    1058.                     <isset property="out.final.file" />  
    1059.                 </condition>  
    1060.                 <then>  
    1061.                     <if>  
    1062.                         <condition>  
    1063.                             <resourceexists>  
    1064.                                 <file file="${out.final.file}" />  
    1065.                             </resourceexists>  
    1066.                         </condition>  
    1067.                         <then>  
    1068.                             <echo>Installing ${out.final.file} onto default emulator or device...</echo>  
    1069.                             <exec executable="${adb}" failonerror="true">  
    1070.                                 <arg line="${adb.device.arg}" />  
    1071.                                 <arg value="install" />  
    1072.                                 <arg value="-r" />  
    1073.                                 <arg path="${out.final.file}" />  
    1074.                             </exec>  
    1075.   
    1076.                             <!-- now install the tested project if applicable -->  
    1077.                             <!-- can't use project.is.test since the setup target might not have run -->  
    1078.                             <if>  
    1079.                                 <condition>  
    1080.                                     <isset property="tested.project.dir" />  
    1081.                                 </condition>  
    1082.                                 <then>  
    1083.                                     <property name="tested.project.absolute.dir" location="${tested.project.dir}" />  
    1084.   
    1085.                                     <!-- figure out which tested package to install based on emma.enabled -->  
    1086.                                     <condition property="tested.project.install.target" value="installi" else="installd">  
    1087.                                         <isset property="emma.enabled" />  
    1088.                                     </condition>  
    1089.                                     <subant target="${tested.project.install.target}" failonerror="true">  
    1090.                                         <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />  
    1091.                                     </subant>  
    1092.                                 </then>  
    1093.                             </if>  
    1094.                         </then>  
    1095.                         <else>  
    1096.                             <fail message="File ${out.final.file} does not exist." />  
    1097.                         </else>  
    1098.                     </if>  
    1099.                 </then>  
    1100.                 <else>  
    1101.                     <echo>Install file not specified.</echo>  
    1102.                     <echo>  
    1103.                     </echo>  
    1104.                     <echo>'ant install' now requires the build target to be specified as well.</echo>  
    1105.                     <echo>  
    1106.                     </echo>  
    1107.                     <echo>  
    1108.                     </echo>  
    1109.                     <echo>    ant debug install</echo>  
    1110.                     <echo>    ant release install</echo>  
    1111.                     <echo>    ant instrument install</echo>  
    1112.                     <echo>This will build the given package and install it.</echo>  
    1113.                     <echo>  
    1114.                     </echo>  
    1115.                     <echo>Alternatively, you can use</echo>  
    1116.                     <echo>    ant installd</echo>  
    1117.                     <echo>    ant installr</echo>  
    1118.                     <echo>    ant installi</echo>  
    1119.                     <echo>    ant installt</echo>  
    1120.                     <echo>to only install an existing package (this will not rebuild the package.)</echo>  
    1121.                     <fail />  
    1122.                 </else>  
    1123.             </if>  
    1124.         </do-only-if-not-library>  
    1125.     </target>  
    1126.   
    1127.     <target name="installd" depends="-set-debug-files, install" description="Installs (only) the debug package." />  
    1128.     <target name="installr" depends="-set-release-mode, install" description="Installs (only) the release package." />  
    1129.     <target name="installi" depends="-set-instrumented-mode, install" description="Installs (only) the instrumented package." />  
    1130.     <target name="installt" depends="-test-project-check, installd" description="Installs (only) the test and tested packages." />  
    1131.   
    1132.   
    1133.     <!-- Uninstalls the package from the default emulator/device -->  
    1134.     <target name="uninstall" description="Uninstalls the application from a running emulator or device.">  
    1135.         <!-- Name of the application package extracted from manifest file -->  
    1136.         <xpath input="AndroidManifest.xml" expression="/manifest/@package" output="manifest.package" />  
    1137.   
    1138.         <if>  
    1139.             <condition>  
    1140.                 <isset property="manifest.package" />  
    1141.             </condition>  
    1142.             <then>  
    1143.                 <uninstall-helper app.package="${manifest.package}" />  
    1144.             </then>  
    1145.             <else>  
    1146.                 <echo>Could not find application package in manifest. Cannot run 'adb uninstall'.</echo>  
    1147.             </else>  
    1148.         </if>  
    1149.   
    1150.         <!-- Now uninstall the tested project, if applicable -->  
    1151.         <!-- can't use project.is.test since the setup target might not have run -->  
    1152.         <if>  
    1153.             <condition>  
    1154.                 <isset property="tested.project.dir" />  
    1155.             </condition>  
    1156.             <then>  
    1157.                 <property name="tested.project.absolute.dir" location="${tested.project.dir}" />  
    1158.   
    1159.                 <!-- Application package of the tested project extracted from its manifest file -->  
    1160.                 <xpath input="${tested.project.absolute.dir}/AndroidManifest.xml" expression="/manifest/@package" output="tested.manifest.package" />  
    1161.                 <if>  
    1162.                     <condition>  
    1163.                         <isset property="tested.manifest.package" />  
    1164.                     </condition>  
    1165.                     <then>  
    1166.                         <uninstall-helper app.package="${tested.manifest.package}" />  
    1167.                     </then>  
    1168.                     <else>  
    1169.                         <echo>Could not find tested application package in manifest. Cannot run 'adb uninstall'.</echo>  
    1170.                     </else>  
    1171.                 </if>  
    1172.             </then>  
    1173.         </if>  
    1174.   
    1175.     </target>  
    1176.   
    1177.   
    1178.     <target name="help">  
    1179.         <!-- displays starts at col 13  
    1180.               |13                                                              80| -->  
    1181.         <echo>Android Ant Build. Available targets:</echo>  
    1182.         <echo>   help:      Displays this help.</echo>  
    1183.         <echo>   clean:     Removes output files created by other targets.</echo>  
    1184.         <echo>              The 'all' target can be used to clean dependencies</echo>  
    1185.         <echo>              (tested projects and libraries)at the same time</echo>  
    1186.         <echo>              using: 'ant all clean'</echo>  
    1187.         <echo>   debug:     Builds the application and signs it with a debug key.</echo>  
    1188.         <echo>   release:   Builds the application. The generated apk file must be</echo>  
    1189.         <echo>              signed before it is published.</echo>  
    1190.         <echo>   instrument:Builds an instrumented package and signs it with a</echo>  
    1191.         <echo>              debug key.</echo>  
    1192.         <echo>   test:      Runs the tests. Project must be a test project and</echo>  
    1193.         <echo>              must have been built. Typical usage would be:</echo>  
    1194.         <echo>                  ant [emma] debug installt test</echo>  
    1195.         <echo>   emma:      Transiently enables code coverage for subsequent</echo>  
    1196.         <echo>              targets.</echo>  
    1197.         <echo>   install:   Installs the newly build package. Must either be used</echo>  
    1198.         <echo>              in conjunction with a build target (debug/release/</echo>  
    1199.         <echo>              instrument) or with the proper suffix indicating</echo>  
    1200.         <echo>              which package to install (see below).</echo>  
    1201.         <echo>              If the application was previously installed, the</echo>  
    1202.         <echo>              application is reinstalled if the signature matches.</echo>  
    1203.         <echo>   installd:  Installs (only) the debug package.</echo>  
    1204.         <echo>   installr:  Installs (only) the release package.</echo>  
    1205.         <echo>   installi:  Installs (only) the instrumented package.</echo>  
    1206.         <echo>   installt:  Installs (only) the test and tested packages.</echo>  
    1207.         <echo>   uninstall: Uninstalls the application from a running emulator or</echo>  
    1208.         <echo>              device.</echo>  
    1209.     </target>  
    1210. </project>  


     
  • 相关阅读:
    MongoDB慢查询性能分析
    redis的LRU算法(二)
    Skynet服务热点火焰图分析
    内存爆灯
    时区问题
    与机器共生
    bug狩猎
    Lesson Learned
    下划线引起的血案
    Intel的CPU漏洞:Spectre
  • 原文地址:https://www.cnblogs.com/greywolf/p/3030027.html
Copyright © 2011-2022 走看看