Android使用Ant自動編譯簽名打包詳解

l_serein發表於2012-06-05

1.生成R.java類檔案: 

[xhtml] view plaincopy
  1. <!-- Generate the R.java file for this project's resources. -->  
  2. <target name="resource-src" depends="copy">  
  3.     <echo>Generating R.java / Manifest.java from the resources...</echo>  
  4.     <exec executable="${aapt}" failonerror="true">  
  5.         <arg value="package" />  
  6.         <arg value="-m" />  
  7.         <arg value="-J" />  
  8.         <arg value="${outdir-gen}" />  
  9.         <arg value="-M" />  
  10.         <arg value="AndroidManifest.xml" />  
  11.         <arg value="-S" />  
  12.         <arg value="${resource-dir}" />  
  13.         <arg value="-I" />  
  14.         <arg value="${android-jar}" />  
  15.     </exec>  
  16. </target>  

2.將.aidl檔案生成.java類檔案:

[xhtml] view plaincopy
  1. <!-- Generate java classes from .aidl files. -->  
  2. <target name="aidl" depends="copy">  
  3.     <echo>Compiling aidl files into Java classes...</echo>  
  4.     <apply executable="${aidl}" failonerror="true">  
  5.         <arg value="-p${android-framework}" />  
  6.         <arg value="-I${srcdir}" />  
  7.         <fileset dir="${srcdir}">  
  8.             <include name="**/*.aidl" />  
  9.         </fileset>  
  10.     </apply>  
  11. </target>  

3.編譯.java類檔案生成class檔案:

[xhtml] view plaincopy
  1. <!-- Compile this project's .java files into .class files. -->  
  2. <target name="compile" depends="copy, resource-src, aidl">  
  3.     <javac encoding="GB18030" target="1.5" debug="true" extdirs="" srcdir="." destdir="${outdir-classes}" bootclasspath="${android-jar}">  
  4.         <classpath>  
  5.             <fileset dir="${external-libs}" includes="*.jar" />  
  6.         </classpath>  
  7.     </javac>  
  8. </target>  
4.將class檔案打包生成classes.dex檔案:

[xhtml] view plaincopy
  1. <!-- Convert this project's .class files into .dex files. -->  
  2.     <target name="dex" depends="compile">  
  3.         <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>  
  4.         <apply executable="${dx}" failonerror="true" parallel="true">  
  5.             <arg value="--dex" />  
  6.             <arg value="--output=${intermediate-dex-ospath}" />  
  7.             <arg path="${outdir-obfuscate-classes-ospath}" />  
  8.             <fileset dir="${external-libs}" includes="*.jar" />  
  9.         </apply>  
  10.     </target>  
  
5.打包資原始檔(包括res、assets、androidmanifest.xml等):

[xhtml] view plaincopy
  1. <!-- Put the project's resources into the output package file. -->  
  2. <target name="package-res-and-assets">  
  3.     <echo>Packaging resources and assets...</echo>  
  4.     <exec executable="${aapt}" failonerror="true">  
  5.         <arg value="package" />  
  6.         <arg value="-f" />  
  7.         <arg value="-M" />  
  8.         <arg value="AndroidManifest.xml" />  
  9.         <arg value="-S" />  
  10.         <arg value="${resource-dir}" />  
  11.         <arg value="-A" />  
  12.         <arg value="${asset-dir}" />  
  13.         <arg value="-I" />  
  14.         <arg value="${android-jar}" />  
  15.         <arg value="-F" />  
  16.         <arg value="${resources-package}" />  
  17.     </exec>  
  18. </target>  

6.生成未簽名的apk安裝檔案:

