在Windows下用ant編譯Android應用生成apk安裝包

yangxi_001發表於2014-03-19

一般情況下,我們用Eclipse開發Android應用,幾乎從來沒有考慮用命令列來編譯,因為Eclipse太強大了以至於Android開發成為了傻瓜。

今天,我決心來嘗試一下用ant來編譯Android應用,ant編譯Android應用其實就是呼叫多個Android SDK提供的多個編譯工具,最後編譯打包簽名,形成apk檔案。

我的開發環境是Windows XP,Java是1.6的,Eclipse是adt自帶的那個。下面是用ant來編譯Android應用的build.xml的內容。

<project default="main" basedir=".">
 
    <property name="apk-name" value="product" />
    <property name="apk-version" value="latest" />
    <property name="apk-market" value="dev" />
    <property name="basedir" value="." />
 
    <property name="android-jar" value="E:/adt-bundle-windows-x86-20130219/sdk/platforms/android-8/android.jar" />
    <!-- <property name="android-jar" value="/usr/lib/android-sdk/platforms/android-10/android.jar" /> -->
 
    <target name="init">
        <echo>start initing ... </echo>
 
        <mkdir dir="out" />
        <delete>
            <fileset dir="out"></fileset>
        </delete>
         
        <mkdir dir="gen" />
        <delete>
            <fileset dir="gen"></fileset>
        </delete>
         
        <mkdir dir="bin/classes" />
        <delete>
            <fileset dir="bin/classes"></fileset>
        </delete>
         
        <mkdir dir="build/${apk-version}" />
 
        <echo>finish initing. </echo>
    </target>
 
    <target name="main" depends="init">
        <echo>generating R.java for project to dir gen (using aapt) ... </echo>
        <exec executable="aapt">
            <arg value="package" />
            <arg value="-m" />
            <arg value="-J" />
            <arg value="gen" />
            <arg value="-M" />
            <arg value="AndroidManifest.xml" />
            <arg value="-S" />
            <arg value="res" />
            <arg value="-S" />
            <arg value="${basedir}/res" />
            <arg value="-I" />
            <arg value="${android-jar}" />
            <arg value="--auto-add-overlay" />
        </exec>
 
        <echo>generating R.java for library to dir gen (using aapt) ... </echo>
        <exec executable="aapt">
            <arg value="package" />
            <arg value="-m" />
            <arg value="--non-constant-id" />
            <arg value="--auto-add-overlay" />
            <arg value="-J" />
            <arg value="gen" />
            <arg value="-M" />
            <arg value="${basedir}/AndroidManifest.xml" />
            <arg value="-S" />
            <arg value="res" />
            <arg value="-S" />
            <arg value="${basedir}/res" />
            <arg value="-I" />
            <arg value="${android-jar}" />
        </exec>
 
        <path id="project.libs">
            <fileset dir="libs">
                <include name="*.jar" />
            </fileset>
        </path>
        <echo>compiling java files to class files (include R.java, library and the third-party jars) ... </echo>
        <javac destdir="bin/classes" bootclasspath="${android-jar}">
            <src path="${basedir}/src" />
            <src path="src" />
            <src path="gen" />
            <classpath refid="project.libs" />
        </javac>
 
        <echo>packaging class files (include the third-party jars) to calsses.dex ... </echo>
        <!-- <exec executable="dx"> -->
        <exec executable="dx.bat">
            <arg value="--dex" />
            <arg value="--output=${basedir}/out/classes.dex" />
            <arg value="${basedir}/bin/classes" />
            <arg value="${basedir}/libs" />
        </exec>
 
        <echo>packaging resource (include res, assets, AndroidManifest.xml, etc.) to res.zip ... </echo>
        <exec executable="aapt">
            <arg value="package" />
            <arg value="-f" />
            <arg value="-M" />
            <arg value="AndroidManifest.xml" />
            <arg value="-S" />
            <arg value="res" />
            <arg value="-S" />
            <arg value="${basedir}/res" />
            <arg value="-A" />
            <arg value="assets" />
            <arg value="-I" />
            <arg value="${android-jar}" />
            <arg value="-F" />
            <arg value="out/res.zip" />
            <arg value="--auto-add-overlay" />
        </exec>
 
        <echo>building unsigned.apk ... </echo>
        <!-- <exec executable="apkbuilder"> -->
        <exec executable="apkbuilder.bat">
            <arg value="${basedir}/out/unsigned.apk" />
            <arg value="-u" />
            <arg value="-z" />
            <arg value="${basedir}/out/res.zip" />
            <arg value="-f" />
            <arg value="${basedir}/out/classes.dex" />
        </exec>
 
        <echo>signing the unsigned apk to final product apk ... </echo>
        <exec executable="jarsigner">
            <arg value="-keystore" />
            <arg value=".keystore" />
            <arg value="-storepass" />
            <arg value="hello123456" />
            <arg value="-keypass" />
            <arg value="hello123456" />
            <arg value="-signedjar" />
            <arg value="${basedir}/build/${apk-version}/${apk-name}_${apk-version}_${apk-market}.apk" />
            <arg value="${basedir}/out/unsigned.apk" />
            <arg value="zhy" />
        </exec>
 
        <echo>done.</echo>
    </target>
</project>

上面的build.xml檔案在我的機器上測試通過了。注意,我加粗的部分就是要稍作修改的部分,你應該懂的。

要注意的是,在執行之前要用 keytool -genkey -alias yourname 來生成一個金鑰。並將.keystore檔案複製到目錄下,或者指定具體的位置。否則,最後一步不能成功的話,就無法生成在手機上可安裝使用的apk檔案了。

E:\workspace\Hello8>ant
Buildfile: E:\workspace\Hello8\build.xml

init:
     [echo] start initing ...
    [mkdir] Created dir: E:\workspace\Hello8\out
    [mkdir] Created dir: E:\workspace\Hello8\gen
    [mkdir] Created dir: E:\workspace\Hello8\bin\classes
    [mkdir] Created dir: E:\workspace\Hello8\build\latest
     [echo] finish initing.

main:
     [echo] generating R.java for project to dir gen (using aapt) ...
     [echo] generating R.java for library to dir gen (using aapt) ...
     [echo] compiling java files to class files (include R.java, library and the
 third-party jars) ...
    [javac] E:\workspace\Hello8\build.xml:76: warning: 'includeantruntime' was n
ot set, defaulting to build.sysclasspath=last; set to false for repeatable build
s
    [javac] Compiling 2 source files to E:\workspace\Hello8\bin\classes
     [echo] packaging class files (include the third-party jars) to calsses.dex
...
     [echo] packaging resource (include res, assets, AndroidManifest.xml, etc.)
to res.zip ...
     [echo] building unsigned.apk ...
     [exec]
     [exec] THIS TOOL IS DEPRECATED. See --help for more information.
     [exec]
     [echo] signing the unsigned apk to final product apk ...
     [exec]
     [exec] 警告:
     [exec] 簽名者證照將在六個月內過期。
     [echo] done.

BUILD SUCCESSFUL
Total time: 9 seconds

E:\workspace\Hello8>

因為我在用keytool -genkey 的時候沒有指定有效期,預設就是六個月。這在實際使用的時候,肯定是要指定時長的,比如10年20年之類的。

之所以,我能夠這麼快就能成功的使用ant編譯android應用,全都是下面這篇文章的功能(不過我修改了一下build.xml才執行通過):

Android學習系列(31)--App自動化之使用Ant編譯專案多渠道打包

http://www.cnblogs.com/qianxudetianxia/archive/2012/07/04/2573687.html#commentform

在此,對該文的作者表示感謝和敬意!

相關文章