[Ant自動打包] 修改渠道打包

參考:http://www.stay4it.com/?p=284


一:準備工作


1.下載Ant  點擊下載

2.下載 ant-contrib-1.0b3.jar   點擊下載 把下載得到的jar包放到ant下的lib文件夾下


二:開始

1.首先要生成build.xml

    命令窗口導航到工程目錄,執行 android update project --name 工程名稱--target 1 --path ./

  稍微解釋一下
--name 後面的是你的工程的名字

--target 指定項目的Android版本對應的id  如果不知道的話,可以先執行 android list targets來查看

--path  指定項目路徑

  執行完後刷新一下項目,你的項目就會添加了build.xml,local.properties,

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

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

在ant.properties裏對key進行聲明

 key.store=xxx.keystore
key.store.password=xxxx
key.alias.password=xxxx
key.alias=\u53F8\u9A6C\u98DE\u9C7C
proguard.config=proguard-project.txt

第一行的是簽名的路徑


如果沒有可以用命令的方式新建一個工程,從那個工程拷貝過來

android create project -p ndkhello2  -n ndkhello2 -t android-10 -k com.droider.ndkhello2 -a MyActivity

 -n --name    : 工程明朝.
  -t --target  : 工程版本對應的id
  -p --path    :工程位置
  -k --package :包名
  -a --activity: 啓動activity

2.我們打開build.xml看看

有一行:

<import file="custom_rules.xml" optional="true" />

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

<?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>
     -->
      <taskdef resource="net/sf/antcontrib/antlib.xml">
        <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="./syb_${channel}.apk" />
        <antcall target="clean" />
        <antcall target="release" />
    </target>
</project>


3.開始打包

   ant deploy


     

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