[xhtml] view plaincopy
  1. <target name="package" depends="dex, package-res">  
  2.     <echo>Packaging ${out-unsigned-package} for release...</echo>  
  3.     <exec executable="${apk-builder}" failonerror="true">  
  4.         <arg value="${out-unsigned-package-ospath}" />  
  5.         <arg value="-u" />  
  6.         <arg value="-z" />  
  7.         <arg value="${resources-package-ospath}" />  
  8.         <arg value="-f" />  
  9.         <arg value="${intermediate-dex-ospath}" />  
  10.         <arg value="-rf" />  
  11.         <arg value="${srcdir-ospath}" />  
  12.         <arg value="-rj" />  
  13.         <arg value="${external-libs-ospath}" />  
  14.     </exec>  
  15.     <echo>It will need to be signed with jarsigner before being published.</echo>  
  16. </target>  
7.對未簽名的apk進行簽名生成簽名後的android檔案: 
[xhtml] view plaincopy
  1. <target name="jarsigner" depends="package">  
  2.     <echo>Packaging ${out-unsigned-package} for release...</echo>  
  3.     <exec executable="${jarsigner}" failonerror="true">  
  4.         <arg value="-verbose" />  
  5.         <arg value="-storepass" />  
  6.         <arg value="byread002" />  
  7.         <arg value="-keypass" />  
  8.         <arg value="byread002" />  
  9.         <arg value="-keystore" />  
  10.         <arg value="bbyread.keystore" />  
  11.         <arg value="-signedjar" />  
  12.         <arg value="${out-signed-package-ospath}" />  
  13.         <arg value="${out-unsigned-package-ospath}" />  
  14.         <arg value="byread" />  
  15.     </exec>  
  16. </target>  

