您的位置:首页 > 移动开发 > Android开发

android使用ant自动打包(包括更改文件中的内容)

2015-06-03 20:49 477 查看
在ant打包过程中的一些学习所得以及用于这个项目的build文件的大体思路如下:

首先配置好整个系统所需的ant编译环境,在build.properties中配置好相关的参数.(build.properties文件主要包涵了一些在编译中需要用到工具的路径,以及一些需要配置的参数,如应用包名,项目名,以及一些需要传入的参数.写在build.properties中主要是为了方便配置文件的集中管理)在这次ant打包的需求中,需要修改一个java文件中的两个常量属性.根据这样的需求,首先需要拿到普通android项目打包的build.xml,和build.properties.

注意配置环境变量:JAVA_HOME、ANDROID_HOME和ANT_HOME

build.properties文件内容如下:

[plain] view
plaincopy

##########SHANHY############

#\u7F16\u8BD1\u7248\u672C

compile.target=1.6

android.sdk.apilevel=8

#\u5E94\u7528\u7A0B\u5E8F\u5305\u540D(\u5728uninstall\u65F6\u9700\u8981)

app.package=com.surfing.conference

#\u590D\u5236\u8D44\u6E90

app.assetssource.path=

app.ressource.path=

#\u53D1\u5E03\u7684\u5BA2\u6237\u7AEF\u540D\u79F0

app.apkname=antPackageDemo

#\u53D1\u5E03\u7684\u5BA2\u6237\u7AEF\u5B58\u653E\u4F4D\u7F6E(\u53EF\u4EE5\u662F\u76F8\u5BF9\u8DEF\u5F84\u6216\u7EDD\u5BF9\u8DEF\u5F84)

output.dir=releaseapkdir

#########KeyStore Info##########

android.keystore=xiaoshan.key

#keystore.alias

android.keystore.alias=shanhy

#keystore.password

android.keystore.password=123456

build.xml文件内容如下:

[html] view
plaincopy

<?xml version="1.0" encoding="UTF-8"?>

<project name="antbuildAndroidDemo" default="zipalign" basedir=".">

<property file="build.properties">

</property>

<property name="ENCODEING_CHARSET" value="UTF-8" />

<property environment="SystemVariable" />

<property name="sdk.folder" value="${SystemVariable.ANDROID_HOME}/platforms/android-${android.sdk.apilevel}" />

<property name="android.tools" value="${SystemVariable.ANDROID_HOME}/platform-tools" />

<property name="apk.tools" value="${SystemVariable.ANDROID_HOME}/tools" />

<property name="jdk.home" value="${SystemVariable.JAVA_HOME}" />

<!-- The intermediates directory -->

<!-- Eclipse uses "bin" for its own output, so we do the same. -->

<property name="outdir" value="bin" />

<!-- ************************************************************************************* -->

<!-- No user servicable parts below. -->

<property name="android-framework" value="${sdk.folder}/framework.aidl" />

<!-- Input directories -->

<property name="resource-dir" value="res" />

<property name="asset-dir" value="assets" />

<property name="srcdir" value="src" />

<condition property="srcdir-ospath" value="${basedir}/${srcdir}" else="${basedir}/${srcdir}">

<os family="windows" />

</condition>

<property name="external-libs" value="libs" />

<condition property="external-libs-ospath" value="${basedir}/${external-libs}" else="${basedir}/${external-libs}">

<os family="windows" />

</condition>

<!-- Output directories -->

<property name="outdir-classes" value="${outdir}/classes" />

<condition property="outdir-classes-ospath" value="${basedir}/${outdir-classes}" else="${basedir}/${outdir-classes}">

<os family="windows" />

</condition>

<condition property="zipalign-package-ospath" value="${output.dir}/${app.apkname}.apk" else="${output.dir}/${app.apkname}.apk">

<os family="windows" />

</condition>

<!-- Create R.java in the source directory -->

<property name="outdir-r" value="gen" />

<!-- Intermediate files -->

<property name="dex-file" value="classes.dex" />

