Ref: http://www.androidengineer.com/2010/06/using-ant-to-automate-building-android.html

 

很多人利用Eclipse GUI 來產生 APK,它幫你做掉了很多事,卻無法達成以下的功能:

  • 新增客製化的 build 步驟
  • 實現 build code 的自動化
  • 更改 build 設定
  • 使用單一指令 build release apk

好佳在,Android ADK 支援 Ant,讓你不用 Eclipse 也能開心開發 App,今天就讓我們來談談這套 Ant 刀法。

 

工欲善其事,利先例其器,練功前請先組裝武器:

 

招式一:認識 Ant script

Ant 預設的 build script 稱為 build.xml,鍵入 ant release 以產生 Android APK,其中 release 為此次 build 的 target,用以產生 release APK;另一個例子為 ant clean,鍵入後會清掉 project 的 binary。

 

招式二:新增 build.xml

要在現有的 project 中加入 Android build script,請進入 project 資料夾,鍵入:android update project --path .,接著便會產生一個 build.xml 在該資料夾中,接著就可以執行 build code 指令,例如:ant debug、ant release...

反之,要直接產生一個新的 project,則改建入:android create project --name YourProjectName --path YourProject --target android-3 --package your.package.name --activity MainActivity,可以先用 android list target 查詢可用的 targets。

 

招式三:自動 sign key

要將 APK 推上Google Play 前必先 sign 一把 real key,使用 Ant 可以簡化 sign 的繁瑣步驟。首先要告訴 build script keystore,先在 project base 目錄產生一個檔案 build.properties,加入以下幾行:

key.store=keystore

key.alias=www.androidengineer.com

key.store.password=password

key.alias.password=password

之後下 ant release 後便會自動產生 sign 好 key 的 APK。

 

招式四:客製化你的 build

Trace build.xml 中有一行:<import file="${sdk.dir}/tools/ant/build.xml" />,如欲客製化自己的 ant build,則複製 Android SDK 中的 build.xml 至 custom_rules.xml,然後隱藏<import file="${sdk.dir}/tools/ant/build.xml" />,接著便可以修改 custom_rules.xml 中的內容,以達成你要的客製化 build。

 

招式五:使用 Java configuration file

本刀法的大絕來了,你可以用 Ant build script 改 Java code!!厲害吧!如何做呢?舉個例子,src/com/yourpackage/Config.java 原本程式碼如下:

public class Config {

    public final static boolean LOGGING = true;

}

這是一段用來 debug 的程式碼,如果你希望在 debug build 設為 true,release build 則設為 false,要怎麼做呢?傷腦筋吧!嘿嘿。

其實根據我們剛剛招式四的客製化,Ant build script 很便可達成,將以下這行加入 build.properties:

config.logging=true

並產生一個 Java template file,複製一份程式碼放到 config/Config.java ,如下:

public class Config {

    public final static boolean LOGGING = @CONFIG.LOGGING@;

}

最後,大絕來了,撰寫 build.xml 一個新的 target 稱為 config,其將 Java template file 取代 source code:

<target name="config" depends="clean">

    <property name="config-target-path" value="${source.dir}/com/androidengineer/antbuild"/>

    <!-- Copy the configuration file, replacing tokens in the file. -->

    <copy file="config/Config.java" todir="${config-target-path}"

            overwrite="true" encoding="utf-8">

        <filterset>

            <filter token="CONFIG.LOGGING" value="${config.logging}"/>

        </filterset>

    </copy>

    <!-- Now set it to read-only, as we don't want people accidentally editing the wrong one. NOTE: This step is unnecessary, but I do it so the developers remember that this is not the original file. -->

    <chmod file="${config-target-path}/Config.java" perm="-w"/>

    <attrib file="${config-target-path}/Config.java" readonly="true"/>

 </target>

然後將 config 加入 compile 的 相依清單中:

compile: <target name="-compile" depends="config, ...">

 

基本劍法告成,接下來就看個人修練了,祝各位擒猿成功!

 

 

arrow
arrow
    全站熱搜

    擒猿小舖 發表在 痞客邦 留言(0) 人氣()