*完整Ant配置檔案build.xml:

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project name="Byread" default="debug">  
  3.     <property environment="env" />  
  4.     <property name="sdk-folder" value="${env.ANDROID_SDK_HOME}" />  
  5.     <!-- SDK Locations -->  
  6.     <property name="sdk2.2-folder" value="${env.ANDROID_SDK_HOME}" />  
  7.     <property name="sdk-folder" value="${sdk2.2-folder}/platforms/android-3" />  
  8.     <property name="sdk-tools" value="${sdk-folder}/tools" />  
  9.     <property name="android-tools" value="${sdk2.2-folder}/tools" />  
  10.     <property name="proguardpath" location="${wtkhome}/lib/proguard" />  
  11.   
  12.     <!-- step 1.generate R.java by aapt  2.compile java to class by javac。exe 3.generate classes.dex by dx.bat  -->  
  13.     <!-- 4.package resources by aapt 5. package resource and classes.dex by apkbuilder.bat 6.sign apk by jarsinger -->  
  14.     <!-- Tools -->  
  15.     <property name="aapt" value="${sdk-tools}/aapt.exe" />  
  16.     <property name="dx" value="${sdk-tools}/dx.bat" />  
  17.     <property name="apk-builder" value="${android-tools}/apkbuilder.bat" />  
  18.     <property name="aidl" value="${android-tools}/aidl.exe" />  
  19.     <property name="adb" value="${android-tools}/adb.exe" />  
  20.     <property name="android-jar" value="${sdk-folder}/android.jar" />  
  21.     <property name="jarsigner" value="${env.JAVA_HOME}/bin/jarsigner.exe" />  
  22.   
  23.     <!-- Application Package Name -->  
  24.     <property name="application-package" value="com.byread.reader" />  
  25.     <property name="useragent" value="byAndroidWeb" />  
  26.     <property name="version" value="1.02" />  
  27.     <!-- The intermediates directory -->  
  28.     <!-- Eclipse uses "bin" for its own output, so we do the same. -->  
  29.     <property name="outdir-bin" value="bin" />  
  30.     <property name="outdir-gen" value="gen" />  
  31.     <!-- source directories -->  
  32.     <property name="resource-dir" value="res" />  
  33.     <property name="asset-dir" value="assets" />  
  34.     <property name="srcdir" value="src" />  
  35.     <property name="srcdir-ospath" value="${basedir}/${srcdir}" />  
  36.     <property name="external-libs" value="libs" />  
  37.     <property name="external-libs-ospath" value="${basedir}/${external-libs}" />  
  38.   
  39.     <!-- dest directories -->  
  40.     <property name="des-resource-dir" value="${outdir-bin}/res" />  
  41.     <property name="des-asset-dir" value="${outdir-bin}/assets" />  
  42.     <property name="des-srcdir" value="${outdir-bin}/src" />  
  43.     <property name="des-srcdir-ospath" value="${basedir}/${outdir-bin}/${srcdir}" />  
  44.     <property name="des-external-libs" value="${outdir-bin}/libs" />  
  45.     <property name="des-external-libs-ospath" value="${basedir}/${outdir-bin}/${external-libs}" />  
  46.   
  47.     <!-- Output directories -->  
  48.     <property name="outdir-classes" value="${outdir-bin}/src" />  
  49.     <property name="outdir-obfuscate-classes" value="${outdir-bin}/classes" />  
  50.     <property name="outdir-obfuscate-classes-ospath" value="${basedir}/${outdir-obfuscate-classes}" />  
  51.   
  52.     <!-- Intermediate files -->  
  53.     <property name="dex-file" value="classes.dex" />  
  54.     <property name="intermediate-dex" value="${outdir-bin}/${dex-file}" />  
  55.     <property name="intermediate-dex-ospath" value="${basedir}/${intermediate-dex}" />  
  56.   
  57.     <!-- The final package file to generate -->  
  58.     <property name="resources-package" value="${outdir-bin}/${ant.project.name}" />  
  59.     <property name="resources-package-ospath" value="${basedir}/${resources-package}" />  
  60.     <property name="out-debug-package" value="${outdir-bin}/${ant.project.name}-debug.apk" />  
  61.     <property name="out-debug-package-ospath" value="${basedir}/${out-debug-package}" />  
  62.     <property name="out-unsigned-package" value="${outdir-bin}/${ant.project.name}-unsigned.apk" />  
  63.     <property name="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" />  
  64.     <property name="out-signed-package" value="${useragent}/${ant.project.name}.apk" />  
  65.     <property name="out-signed-package-ospath" value="${basedir}/${out-signed-package}" />  
  66.   
  67.   
  68.     <!-- init -->  
  69.     <target name="init">  
  70.         <echo>Creating all output directories </echo>  
  71.         <delete dir="${outdir-bin}" />  
  72.         <delete dir="${useragent}" />  
  73.         <mkdir dir="${outdir-bin}" />  
  74.         <mkdir dir="${outdir-classes}" />  
  75.         <mkdir dir="${useragent}" />  
  76.     </target>  
  77.   
  78.     <!-- copy original strings and modify useragent -->  
  79.     <target name="copy" depends="init">  
  80.         <echo>copy files to output folder</echo>  
  81.         <delete file="${resource-dir}/values/strings.xml" />  
  82.         <copy file="strings.xml" todir="${resource-dir}/values" />  
  83.         <replace file="${resource-dir}/values/strings.xml" token="@USERAGENT@" value="${useragent}" encoding="utf-8" />  
  84.     </target>  
  85.   
  86.     <!-- Generate the R.java file for this project's resources. -->  
  87.     <target name="resource-src" depends="copy">  
  88.         <echo>Generating R.java / Manifest.java from the resources...</echo>  
  89.         <exec executable="${aapt}" failonerror="true">  
  90.             <arg value="package" />  
  91.             <arg value="-m" />  
  92.             <arg value="-J" />  
  93.             <arg value="${outdir-gen}" />  
  94.             <arg value="-M" />  
  95.             <arg value="AndroidManifest.xml" />  
  96.             <arg value="-S" />  
  97.             <arg value="${resource-dir}" />  
  98.             <arg value="-I" />  
  99.             <arg value="${android-jar}" />  
  100.         </exec>  
  101.     </target>  
  102.   
  103.     <!-- Generate java classes from .aidl files. -->  
  104.     <target name="aidl" depends="copy">  
  105.         <echo>Compiling aidl files into Java classes...</echo>  
  106.         <apply executable="${aidl}" failonerror="true">  
  107.             <arg value="-p${android-framework}" />  
  108.             <arg value="-I${srcdir}" />  
  109.             <fileset dir="${srcdir}">  
  110.                 <include name="**/*.aidl" />  
  111.             </fileset>  
  112.         </apply>  
  113.     </target>  
  114.   
  115.     <!-- Compile this project's .java files into .class files. -->  
  116.     <target name="compile" depends="copy, resource-src, aidl">  
  117.         <javac encoding="GB18030" target="1.5" debug="true" extdirs="" srcdir="." destdir="${outdir-classes}" bootclasspath="${android-jar}">  
  118.             <classpath>  
  119.                 <fileset dir="${external-libs}" includes="*.jar" />  
  120.             </classpath>  
  121.         </javac>  
  122.     </target>  
  123.   
  124.   
  125.   
  126.   
  127.     <!-- Convert this project's .class files into .dex files. -->  
  128.     <target name="dex" depends="compile">  
  129.         <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>  
  130.         <apply executable="${dx}" failonerror="true" parallel="true">  
  131.             <arg value="--dex" />  
  132.             <arg value="--output=${intermediate-dex-ospath}" />  
  133.             <arg path="${outdir-obfuscate-classes-ospath}" />  
  134.             <fileset dir="${external-libs}" includes="*.jar" />  
  135.         </apply>  
  136.     </target>  
  137.   
  138.     <!-- Put the project's resources into the output package file. -->  
  139.     <target name="package-res-and-assets">  
  140.         <echo>Packaging resources and assets...</echo>  
  141.         <exec executable="${aapt}" failonerror="true">  
  142.             <arg value="package" />  
  143.             <arg value="-f" />  
  144.             <arg value="-M" />  
  145.             <arg value="AndroidManifest.xml" />  
  146.             <arg value="-S" />  
  147.             <arg value="${resource-dir}" />  
  148.             <arg value="-A" />  
  149.             <arg value="${asset-dir}" />  
  150.             <arg value="-I" />  
  151.             <arg value="${android-jar}" />  
  152.             <arg value="-F" />  
  153.             <arg value="${resources-package}" />  
  154.         </exec>  
  155.     </target>  
  156.   
  157.     <!-- Same as package-res-and-assets, but without "-A ${asset-dir}" -->  
  158.     <target name="package-res-no-assets">  
  159.         <echo>Packaging resources...</echo>  
  160.         <exec executable="${aapt}" failonerror="true">  
  161.             <arg value="package" />  
  162.             <arg value="-f" />  
  163.             <arg value="-M" />  
  164.             <arg value="AndroidManifest.xml" />  
  165.             <arg value="-S" />  
  166.             <arg value="${resource-dir}" />  
  167.             <!-- No assets directory -->  
  168.             <arg value="-I" />  
  169.             <arg value="${android-jar}" />  
  170.             <arg value="-F" />  
  171.             <arg value="${resources-package}" />  
  172.         </exec>  
  173.     </target>  
  174.   
  175.     <!-- Invoke the proper target depending on whether or not  
  176.                  an assets directory is present. -->  
  177.     <!-- TODO: find a nicer way to include the "-A ${asset-dir}" argument  
  178.                  only when the assets dir exists. -->  
  179.     <target name="package-res">  
  180.         <available file="${asset-dir}" type="dir" property="res-target" value="and-assets" />  
  181.         <property name="res-target" value="no-assets" />  
  182.         <antcall target="package-res-${res-target}" />  
  183.     </target>  
  184.   
  185.     <!-- Package the application and sign it with a debug key.  
  186.                  This is the default target when building. It is used for debug. -->  
  187.     <target name="debug" depends="dex, package-res">  
  188.         <echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>  
  189.         <exec executable="${apk-builder}" failonerror="true">  
  190.             <arg value="${out-debug-package-ospath}" />  
  191.             <arg value="-z" />  
  192.             <arg value="${resources-package-ospath}" />  
  193.             <arg value="-f" />  
  194.             <arg value="${intermediate-dex-ospath}" />  
  195.             <arg value="-rf" />  
  196.             <arg value="${srcdir-ospath}" />  
  197.             <arg value="-rj" />  
  198.             <arg value="${external-libs-ospath}" />  
  199.         </exec>  
  200.     </target>  
  201.   
  202.     <!-- Package the application without signing it.  
  203.                  This allows for the application to be signed later with an official publishing key. -->  
  204.     <target name="package" depends="dex, package-res">  
  205.         <echo>Packaging ${out-unsigned-package} for release...</echo>  
  206.         <exec executable="${apk-builder}" failonerror="true">  
  207.             <arg value="${out-unsigned-package-ospath}" />  
  208.             <arg value="-u" />  
  209.             <arg value="-z" />  
  210.             <arg value="${resources-package-ospath}" />  
  211.             <arg value="-f" />  
  212.             <arg value="${intermediate-dex-ospath}" />  
  213.             <arg value="-rf" />  
  214.             <arg value="${srcdir-ospath}" />  
  215.             <arg value="-rj" />  
  216.             <arg value="${external-libs-ospath}" />  
  217.         </exec>  
  218.         <echo>It will need to be signed with jarsigner before being published.</echo>  
  219.     </target>  
  220.   
  221.     <target name="jarsigner" depends="package">  
  222.         <echo>Packaging ${out-unsigned-package} for release...</echo>  
  223.         <exec executable="${jarsigner}" failonerror="true">  
  224.             <arg value="-verbose" />  
  225.             <arg value="-storepass" />  
  226.             <arg value="byread002" />  
  227.             <arg value="-keypass" />  
  228.             <arg value="byread002" />  
  229.             <arg value="-keystore" />  
  230.             <arg value="bbyread.keystore" />  
  231.             <arg value="-signedjar" />  
  232.             <arg value="${out-signed-package-ospath}" />  
  233.             <arg value="${out-unsigned-package-ospath}" />  
  234.             <arg value="byread" />  
  235.         </exec>  
  236.     </target>  
  237.   
  238.     <target name="release" depends="jarsigner">  
  239.         <echo>release for release...</echo>  
  240.     </target>  
  241.   
  242.     <!-- Install the package on the default emulator -->  
  243.     <target name="install" depends="debug">  
  244.         <echo>Installing ${out-debug-package} onto default emulator...</echo>  
  245.         <exec executable="${adb}" failonerror="true">  
  246.             <arg value="install" />  
  247.             <arg value="${out-debug-package}" />  
  248.         </exec>  
  249.     </target>  
  250.   
  251.     <target name="reinstall" depends="debug">  
  252.         <echo>Installing ${out-debug-package} onto default emulator...</echo>  
  253.         <exec executable="${adb}" failonerror="true">  
  254.             <arg value="install" />  
  255.             <arg value="-r" />  
  256.             <arg value="${out-debug-package}" />  
  257.         </exec>  
  258.     </target>  
  259.   
  260.     <!-- Uinstall the package from the default emulator -->  
  261.     <target name="uninstall">  
  262.         <echo>Uninstalling ${application-package} from the default emulator...</echo>  
  263.         <exec executable="${adb}" failonerror="true">  
  264.             <arg value="uninstall" />  
  265.             <arg value="${application-package}" />  
  266.         </exec>  
  267.     </target>  
  268.   
  269. </project>  

*在工程目錄下建立一個build.bat批處理檔案,內容為:

ant -buildfile build.xml

執行這個批處理檔案即可。

注意:SDK的安裝目錄最後不要有中文和空格,否則會出現不必要的錯誤。如果你的安裝路徑包含Program Files(有空格),Ant方式打包會報錯,沒關係,不用重灌,解決方法:配置SDk環境變數的時候把Program Files改為Progra~1即可。

轉自:http://jojol-zhou.iteye.com/blog/729271

相關文章