<property name="intermediate-dex" value="${outdir}/${dex-file}" />

<condition property="intermediate-dex-ospath" value="${basedir}/${intermediate-dex}" else="${basedir}/${intermediate-dex}">

<os family="windows" />

</condition>

<!-- The final package file to generate -->

<property name="resources-package" value="${outdir}/${ant.project.name}.ap_" />

<condition property="resources-package-ospath" value="${basedir}/${resources-package}" else="${basedir}/${resources-package}">

<os family="windows" />

</condition>

<property name="out-debug-package" value="${outdir}/${ant.project.name}-debug.apk" />

<condition property="out-debug-package-ospath" value="${basedir}/${out-debug-package}" else="${basedir}/${out-debug-package}">

<os family="windows" />

</condition>

<property name="out-unsigned-package" value="${outdir}/${ant.project.name}-unsigned.apk" />

<property name="out-signed-package" value="${outdir}/${ant.project.name}-signed.apk" />

<condition property="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" else="${basedir}/${out-unsigned-package}">

<os family="windows" />

</condition>

<condition property="out-signed-package-ospath" value="${basedir}/${out-signed-package}" else="${basedir}/${out-signed-package}">

<os family="windows" />

</condition>

<!-- Tools -->

<condition property="aapt" value="${android.tools}/aapt.exe" else="${android.tools}/aapt">

<os family="windows" />

</condition>

<condition property="zipalign" value="${apk.tools}/zipalign.exe" else="${apk.tools}/zipalign">

<os family="windows" />

</condition>

<condition property="jarsigner" value="${jdk.home}/bin/jarsigner.exe" else="${jdk.home}/bin/jarsigner">

<os family="windows" />

</condition>

<condition property="aidl" value="${android.tools}/aidl.exe" else="${android.tools}/aidl">

<os family="windows" />

</condition>

<condition property="adb" value="${android.tools}/adb.exe" else="${apk.tools}/adb">

<os family="windows" />

</condition>

<condition property="dx" value="${android.tools}/dx.bat" else="${android.tools}/dx">

<os family="windows" />

</condition>

<condition property="apk-builder" value="${apk.tools}/apkbuilder.bat" else="${apk.tools}/apkbuilder">

<os family="windows" />

</condition>

<property name="android-jar" value="${sdk.folder}/android.jar" />

<!-- Rules -->

<!-- Create the output directories if they don't exist yet. -->

<target name="dirs" depends="init">

<echo>Creating output directories if needed...</echo>

<mkdir dir="${outdir}" />

<mkdir dir="${outdir-classes}" />

</target>

<!-- Generate the R.java file for this project's resources. -->

<target name="resource-src" depends="dirs">

<echo>Generating R.java / Manifest.java from the resources...</echo>

<exec executable="${aapt}" failonerror="true">

<arg value="package" />

<arg value="-m" />

<arg value="-J" />

<arg value="${outdir-r}" />

<arg value="-M" />

<arg value="AndroidManifest.xml" />

<arg value="-S" />

<arg value="${resource-dir}" />

<arg value="-I" />

<arg value="${android-jar}" />

</exec>

</target>

<!-- Generate java classes from .aidl files. -->

<target name="aidl" depends="dirs">

<echo>Compiling aidl files into Java classes...</echo>

<apply executable="${aidl}" failonerror="true">

<arg value="-p${android-framework}" />

<arg value="-I${srcdir}" />

<fileset dir="${srcdir}">

<include name="**/*.aidl" />

</fileset>

</apply>

</target>

<!-- Compile this project's .java files into .class files. -->

<target name="compile" depends="dirs, resource-src, aidl">

<!-- 下面的encoding要与项目的整体编码一致,否则会出现“编码 xxx 的不可映射字符” -->

<javac encoding="${ENCODEING_CHARSET}" target="${compile.target}" debug="true" extdirs="" srcdir="." destdir="${outdir-classes}" bootclasspath="${android-jar}" includeantruntime="on">

<classpath>

<fileset dir="${external-libs}" includes="*.*" />

