在ant打包过程中的一些学习所得以及用于这个项目的build文件的大体思路如下:
首先配置好整个系统所需的ant编译环境,在build.properties中配置好相关的参数.(build.properties文件主要包涵了一些在编译中需要用到工具的路径,以及一些需要配置的参数,如应用包名,项目名,以及一些需要传入的参数.写在build.properties中主要是为了方便配置文件的集中管理)在这次ant打包的需求中,需要修改一个java文件中的两个常量属性.根据这样的需求,首先需要拿到普通android项目打包的build.xml,和build.properties.
注意配置环境变量:JAVA_HOME、ANDROID_HOME和ANT_HOME
build.properties文件内容如下:
在这里需要指出的是,由于android sdk tool,和platform tools的一些升级,一部分android的压缩编译工具被转移到了platform _tools目录下,所以,在配置的时候需要稍微改动一下build.properties中的内容
然后,开始动手实现我们的需求吧.由于对ant理解得不算深入,用的方法比较死,可能效率上来说不是最高的,下面说一下我的思路
<target name=”CopyReplaceJava”>
<copy file=”${basedir}\${srcdir}\${file.replace.path}\${fileName}” todir=”..\temp\build\META-INF” />
<replace file =”${basedir}\${srcdir}\${file.replace.path}\${fileName}” token=”@Company_Name@” value=”${company.name}” encoding=”utf-8″/>
<replace file =”${basedir}\${srcdir}\${file.replace.path}\${fileName}” token=”@App_id@” value=”${app.id}” encoding=”utf-8″/>
</target>
首先,我们复制我们需要修改的java文件到一个临时的temp文件夹中,然后对位于src中的java文件进行字符的替换,我们这用@Company_Name@这类特殊字符来代替替换位置,防止替换了正常的文件代码.替换完毕,然后执行后续的编译,压缩,打包,这时打出的包中的常量数值就是我们传如参数的数值了.由于替换了文件中@Company_Name@这类特殊字符,为了下次能正常打包,需要将复制到temp中的java文件替换回来.在打包完之后,我们用这段代码来实现(注意depends参数决定了target的执行顺序,这里我们给的是在compile之后)
<target name=”replaceJava” depends=”compile”>
<delete file=”${basedir}\${srcdir}\${file.replace.path}\${fileName}”/>
<copy file=”..\temp\build\META-INF\${fileName}” todir=”${basedir}\${srcdir}\${file.replace.path}” />
</target>
接下来,我们需要对生成的不需要的中间文件进行清理,如classes文件夹等.
<delete dir=”${basedir}\${outdir}\classes” />
<delete file=”${basedir}\${outdir}\classes.dex” />
<delete file=”${basedir}\${outdir}\jjdd.ap_” />
这样就完成了build.xml的编辑,eclipse继承了ANT,所以我们可以在eclipse中直接运行,也可以在代码中调用。
首先我们需要下载ANT,然后配置相应的环境变量信息,最后我们这样调用:
清理完毕,一个修改了属性值的apk包就自动生成了.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
其他说明:
一、Windows 批处理循环处理
接下来,需求有了进一步的加强,我们需要10几个相同公司名,不同id的包.那么我们怎么自动生成这么一些包呢,这里我用到了dos命令来完成我们的需求(ant虽然也能实现,但是好像ant中执行for需要有插件支持,并且对ant不算太熟,所以,这里我采用dos来做),一下代码是生成指定公司名的不同id的dos代码.
cd /d F:\WorkSpace\online\trunk\project_name
@echo off
set /p x=请输入产品投放的市场名称:
set /p min=最小id值是:
set /p max=最大id值是:
for /l %%i in (%min%,1,%max%) do ant -f build.xml -Dcompany.name =%x% -Dapp.id=%%i
ant -f build.xml -Dcompany.name =%x% -Dapp.id=%%i,这条命令是执行ant,并给build.xml中的company.name赋值输入的x,给app.id赋值i.同时用一个循环完成输入的min到max次调用ant打包,生成id不同的多个ant包.
二、Ant问题解决
Ant问题:warning: 'includeantruntime' was not set
解决:
修改
<javac encoding="ascii" target="${compile.target}" debug="true" extdirs="" srcdir="." destdir="${outdir-classes}" bootclasspath="${android-jar}">
为
<javac encoding="ascii" target="${compile.target}" debug="true" extdirs="" srcdir="." destdir="${outdir-classes}" bootclasspath="${android-jar}" includeantruntime="on">
(在其中增加includeantruntime="on")
三、Ant 详解资料
ANT详解
http://www.cnblogs.com/clarkchen/archive/2011/03/10/1980194.html
ant打包相关参考资料:
http://hi.baidu.com/%F5%CC%C4%A7/blog/item/3f9bc5ec2338ad3726979186.html
http://www.diybl.com/course/3_program/java/javajs/20090201/154692.html
Android ant打包相关:
http://marshal.easymorse.com/archives/1665
http://handsomeliuyang.iteye.com/blog/1156070
http://www.bangchui.org/simple/?t13358.html
四、ant中读取系统环境变量方法
<property environment="SystemVariable" />
命令后面追加参数使用-D方式,如:
ant -buildfile build.xml -Dapp.name=xiaoshan-Doutput.dir=g:\\releaseapkdir
六、ant中的条件判断“condition”的使用
先说明下antcall与ant的区别:
<antcall> 只能调用同一个脚本之内的构建目标(target),
<ant>可以通过antfile属性指定其他脚本内的目标(target).
一般如果目标在脚本内部,用<antcall>组织一下,分布在不同脚本里,用<ant>组织。
1、istrue isfalse:断言真假
<project name="testCondition">
<target name="test">
<condition property="scondition">
<istrue value="true"/>
</condition>
<antcall target="isTrue"></antcall>
<antcall target="isFalse"></antcall>
</target>
<target name="isTrue" if="scondition">
<echo>is ture</echo>
</target>
<target name="isFalse" unless="scondition">
<echo>is false</echo>
</target>
</project>
2、逻辑运算
2.1、not 逻辑非
<project name="testCondition">
<target name="test">
<condition property="scondition">
<not>
<istrue value="true"/>
</not>
</condition>
<antcall target="isTrue"></antcall>
<antcall target="isFalse"></antcall>
</target>
<target name="isTrue" if="scondition">
<echo>is ture</echo>
</target>
<target name="isFalse" unless="scondition">
<echo>is false</echo>
</target>
</project>
2.2、and 逻辑与
<project name="testCondition">
<target name="test">
<condition property="scondition">
<and>
<istrue value="true"/>
<istrue value="false"/>
</and>
</condition>
<antcall target="isTrue"></antcall>
<antcall target="isFalse"></antcall>
</target>
<target name="isTrue" if="scondition">
<echo>is ture</echo>
</target>
<target name="isFalse" unless="scondition">
<echo>is false</echo>
</target>
</project>
2.3、or 逻辑或 xor异或 (语法上与and类似)
3、available 是否可用
<project name="testCondition">
<path id="all.test.classes">
<pathelement location="bin"/>
</path>
<target name="test">
<condition property="scondition">
<!--在指定的classpath路径下是否存在资源 TestTest.class-->
<available resource="TestTest.class">
<classpath refid="all.test.classes" />
</available>
</condition>
<antcall target="isTrue"></antcall>
<antcall target="isFalse"></antcall>
</target>
<target name="isTrue" if="scondition">
<echo>is ture</echo>
</target>
<target name="isFalse" unless="scondition">
<echo>is false</echo>
</target>
</project>
4、isset 指定属性是否存在
<project name="testCondition">
<!--属性也可以通过ant参数-D来设置-->
<property name="name" value="this is name"/>
<target name="test">
<condition property="scondition">
<!--如果属性name不存在则返回false-->
<isset property="name"/>
</condition>
<antcall target="isTrue"></antcall>
<antcall target="isFalse"></antcall>
</target>
<target name="isTrue" if="scondition">
<echo>is ture</echo>
</target>
<target name="isFalse" unless="scondition">
<echo>is false</echo>
</target>
</project>
5、equals 是否相等
<project name="testCondition">
<!--属性也可以通过ant参数-D来设置-->
<property name="name" value="this is name"/>
<target name="test">
<condition property="scondition">
<!--如果arg1的值与arg2的值相等返回true,否则为false-->
<equals arg1="${name}" arg2="this is name"/>
</condition>
<antcall target="isTrue"></antcall>
<antcall target="isFalse"></antcall>
</target>
<target name="isTrue" if="scondition">
<echo>is ture</echo>
</target>
<target name="isFalse" unless="scondition">
<echo>is false</echo>
</target>
</project>
6、filesmatch 比较文件
<project name="testCondition">
<target name="test">
<condition property="scondition">
<!--如果file1所代表的文件与file2所代表的文件相等返回true,否则为false-->
<filesmatch file1="testfile1.txt" file2="testfile2.txt"/>
</condition>
<antcall target="isTrue"></antcall>
<antcall target="isFalse"></antcall>
</target>
<target name="isTrue" if="scondition">
<echo>is ture</echo>
</target>
<target name="isFalse" unless="scondition">
<echo>is false</echo>
</target>
</project>
七、错误: 编码XXX的不可映射字符
解决方法:在javac标签中增加 encoding="utf-8",其中UTF-8要与我们项目的编译编码一致,可能是UTF-8或GBK等。
八、签名失败问题解决
遗留问题:
目前采用默认的方法build生成的APK,虽然已经被签名了,但是,安装时错误,提示未签名。
查看APK包中的签名文件,不是默认的CERT.*,而是<key>.*。
然后,即使将名称修改成CERT.*,程序仍然不能正常安装。
如果导出debug版本,则不会有这个问题。
用ADT插件导出签名APK,也不会有这个问题。
解决方法:
产生此问题的根本原因是JDK1.7造成的,只有运行Ant使用jre1.7的版本时,才会发生该问题。
可以通过设置运行build.xml文件时使用的jre版本来解决,具体方法是:
选中build.xml->右键->Run As->External Tools Configurations,
在右侧区域选中JRE标签页,可以看到对jre设定有三个选项:
Run in the same JRE as the workspace使用与workspace相同版本的jre。
Execution environment根据相关环境选择一个jre版本。
Separate JRE使用一个已经安装的jre的当前版本。
一般项目的jre都会设定为1.7以下的版本,所以建议选择第一个,使其与项目设定保持一致即可。
或者选择Execution environment 选择低于1.7的版本。
九、本地libs下面的so文件未被打包到apk中
官方对apkbuilder参数有说明,需要一个 -nf 参数,如下:
官方详细说明为:
十、在build文件中使用for循环和字符串处理
默认安装的ant不支持for循环写法,我们需要加入一个jar包来支持。
jar包名称为“ant-contrib”,推荐一个下载地址:http://www.findjar.com/index.x?query=ant-contrib
1、下载ant-contrib-1.0b3.jar后,将其复制到ant_home目录下的lib中。
2、需要在build.xml文件开始部分加入:
<taskdef resource="net/sf/antcontrib/antlib.xml" />3、如下是一个for循环和字符串处理的例子