簡單的ant打包,修改渠道號

http://www.cnblogs.com/stay/archive/2013/05/27/3102027.html


昨天在網上看了ant打包,網上寫的亂七八糟,而且build.xml文件那麼多東西,看的真心煩,花了兩小時重新整理了下,改動的地方不大,步驟也簡單,通俗易懂。

言歸正傳:

1.ant支持,要安裝什麼你懂得,不懂自己搜下,

a) 配置android和ant的環境變量

export ANDROID_HOME=/Users/Stay/Desktop/develop/android-sdk-mac_x86/
export PATH=${PATH}:${ANDROID_HOME}/platform-tools
export PATH=${PATH}:${ANDROID_HOME}/tools
export ANT_HOME=/Users/Stay/Desktop/develop/ant/apache-ant-1.8.4
export PATH=${PATH}:${ANT_HOME}/bin

2.我用ant自帶的ant loop,如果不支持的下載個ant-contrib-1.0b3.jar放sdk的tool/lib包下

3.生成一個簡單的build.xml,本身sdk/tool/ant 下有個完整的build.xml,我們只要基於它創建一個簡單的build.xml即可,不需要複製過來,這樣你看的麻煩。

a) android update project -p xxx     (xxx爲項目路徑)

複製代碼
mb-zhst:~ Stay$ android update project -p /Users/Stay/Documents/workspace/waterfall
Updated local.properties
No project name specified, using Activity name 'MainActivity'.
If you wish to change it, edit the first line of build.xml.
Added file /Users/Stay/Documents/workspace/waterfall/build.xml
Updated file /Users/Stay/Documents/workspace/waterfall/proguard-project.txt
It seems that there are sub-projects. If you want to update them
please use the --subprojects parameter.
複製代碼

 現在看你的項目,根目錄下自動生成了build.xml和ant.properties

其實這個時候你就已經可以ant debug或者ant release 進行打包了,只不過還沒改渠道號而已

如果要用ant release,那麼你的key的就要準備好

在ant.properties裏對key進行聲明

key.store=/Users/Stay/Desktop/xxx.keystore
key.store.password=xxx
key.alias=stay
key.alias.password=xxx

b) 打開build.xml看下

複製代碼
<!--
        Import per project custom build rules if present at the root of the project.
        This is the place to put custom intermediary targets such as:
            -pre-build
            -pre-compile
            -post-compile (This is typically used for code obfuscation.
                           Compiled code location: ${out.classes.absolute.dir}
                           If this is not done in place, override ${out.dex.input.absolute.dir})
            -post-package
            -post-build
            -pre-clean
    -->
    <import file="custom_rules.xml" optional="true" />
複製代碼

 裏面import了一個custom_rules.xml,這個在自己project的根目錄是沒有的,如果你想在打包前做些額外的操作,比如對channel的修改,就自己創建一個custom_rules.xml在根目錄,這樣當你編譯的時候會去執行custom_rules裏的操作

下面是我自己的

複製代碼
<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules" >

    <taskdef resource="net/sf/antcontrib/antcontrib.properties" >
        <classpath>
            <pathelement location="lib/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>

    <target name="deploy" >
        <foreach
            delimiter=","
            list="${market_channels}"
            param="channel"
            target="modify_manifest" >
        </foreach>
    </target>

    <target name="modify_manifest" >
       <replaceregexp flags="g" byline="false">  
        <regexp pattern="android:value=&quot;(.*)&quot; android:name=&quot;UMENG_CHANNEL&quot;" />  
            <substitution expression="android:value=&quot;${channel}&quot; android:name=&quot;UMENG_CHANNEL&quot;" />  
           <fileset
                dir=""
                includes="AndroidManifest.xml" />
        </replaceregexp>
        <property
            name="out.final.file"
            location="${apk.dir}/XXX_${channel}.apk" />
        <antcall target="clean" />
        <antcall target="release" />
    </target>
</project>
複製代碼

 下面我來做下解釋

複製代碼
<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules" >
<!--     聲明ant loop ,這裏直接用ant的循環功能,批處理什麼的又要多寫代碼,而且我也不熟 -->
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" >
        <classpath>
            <pathelement location="lib/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>
<!-- 這裏相當於一個方法把,(表示ant不會,只能看懂= =) ,以後可以用命令行 ant deploy 來表示批量打包 -->
<!-- ${market_channels} 要在local.properties裏聲明,並用,來分隔你要打包的channel名 -->
<!-- 比如我的local.properties裏是這樣寫的   market_channels=Google,Gfan,AnZhi,MuMayi -->
    <target name="deploy" >
        <foreach
            delimiter=","
            list="${market_channels}"
            param="channel"
            target="modify_manifest" >
        </foreach>
    </target>
<!-- 修改manifest.xml裏的渠道名,如果你要改其他文件,舉一反三把 -->
<!-- regexp pattern是正則匹配,這裏雙引號要用&quot;而不是\ -->
<!-- substitution expression 是你要替換的的channel名-->
<!-- 打包完畢後要把apk移動到一個指定的目錄把,你可以在sdk/tools/ant/build.xml搜下out.final.file這個property在哪用到的-->
<!-- <property name="out.final.file" location="${apk.dir}/XXX_${channel}.apk" />  ${apk.dir}表示你要指定的apk目錄 XXX表示你要定義apk名和${channel}渠道號-->
<!-- <antcall target="clean" /> <antcall target="release" /> release之前要調下clean,不然以後改的channel名不生效,你懂得-->
    <target name="modify_manifest" >
       <replaceregexp flags="g" byline="false">  
        <regexp pattern="android:value=&quot;(.*)&quot; android:name=&quot;UMENG_CHANNEL&quot;" />  
            <substitution expression="android:value=&quot;${channel}&quot; android:name=&quot;UMENG_CHANNEL&quot;" />  
           <fileset
                dir=""
                includes="AndroidManifest.xml" />
        </replaceregexp>
        <property
            name="out.final.file"
            location="${apk.dir}/XXX_${channel}.apk" />
        <antcall target="clean" />
        <antcall target="release" />
    </target>
</project>
複製代碼
 ok,所有的配置都弄完了,如果還要改其他東西,舉一反三把,額,有問題自己解決把,我不會ant,我只是根據自己的需求上網查的,你如果要問我命令啥的,我真心不會。

最後,打包命令

打開命令行: ant deploy

等把,apks都會出來的

p.s. 如果你有引入其他libary,如果它們沒有build.xml給它們也生成一個,同3.a)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章