</classpath>

</javac>

</target>

<!-- Convert this project's .class files into .dex files. -->

<target name="dex" depends="compile">

<echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>

<apply executable="${dx}" failonerror="true" parallel="true">

<arg value="--dex" />

<!--mce:0 -->

<!--

<script src="/javascripts/tinymce/themes/advanced/langs/zh.js" type="text/javascript">

</script>

-->

<!--mce:1 -->

<!--

<script src="/javascripts/tinymce/plugins/javaeye/langs/zh.js" type="text/javascript">

</script>

-->

<arg value="--output=${intermediate-dex-ospath}" />

<arg path="${outdir-classes-ospath}" />

<fileset dir="${external-libs}" includes="*.jar" />

</apply>

</target>

<!-- Put the project's resources into the output package file. -->

<target name="package-res-and-assets">

<echo>Packaging resources and assets...</echo>

<exec executable="${aapt}" failonerror="true">

<arg value="package" />

<arg value="-f" />

<arg value="-M" />

<arg value="AndroidManifest.xml" />

<arg value="-S" />

<arg value="${resource-dir}" />

<arg value="-A" />

<arg value="${asset-dir}" />

<arg value="-I" />

<arg value="${android-jar}" />

<arg value="-F" />

<arg value="${resources-package}" />

</exec>

</target>

<!-- Same as package-res-and-assets, but without "-A ${asset-dir}" -->

<target name="package-res-no-assets">

<echo>Packaging resources...</echo>

<exec executable="${aapt}" failonerror="true">

<arg value="package" />

<arg value="-f" />

<arg value="-M" />

<arg value="AndroidManifest.xml" />

<arg value="-S" />

<arg value="${resource-dir}" />

<!-- No assets directory -->

<arg value="-I" />

<arg value="${android-jar}" />

<arg value="-F" />

<arg value="${resources-package}" />

</exec>

</target>

<!-- Invoke the proper target depending on whether or not an assets directory is present. -->

<!-- TODO: find a nicer way to include the "-A ${asset-dir}" argument only when the assets dir exists. -->

<target name="package-res">

<available file="${asset-dir}" type="dir" property="res-target" value="and-assets" />

<property name="res-target" value="no-assets" />

<antcall target="package-res-${res-target}" />

</target>

<!-- Package the application and sign it with a debug key. This is the default target when building. It is used for debug. -->

<target name="debug" depends="dex, package-res">

<echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>

<exec executable="${apk-builder}" failonerror="true">

<arg value="${out-debug-package-ospath}" />

<arg value="-z" />

<arg value="${resources-package-ospath}" />

<arg value="-f" />

<arg value="${intermediate-dex-ospath}" />

<arg value="-rf" />

<arg value="${srcdir-ospath}" />

<arg value="-rj" />

<arg value="${external-libs-ospath}" />

<!-- 包括本地so文件 -->

<arg value="-nf" />

<arg value="${external-libs-ospath}" />

</exec>

</target>

<!-- Package the application without signing it. This allows for the application to be signed later with an official publishing key. -->

<target name="release" depends="dex, package-res">

<exec executable="${apk-builder}" failonerror="true">

<arg value="${out-unsigned-package-ospath}" />

<arg value="-u" />

<arg value="-z" />

<arg value="${resources-package-ospath}" />

<arg value="-f" />

<arg value="${intermediate-dex-ospath}" />

<arg value="-rf" />

<arg value="${srcdir-ospath}" />

<arg value="-rj" />

<arg value="${external-libs-ospath}" />

<!-- 包括本地so文件 -->

<arg value="-nf" />

<arg value="${external-libs-ospath}" />

</exec>

<echo>It will need to be signed with jarsigner before being published.</echo>

</target>

<!-- Install the package on the default emulator -->

<target name="install" depends="debug">

<echo>Installing ${out-debug-package} onto default emulator...</echo>

<exec executable="${adb}" failonerror="true">

<arg value="install" />

<arg value="${out-debug-package}" />

</exec>

</target>

<target name="reinstall" depends="debug">

