android使用ant自動打包(包括更改文件中的內容)
在ant打包過程中的一些學習所得以及用於這個項目的build文件的大體思路如下:
首先配置好整個係統所需的ant編譯環境,在build.properties中配置好相關的參數.(build.properties文件主要包涵了一些在編譯中需要用到工具的路徑,以及一些需要配置的參數,如應用包名,項目名,以及一些需要傳入的參數.寫在build.properties中主要是為了方便配置文件的集中管理)在這次ant打包的需求中,需要修改一個java文件中的兩個常量屬性.根據這樣的需求,首先需要拿到普通android項目打包的build.xml,和build.properties.
注意配置環境變量:JAVA_HOME、ANDROID_HOME和ANT_HOME
build.properties文件內容如下:
##########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=123456build.xml文件內容如下:
<?xml version="1.0" encoding="UTF-8"?> <project name="antbuildAndroidDemo" default="zipalign" basedir="."> <property > </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 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>備注:
如果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,然後配置相應的環境變量信息,最後我們這樣調用:
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詳解
https://www.cnblogs.com/clarkchen/archive/2011/03/10/1980194.html
ant打包相關參考資料:
https://hi.baidu.com/%F5%CC%C4%A7/blog/item/3f9bc5ec2338ad3726979186.html
https://www.diybl.com/course/3_program/java/javajs/20090201/154692.html
Android ant打包相關:
https://marshal.easymorse.com/archives/1665
https://handsomeliuyang.iteye.com/blog/1156070
https://www.bangchui.org/simple/?t13358.html
四、ant中讀取係統環境變量方法
<property environment="SystemVariable" />
<!-- 測試查看環境變量 --> <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
六、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 >
<pathelement location="bin"/>
</path>
<target name="test">
<condition property="scondition">
<!--在指定的classpath路徑下是否存在資源 TestTest.class-->
<available resource="TestTest.class">
<classpath ref />
</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 參數,如下:
<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”,推薦一個下載地址:https://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循環和字符串處理的例子
<!-- 按逗號分割循環輸出,可以使用屬性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 to overwrite="true" /> <copy to overwrite="true" /> </sequential> </for>
字符串的替換: 將原字符串svr中的password替換為pwd並賦值給變量svr1 <propertyregex property="${svr1}" input="${svr}" regexp='password' replace="pwd"/> propertyregex元素中有一個 override 屬性很重要,默認值是false,特別是在循環中, 如果不添加 override="true" 那麼屬性隻會被設置一次,這個屬性的意思是“如果已經被設定值是否替換” 詳細參考資料:https://ant-contrib.sourceforge.net/tasks/tasks/propertyregex.html
最後更新:2017-04-02 17:09:28