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文件内容如下:

[plain]  view plain copy
  1. ##########SHANHY############  
  2. #\u7F16\u8BD1\u7248\u672C  
  3. compile.target=1.6  
  4. android.sdk.apilevel=8  
  5. #\u5E94\u7528\u7A0B\u5E8F\u5305\u540D(\u5728uninstall\u65F6\u9700\u8981)  
  6. app.package=com.surfing.conference  
  7. #\u590D\u5236\u8D44\u6E90  
  8. app.assetssource.path=  
  9. app.ressource.path=  
  10. #\u53D1\u5E03\u7684\u5BA2\u6237\u7AEF\u540D\u79F0  
  11. app.apkname=antPackageDemo  
  12. #\u53D1\u5E03\u7684\u5BA2\u6237\u7AEF\u5B58\u653E\u4F4D\u7F6E(\u53EF\u4EE5\u662F\u76F8\u5BF9\u8DEF\u5F84\u6216\u7EDD\u5BF9\u8DEF\u5F84)  
  13. output.dir=releaseapkdir  
  14. #########KeyStore Info##########  
  15. android.keystore=xiaoshan.key  
  16. #keystore.alias  
  17. android.keystore.alias=shanhy  
  18. #keystore.password  
  19. android.keystore.password=123456  
build.xml文件内容如下:
[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project name="antbuildAndroidDemo" default="zipalign" basedir=".">  
  3.     <property file="build.properties">  
  4.     </property>  
  5.     <property name="ENCODEING_CHARSET" value="UTF-8" />  
  6.       
  7.     <property environment="SystemVariable" />  
  8.     <property name="sdk.folder" value="${SystemVariable.ANDROID_HOME}/platforms/android-${android.sdk.apilevel}" />  
  9.     <property name="android.tools" value="${SystemVariable.ANDROID_HOME}/platform-tools" />  
  10.     <property name="apk.tools" value="${SystemVariable.ANDROID_HOME}/tools" />  
  11.     <property name="jdk.home" value="${SystemVariable.JAVA_HOME}" />  
  12.   
  13.     <!-- The intermediates directory -->  
  14.     <!-- Eclipse uses "bin" for its own output, so we do the same. -->  
  15.     <property name="outdir" value="bin" />  
  16.   
  17.     <!-- ************************************************************************************* -->  
  18.     <!-- No user servicable parts below. -->  
  19.   
  20.     <property name="android-framework" value="${sdk.folder}/framework.aidl" />  
  21.   
  22.     <!-- Input directories -->  
  23.     <property name="resource-dir" value="res" />  
  24.     <property name="asset-dir" value="assets" />  
  25.     <property name="srcdir" value="src" />  
  26.           
  27.     <condition property="srcdir-ospath" value="${basedir}/${srcdir}" else="${basedir}/${srcdir}">  
  28.         <os family="windows" />  
  29.     </condition>  
  30.   
  31.     <property name="external-libs" value="libs" />  
  32.     <condition property="external-libs-ospath" value="${basedir}/${external-libs}" else="${basedir}/${external-libs}">  
  33.         <os family="windows" />  
  34.     </condition>  
  35.   
  36.     <!-- Output directories -->  
  37.     <property name="outdir-classes" value="${outdir}/classes" />  
  38.     <condition property="outdir-classes-ospath" value="${basedir}/${outdir-classes}" else="${basedir}/${outdir-classes}">  
  39.         <os family="windows" />  
  40.     </condition>  
  41.   
  42.     <condition property="zipalign-package-ospath" value="${output.dir}/${app.apkname}.apk" else="${output.dir}/${app.apkname}.apk">  
  43.         <os family="windows" />  
  44.     </condition>  
  45.   
  46.     <!-- Create R.java in the source directory -->  
  47.     <property name="outdir-r" value="gen" />  
  48.       
  49.     <!-- Intermediate files -->  
  50.     <property name="dex-file" value="classes.dex" />  
  51.     <property name="intermediate-dex" value="${outdir}/${dex-file}" />  
  52.     <condition property="intermediate-dex-ospath" value="${basedir}/${intermediate-dex}" else="${basedir}/${intermediate-dex}">  
  53.         <os family="windows" />  
  54.     </condition>  
  55.   
  56.     <!-- The final package file to generate -->  
  57.     <property name="resources-package" value="${outdir}/${ant.project.name}.ap_" />  
  58.     <condition property="resources-package-ospath" value="${basedir}/${resources-package}" else="${basedir}/${resources-package}">  
  59.         <os family="windows" />  
  60.     </condition>  
  61.   
  62.     <property name="out-debug-package" value="${outdir}/${ant.project.name}-debug.apk" />  
  63.     <condition property="out-debug-package-ospath" value="${basedir}/${out-debug-package}" else="${basedir}/${out-debug-package}">  
  64.         <os family="windows" />  
  65.     </condition>  
  66.   
  67.     <property name="out-unsigned-package" value="${outdir}/${ant.project.name}-unsigned.apk" />  
  68.     <property name="out-signed-package" value="${outdir}/${ant.project.name}-signed.apk" />  
  69.     <condition property="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" else="${basedir}/${out-unsigned-package}">  
  70.         <os family="windows" />  
  71.     </condition>  
  72.     <condition property="out-signed-package-ospath" value="${basedir}/${out-signed-package}" else="${basedir}/${out-signed-package}">  
  73.         <os family="windows" />  
  74.     </condition>  
  75.   
  76.     <!-- Tools -->  
  77.     <condition property="aapt" value="${android.tools}/aapt.exe" else="${android.tools}/aapt">  
  78.         <os family="windows" />  
  79.     </condition>  
  80.     <condition property="zipalign" value="${apk.tools}/zipalign.exe" else="${apk.tools}/zipalign">  
  81.         <os family="windows" />  
  82.     </condition>  
  83.     <condition property="jarsigner" value="${jdk.home}/bin/jarsigner.exe" else="${jdk.home}/bin/jarsigner">  
  84.         <os family="windows" />  
  85.     </condition>  
  86.     <condition property="aidl" value="${android.tools}/aidl.exe" else="${android.tools}/aidl">  
  87.         <os family="windows" />  
  88.     </condition>  
  89.     <condition property="adb" value="${android.tools}/adb.exe" else="${apk.tools}/adb">  
  90.         <os family="windows" />  
  91.     </condition>  
  92.     <condition property="dx" value="${android.tools}/dx.bat" else="${android.tools}/dx">  
  93.         <os family="windows" />  
  94.     </condition>  
  95.     <condition property="apk-builder" value="${apk.tools}/apkbuilder.bat" else="${apk.tools}/apkbuilder">  
  96.         <os family="windows" />  
  97.     </condition>  
  98.   
  99.     <property name="android-jar" value="${sdk.folder}/android.jar" />  
  100.   
  101.     <!-- Rules -->  
  102.   
  103.     <!-- Create the output directories if they don't exist yet. -->  
  104.     <target name="dirs" depends="init">  
  105.         <echo>Creating output directories if needed...</echo>  
  106.         <mkdir dir="${outdir}" />  
  107.         <mkdir dir="${outdir-classes}" />  
  108.     </target>  
  109.   
  110.     <!-- Generate the R.java file for this project's resources. -->  
  111.     <target name="resource-src" depends="dirs">  
  112.         <echo>Generating R.java / Manifest.java from the resources...</echo>  
  113.         <exec executable="${aapt}" failonerror="true">  
  114.             <arg value="package" />  
  115.             <arg value="-m" />  
  116.             <arg value="-J" />  
  117.             <arg value="${outdir-r}" />  
  118.             <arg value="-M" />  
  119.             <arg value="AndroidManifest.xml" />  
  120.             <arg value="-S" />  
  121.             <arg value="${resource-dir}" />  
  122.             <arg value="-I" />  
  123.             <arg value="${android-jar}" />  
  124.         </exec>  
  125.     </target>  
  126.   
  127.     <!-- Generate java classes from .aidl files. -->  
  128.     <target name="aidl" depends="dirs">  
  129.         <echo>Compiling aidl files into Java classes...</echo>  
  130.         <apply executable="${aidl}" failonerror="true">  
  131.             <arg value="-p${android-framework}" />  
  132.             <arg value="-I${srcdir}" />  
  133.             <fileset dir="${srcdir}">  
  134.                 <include name="**/*.aidl" />  
  135.             </fileset>  
  136.         </apply>  
  137.     </target>  
  138.   
  139.     <!-- Compile this project's .java files into .class files. -->  
  140.     <target name="compile" depends="dirs, resource-src, aidl">  
  141.         <!-- 下面的encoding要与项目的整体编码一致,否则会出现“编码 xxx  的不可映射字符” -->  
  142.         <javac encoding="${ENCODEING_CHARSET}" target="${compile.target}" debug="true" extdirs="" srcdir="." destdir="${outdir-classes}" bootclasspath="${android-jar}" includeantruntime="on">  
  143.             <classpath>  
  144.                 <fileset dir="${external-libs}" includes="*.*" />  
  145.             </classpath>  
  146.         </javac>  
  147.     </target>  
  148.   
  149.     <!-- Convert this project's .class files into .dex files. -->  
  150.     <target name="dex" depends="compile">  
  151.         <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>  
  152.         <apply executable="${dx}" failonerror="true" parallel="true">  
  153.             <arg value="--dex" />  
  154.             <!--mce:0 -->  
  155.             <!--  
  156.             <script src="/javascripts/tinymce/themes/advanced/langs/zh.js" type="text/javascript">  
  157.             </script>  
  158.             -->  
  159.             <!--mce:1 -->  
  160.             <!--  
  161.             <script src="/javascripts/tinymce/plugins/javaeye/langs/zh.js" type="text/javascript">  
  162.             </script>  
  163.             -->  
  164.             <arg value="--output=${intermediate-dex-ospath}" />  
  165.             <arg path="${outdir-classes-ospath}" />  
  166.             <fileset dir="${external-libs}" includes="*.jar" />  
  167.         </apply>  
  168.     </target>  
  169.   
  170.     <!-- Put the project's resources into the output package file. -->  
  171.     <target name="package-res-and-assets">  
  172.         <echo>Packaging resources and assets...</echo>  
  173.         <exec executable="${aapt}" failonerror="true">  
  174.             <arg value="package" />  
  175.             <arg value="-f" />  
  176.             <arg value="-M" />  
  177.             <arg value="AndroidManifest.xml" />  
  178.             <arg value="-S" />  
  179.             <arg value="${resource-dir}" />  
  180.             <arg value="-A" />  
  181.             <arg value="${asset-dir}" />  
  182.             <arg value="-I" />  
  183.             <arg value="${android-jar}" />  
  184.             <arg value="-F" />  
  185.             <arg value="${resources-package}" />  
  186.         </exec>  
  187.     </target>  
  188.   
  189.     <!-- Same as package-res-and-assets, but without "-A ${asset-dir}" -->  
  190.     <target name="package-res-no-assets">  
  191.         <echo>Packaging resources...</echo>  
  192.         <exec executable="${aapt}" failonerror="true">  
  193.             <arg value="package" />  
  194.             <arg value="-f" />  
  195.             <arg value="-M" />  
  196.             <arg value="AndroidManifest.xml" />  
  197.             <arg value="-S" />  
  198.             <arg value="${resource-dir}" />  
  199.             <!-- No assets directory -->  
  200.             <arg value="-I" />  
  201.             <arg value="${android-jar}" />  
  202.             <arg value="-F" />  
  203.             <arg value="${resources-package}" />  
  204.         </exec>  
  205.     </target>  
  206.   
  207.     <!-- Invoke the proper target depending on whether or not an assets directory is present. -->  
  208.     <!-- TODO: find a nicer way to include the "-A ${asset-dir}" argument only when the assets dir exists. -->  
  209.     <target name="package-res">  
  210.         <available file="${asset-dir}" type="dir" property="res-target" value="and-assets" />  
  211.         <property name="res-target" value="no-assets" />  
  212.         <antcall target="package-res-${res-target}" />  
  213.     </target>  
  214.   
  215.     <!-- Package the application and sign it with a debug key. This is the default target when building. It is used for debug. -->  
  216.     <target name="debug" depends="dex, package-res">  
  217.         <echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>  
  218.         <exec executable="${apk-builder}" failonerror="true">  
  219.             <arg value="${out-debug-package-ospath}" />  
  220.             <arg value="-z" />  
  221.             <arg value="${resources-package-ospath}" />  
  222.             <arg value="-f" />  
  223.             <arg value="${intermediate-dex-ospath}" />  
  224.             <arg value="-rf" />  
  225.             <arg value="${srcdir-ospath}" />  
  226.             <arg value="-rj" />  
  227.             <arg value="${external-libs-ospath}" />  
  228.             <!-- 包括本地so文件 -->  
  229.             <arg value="-nf" />  
  230.             <arg value="${external-libs-ospath}" />  
  231.         </exec>  
  232.     </target>  
  233.   
  234.     <!-- Package the application without signing it. This allows for the application to be signed later with an official publishing key. -->  
  235.     <target name="release" depends="dex, package-res">  
  236.         <exec executable="${apk-builder}" failonerror="true">  
  237.             <arg value="${out-unsigned-package-ospath}" />  
  238.             <arg value="-u" />  
  239.             <arg value="-z" />  
  240.             <arg value="${resources-package-ospath}" />  
  241.             <arg value="-f" />  
  242.             <arg value="${intermediate-dex-ospath}" />  
  243.             <arg value="-rf" />  
  244.             <arg value="${srcdir-ospath}" />  
  245.             <arg value="-rj" />  
  246.             <arg value="${external-libs-ospath}" />  
  247.             <!-- 包括本地so文件 -->  
  248.             <arg value="-nf" />  
  249.             <arg value="${external-libs-ospath}" />  
  250.         </exec>  
  251.         <echo>It will need to be signed with jarsigner before being published.</echo>  
  252.     </target>  
  253.   
  254.     <!-- Install the package on the default emulator -->  
  255.     <target name="install" depends="debug">  
  256.         <echo>Installing ${out-debug-package} onto default emulator...</echo>  
  257.         <exec executable="${adb}" failonerror="true">  
  258.             <arg value="install" />  
  259.             <arg value="${out-debug-package}" />  
  260.         </exec>  
  261.     </target>  
  262.   
  263.     <target name="reinstall" depends="debug">  
  264.         <echo>Installing ${out-debug-package} onto default emulator...</echo>  
  265.         <exec executable="${adb}" failonerror="true">  
  266.             <arg value="install" />  
  267.             <arg value="-r" />  
  268.             <arg value="${out-debug-package}" />  
  269.         </exec>  
  270.     </target>  
  271.   
  272.     <condition property="doUninstall">  
  273.          <!--如果arg1的值与arg2的值相等返回true,否则为false-->  
  274.          <equals arg1="${app.package}" arg2=""/>  
  275.      </condition>  
  276.     <!-- Uinstall the package from the default emulator -->  
  277.     <target name="uninstall" unless="doUninstall">  
  278.         <echo>Uninstalling ${app.package} from the default emulator...</echo>  
  279.         <exec executable="${adb}" failonerror="true">  
  280.             <arg value="uninstall" />  
  281.             <arg value="${app.package}" />  
  282.         </exec>  
  283.     </target>  
  284.   
  285.     <!--初始化目录 -->  
  286.     <target name="init" depends="Copy_Ressource">  
  287.         <echo message="Init output directory.....">  
  288.         </echo>  
  289.         <echo message="====================================" />  
  290.          <echo message="初始化任务....." />  
  291.          <echo message="删除bin目录....." />  
  292.          <delete dir="${outdir}"/>  
  293.          <echo message="新建bin目录....." />  
  294.         <mkdir dir="${outdir}" />  
  295.     </target>  
  296.     <!--拷贝资源,这里只写了一个assets目录的资源,像res目录下的文件也可以替换,这块代码执行在编译前,我们可以做我们想替换的所有操作,包括替换Java代码内容 -->  
  297.     <target name="Copy_Ressource">  
  298.         <echo message="Copy app resource. ">  
  299.         </echo>  
  300.         <condition property="doCopyRessourceAssets">  
  301.              <!--如果arg1的值与arg2的值相等返回true,否则为false-->  
  302.              <equals arg1="${app.assetssource.path}" arg2=""/>  
  303.          </condition>  
  304.         <condition property="doCopyRessourceRes">  
  305.              <!--如果arg1的值与arg2的值相等返回true,否则为false-->  
  306.              <equals arg1="${app.ressource.path}" arg2=""/>  
  307.          </condition>  
  308.           
  309.         <antcall target="Copy_Ressource_assets"/>  
  310.         <antcall target="Copy_Ressource_res"/>  
  311.     </target>  
  312.       
  313.     <!-- Copy_Ressource asset,app.assetssource.path的值不为空的情况下执行 -->  
  314.     <target name="Copy_Ressource_assets" unless="doCopyRessourceAssets">  
  315.         <copy todir="${asset-dir}" overwrite="true" failonerror="false">  
  316.                 <fileset dir="${app.assetssource.path}" >  
  317.                     <include name="*.*" />  
  318.                     <exclude name="*svn" />  
  319.                 </fileset>  
  320.             </copy>  
  321.     </target>  
  322.       
  323.     <!-- Copy_Ressource res,app.resssource.path的值不为空的情况下执行 -->  
  324.     <target name="Copy_Ressource_res" unless="doCopyRessourceRes">  
  325.         <copy todir="${asset-dir}" overwrite="true" failonerror="false">  
  326.                 <fileset dir="${app.assetssource.path}" >  
  327.                     <include name="*.*" />  
  328.                 </fileset>  
  329.             </copy>  
  330.     </target>  
  331.       
  332.     <!--进行签名 -->  
  333.     <target name="jarsigner" depends="release">  
  334.         <exec executable="${jarsigner}" failonerror="true">  
  335.             <!-- 输出详细信息 -->  
  336.             <arg value="-verbose" />  
  337.             <arg value="-storepass" />  
  338.             <arg value="${android.keystore.password}" />  
  339.             <arg value="-keypass" />   
  340.             <arg value="${android.keystore.password}" />  
  341.             <arg value="-keystore" />  
  342.             <arg value="${android.keystore}" />  
  343.             <arg value="-signedjar" />  
  344.             <arg value="${out-signed-package-ospath}" />  
  345.             <arg value="${out-unsigned-package-ospath}" />  
  346.             <arg value="${android.keystore.alias}" />  
  347.         </exec>  
  348.     </target>  
  349.     <!--进行优化 -->  
  350.     <target name="zipalign" depends="jarsigner">  
  351.         <exec executable="${zipalign}" failonerror="true">  
  352.             <arg value="-v" />  
  353.             <arg value="-f" />  
  354.             <arg value="4" />  
  355.             <arg value="${out-signed-package-ospath}" />  
  356.             <arg value="${zipalign-package-ospath}" />  
  357.         </exec>  
  358.     </target>  
  359.     <!--直接上传到手机中去 -->  
  360.     <target name="adb" depends="zipalign">  
  361.         <exec executable="${adb}" failonerror="true">  
  362.             <arg value="install" />  
  363.             <arg value="-r" />  
  364.             <arg value="${zipalign-package-ospath}" />  
  365.         </exec>  
  366.     </target>  
  367. </project>    
备注:
[plain]  view plain copy
  1. 如果keystore不在此目录,  
  2. 你就修改 :  
  3.   
  4.    <target name="jarsigner" depends="release">   
  5.         <exec executable="${jarsigner}" failοnerrοr="true">   
  6.             <arg value="-verbose" />   
  7.             <arg value="-storepass" />   
  8.             <arg value="${password}" />   
  9.             <arg value="-keystore" />   
  10.             <!--签名文件-->  
  11.             <arg value="android.keystore" />   
  12.             <arg value="-signedjar" />   
  13.             <arg value="${out-signed-package-ospath}" />   
  14.             <arg value="${out-unsigned-package-ospath}" />   
  15.             <!-签名文件的alias-->  
  16.             <arg value="android" />   
  17.         </exec>   
  18.     </target>   
  19.   
  20. 中的  
  21. <arg value="android.keystore" />   
  22. <arg value="android" />   
  23. 路径。  


在这里需要指出的是,由于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 plain copy
  1. Process p = Runtime.getRuntime().exec("ant.bat -buildfile d:/workspace/ant/build.xml");  
  2. InputStream is = p.getInputStream();  
  3. BufferedReader br = new BufferedReader(new InputStreamReader(is));  
  4. String line = null;  
  5. while ((line = br.readLine()) != null) {  
  6.     System.out.println(line);  
  7. }  
  8. System.out.println("SUCCESS.");  

清理完毕,一个修改了属性值的apk包就自动生成了.


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

其他说明:

一、Windows 批处理循环处理

接下来,需求有了进一步的加强,我们需要10几个相同公司名,不同id的包.那么我们怎么自动生成这么一些包呢,这里我用到了dos命令来完成我们的需求(ant虽然也能实现,但是好像ant中执行for需要有插件支持,并且对ant不算太熟,所以,这里我采用dos来做),一下代码是生成指定公司名的不同iddos代码.

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.同时用一个循环完成输入的minmax次调用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 plain copy
  1. <!-- 测试查看环境变量  -->    
  2. <property environment="SystemVariable" />    
  3. <property name="USERNAME" value="${SystemVariable.USERNAME}" />    
  4. <property name="ANDROID_HOME" value="${SystemVariable.ANDROID_HOME}" />    
  5. <property name="JAVA_HOME" value="${SystemVariable.JAVA_HOME}" />    
  6. <target name="run">    
  7.     <echo message="### ${USERNAME} ###"/>    
  8.     <echo message="### ${JAVA_HOME} ###"/>    
  9.     <echo message="### ${ANDROID_HOME} ###"/>    
  10. </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 plain copy
  1. <target name="debug" depends="dex, package-res">  
  2.     <echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>  
  3.     <exec executable="${apk-builder}" failonerror="true">  
  4.         <arg value="${out-debug-package-ospath}" />  
  5.         <arg value="-z" />  
  6.         <arg value="${resources-package-ospath}" />  
  7.         <arg value="-f" />  
  8.         <arg value="${intermediate-dex-ospath}" />  
  9.         <arg value="-rf" />  
  10.         <arg value="${srcdir-ospath}" />  
  11.         <arg value="-rj" />  
  12.         <arg value="${external-libs-ospath}" />  
  13.         <!-- 包括本地so文件 -->  
  14.         <arg value="-nf" />  
  15.         <arg value="${external-libs-ospath}" />  
  16.     </exec>  
  17. </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 plain copy
  1.         <!-- 按逗号分割循环输出,可以使用属性delimiter指定分隔符,默认不指定时,分隔符为英文逗号 -->  
  2.         <!--replaceresfiles为需要替换的图片文件,格式:源图片文件1:目标图片名称1,源图片文件2:目标图片文件2,……-->  
  3.         <for list="${replaceresfiles}" param="file" delimiter=",">  
  4.             <sequential>  
  5.                 <propertyregex property="fromfilepath" input="${file}" regexp="(.*):" select="\1"/>  
  6.                 <propertyregex property="tofilename" input="${file}" regexp=":(.*)" select="\1"/>  
  7.                 <echo message="备份和替换目标文件:@{tofilename}" />  
  8.                 <copy file="${basedir}/${resource-dir}/drawable-hdpi/@{tofilename}" tofile="../temp/build/res/drawable-hdpi/@{tofilename}" overwrite="true" />  
  9.                 <copy file="@{fromfilepath}" tofile="${basedir}/${resource-dir}/drawable-hdpi/${tofilename}" overwrite="true" />  
  10.             </sequential>  
  11.         </for>  
  12. <pre name="code" class="html">字符串的替换:  
  13. 将原字符串svr中的password替换为pwd并赋值给变量svr1  
  14. <propertyregex property="${svr1}" input="${svr}" regexp='password' replace="pwd"/>  
  15. propertyregex元素中有一个 override 属性很重要,默认值是false,特别是在循环中,  
  16. 如果不添加 override="true" 那么属性只会被设置一次,这个属性的意思是“如果已经被设定值是否替换”  
  17. 详细参考资料:http://ant-contrib.sourceforge.net/tasks/tasks/propertyregex.html  
  18. </pre>  
  19. <pre></pre>  
  20. <pre></pre>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值