<echo>Installing ${out-debug-package} onto default emulator...</echo>

<exec executable="${adb}" failonerror="true">

<arg value="install" />

<arg value="-r" />

<arg value="${out-debug-package}" />

</exec>

</target>

<condition property="doUninstall">

<!--如果arg1的值与arg2的值相等返回true,否则为false-->

<equals arg1="${app.package}" arg2=""/>

</condition>

<!-- Uinstall the package from the default emulator -->

<target name="uninstall" unless="doUninstall">

<echo>Uninstalling ${app.package} from the default emulator...</echo>

<exec executable="${adb}" failonerror="true">

<arg value="uninstall" />

<arg value="${app.package}" />

</exec>

</target>

<!--初始化目录 -->

<target name="init" depends="Copy_Ressource">

<echo message="Init output directory.....">

</echo>

<echo message="====================================" />

<echo message="初始化任务....." />

<echo message="删除bin目录....." />

<delete dir="${outdir}"/>

<echo message="新建bin目录....." />

<mkdir dir="${outdir}" />

</target>

<!--拷贝资源,这里只写了一个assets目录的资源,像res目录下的文件也可以替换,这块代码执行在编译前,我们可以做我们想替换的所有操作,包括替换Java代码内容 -->

<target name="Copy_Ressource">

<echo message="Copy app resource. ">

</echo>

<condition property="doCopyRessourceAssets">

<!--如果arg1的值与arg2的值相等返回true,否则为false-->

<equals arg1="${app.assetssource.path}" arg2=""/>

</condition>

<condition property="doCopyRessourceRes">

<!--如果arg1的值与arg2的值相等返回true,否则为false-->

<equals arg1="${app.ressource.path}" arg2=""/>

</condition>

<antcall target="Copy_Ressource_assets"/>

<antcall target="Copy_Ressource_res"/>

</target>

<!-- Copy_Ressource asset,app.assetssource.path的值不为空的情况下执行 -->

<target name="Copy_Ressource_assets" unless="doCopyRessourceAssets">

<copy todir="${asset-dir}" overwrite="true" failonerror="false">

<fileset dir="${app.assetssource.path}" >

<include name="*.*" />

<exclude name="*svn" />

</fileset>

</copy>

</target>

<!-- Copy_Ressource res,app.resssource.path的值不为空的情况下执行 -->

<target name="Copy_Ressource_res" unless="doCopyRessourceRes">

<copy todir="${asset-dir}" overwrite="true" failonerror="false">

<fileset dir="${app.assetssource.path}" >

<include name="*.*" />

</fileset>

</copy>

</target>

<!--进行签名 -->

<target name="jarsigner" depends="release">

<exec executable="${jarsigner}" failonerror="true">

<!-- 输出详细信息 -->

<arg value="-verbose" />

<arg value="-storepass" />

<arg value="${android.keystore.password}" />

<arg value="-keypass" />

<arg value="${android.keystore.password}" />

<arg value="-keystore" />

<arg value="${android.keystore}" />

<arg value="-signedjar" />

<arg value="${out-signed-package-ospath}" />

<arg value="${out-unsigned-package-ospath}" />

<arg value="${android.keystore.alias}" />

</exec>

</target>

<!--进行优化 -->

<target name="zipalign" depends="jarsigner">

<exec executable="${zipalign}" failonerror="true">

<arg value="-v" />

<arg value="-f" />

<arg value="4" />

<arg value="${out-signed-package-ospath}" />

<arg value="${zipalign-package-ospath}" />

</exec>

</target>

<!--直接上传到手机中去 -->

<target name="adb" depends="zipalign">

<exec executable="${adb}" failonerror="true">

<arg value="install" />

<arg value="-r" />

<arg value="${zipalign-package-ospath}" />

</exec>

</target>

</project>

备注:

[plain] view
plaincopy

如果keystore不在此目录,

你就修改 :

<target name="jarsigner" depends="release">

<exec executable="${jarsigner}" failonerror="true">

<arg value="-verbose" />

<arg value="-storepass" />

