APK包的生成是一系列操作的结果,而Ant则是将这一系列操作流程化,提供出定制化的接口,以及可配置的参数供修改,而这些都是通过指定的构建文件来实现。我们就从Ant的打包流程来理解Ant的一些基本用法。当在命令行中执行ant,默认会去解析当前目录的build.xml作为构建文件。下面是个删除部分注视的版本:
<project name="工程名" default="help"> <!-- The local.properties file is created and updated by the 'android' tool. It contains the path to the SDK. It should *NOT* be checked into Version Control Systems. --> <property file="local.properties" /> <property file="ant.properties" /> <!-- Import per project custom build rules if present at the root of the project. This is the place to put custom intermediary targets such as: -pre-build -pre-compile -post-compile (This is typically used for code obfuscation. Compiled code location: ${out.classes.absolute.dir} If this is not done in place, override ${out.dex.input.absolute.dir}) -post-package -post-build -pre-clean --> <import file="custom_rules.xml" optional="true" /> <!-- Import the actual build file. <import file="${sdk.dir}/tools/ant/build.xml" />再跟进到你的SDK目录的tools/ant/build.xml,查看Ant打包的完整过程。
<target name="release" depends="-set-release-mode, -release-obfuscation-check, -package, -post-package, -release-prompt-for-password, -release-nosign, -release-sign, -post-build" description="Builds the application in release mode."> </target>target的name和description顾名思义,而depends则是该target执行前,需要执行或者说依赖的target,而且是依据从左到右的顺序依次执行。继续跟进-package它的定义:<target name="-package" depends="-dex, -package-resources">,分别是dex文件的生成和资源打包,里面又定义了很多具体的target。整个过程中就有或者说预留了一些空的target,比如-pre-build -pre-compile -post-package -post-build,是每个打包编译步骤之前后结束,通过在custom_rules.xml中重写这些target,达到定制化的要求
<path id="base.path"> <pathelement path="${classpath}"/> <fileset dir="lib"> <include name="**/*.jar"/> </fileset> <pathelement location="classes"/> </path>一个可以在其它地方通过refid引用的path,里面包括了具体path路径pathelement和通过include或exclude来筛选文件的fileset
<copy todir="${source.absolute.dir}"> <fileset dir="其它源码目录"> <include name="**/*.java" /> <include name="**/*.aidl" /> </fileset> </copy>odir:目标目录,源目录在fileset上定义,规则是包括所有.java文件和所有aidl文件;简单的如拷贝单个文件
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/w7849516230/article/details/47975463