<arg value="${password}" />

<arg value="-keystore" />

<!--签名文件-->

<arg value="android.keystore" />

<arg value="-signedjar" />

<arg value="${out-signed-package-ospath}" />

<arg value="${out-unsigned-package-ospath}" />

<!-签名文件的alias-->

<arg value="android" />

</exec>

</target>

中的

<arg value="android.keystore" />

<arg value="android" />

路径。

在这里需要指出的是,由于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,然后配置相应的环境变量信息,最后我们这样调用:

[java] view
plaincopy

Process p = Runtime.getRuntime().exec("ant.bat -buildfile d:/workspace/ant/build.xml");

InputStream is = p.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(is));

String line = null;

while ((line = br.readLine()) != null) {

System.out.println(line);

}

System.out.println("SUCCESS.");

清理完毕,一个修改了属性值的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" />

[javascript] view
plaincopy

<!-- 测试查看环境变量 -->

<property environment="SystemVariable" />

<property name="USERNAME" value="${SystemVariable.USERNAME}" />

<property name="ANDROID_HOME" value="${SystemVariable.ANDROID_HOME}" />

<property name="JAVA_HOME" value="${SystemVariable.JAVA_HOME}" />

<target name="run">

<echo message="### ${USERNAME} ###"/>

<echo message="### ${JAVA_HOME} ###"/>

<echo message="### ${ANDROID_HOME} ###"/>

</target>

五、追加参数的方法

命令后面追加参数使用-D方式,如:

ant -buildfile build.xml -Dapp.name=xiaoshan-Doutput.dir=g:\\releaseapkdir

使用这种方式追加的参数,如果build.properties中已经存在,则会覆盖build.properties中配置的参数值,以追加的参数为准。

六、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 参数,如下:

[html] view
plaincopy

<target name="debug" depends="dex, package-res">

<echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>

<exec executable="${apk-builder}" failonerror="true">

<arg value="${out-debug-package-ospath}" />

<arg value="-z" />

<arg value="${resources-package-ospath}" />

<arg value="-f" />

<arg value="${intermediate-dex-ospath}" />

<arg value="-rf" />

<arg value="${srcdir-ospath}" />

<arg value="-rj" />

<arg value="${external-libs-ospath}" />

<!-- 包括本地so文件 -->

<arg value="-nf" />

<arg value="${external-libs-ospath}" />

</exec>

</target>

官方详细说明为:



十、在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循环和字符串处理的例子

[html] view
plaincopy

<!-- 按逗号分割循环输出,可以使用属性delimiter指定分隔符,默认不指定时,分隔符为英文逗号 -->

<!--replaceresfiles为需要替换的图片文件,格式:源图片文件1:目标图片名称1,源图片文件2:目标图片文件2,……-->

<for list="${replaceresfiles}" param="file" delimiter=",">

<sequential>

<propertyregex property="fromfilepath" input="${file}" regexp="(.*):" select="\1"/>

<propertyregex property="tofilename" input="${file}" regexp=":(.*)" select="\1"/>

<echo message="备份和替换目标文件:@{tofilename}" />

<copy file="${basedir}/${resource-dir}/drawable-hdpi/@{tofilename}" tofile="../temp/build/res/drawable-hdpi/@{tofilename}" overwrite="true" />

<copy file="@{fromfilepath}" tofile="${basedir}/${resource-dir}/drawable-hdpi/${tofilename}" overwrite="true" />

</sequential>

</for>

<pre name="code" class="html">字符串的替换:

将原字符串svr中的password替换为pwd并赋值给变量svr1

<propertyregex property="${svr1}" input="${svr}" regexp='password' replace="pwd"/>

propertyregex元素中有一个 override 属性很重要,默认值是false,特别是在循环中,

如果不添加 override="true" 那么属性只会被设置一次,这个属性的意思是“如果已经被设定值是否替换”

详细参考资料:http://ant-contrib.sourceforge.net/tasks/tasks/propertyregex.html

</pre>

<pre></pre>

<pre></pre>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: