很多的應用中需要加上應用推廣的統計,如果一個一個的去產生不同渠道包的應用,效率低不說,還有可能不小心弄錯了分發渠道,使用ant可以批量產生應用。
一、添加渠道包資訊
為了統計渠道資訊,就不得不在程式的某個地方加入渠道的資訊,然後針對不同的渠道打不同的包。一般可以在Manifest檔案中加入渠道編號,而不直接寫在代碼中。這樣做的好處是,可以針對不同渠道,自動化去修改Manifest檔案中的渠道編號,然後自動為該渠道打包。
Manifest檔案支援Meta Data標籤,建議使用這種自訂標籤。例如下面的檔案片段。
<meta-data android:value="000000" android:name="CHANNEL"/>
二、渠道包讀取
public static String getChanel(Context ctx){String CHANNELID="000000";try { ApplicationInfo ai = ctx.getPackageManager().getApplicationInfo( ctx.getPackageName(), PackageManager.GET_META_DATA); Object value = ai.metaData.get(""); if (value != null) { CHANNELID= value.toString(); } } catch (Exception e) { // }return CHANNELID;}
三、自動打包實現
Ant編譯android程式 簡單介紹了使用ant命令打包android程式,實現批量打包需要的加一個類似於for迴圈的功能即可,在Ant的核心包裡沒有相關的For迴圈的Task,要下載相應的擴充包。可以使用開源的Ant-contrib包。:http://ant-contrib.sourceforge.net/ 。下載後的解壓得到的jar檔案放到ant的lib目錄。
在build.xml中增加如下代碼就可以實現批量打包:
taskdef resource="net/sf/antcontrib/antcontrib.properties"> <classpath><pathelement location="lib/ant-contrib-1.0b3.jar"/> </classpath></taskdef> <target name="deploy"> <foreach target="modify_manifest" list="${market_channels}" param="channel" delimiter=","> </foreach> </target><target name="modify_manifest"><replaceregexp flags="g" byline="false"><regexp pattern="android:value="(.*)" android:name="CHANNEL"" /><substitution expression="android:value="${channel}" android:name="CHANNEL"" /><fileset dir="" includes="AndroidManifest.xml" /></replaceregexp><property name="out.release.file" location="${out.absolute.dir}/${ant.project.name}_${channel}_${app_version}.apk" /><antcall target="release" /></target>
taskdef 聲明需要放到較前位置,因為if condition也會用到此聲明。
build.properties檔案增加:
taskdef 聲明需要放到較前位置,因為if condition也會用到此聲明。
build.properties檔案增加:
market_channels=000000,012345 app_version=1.2.1
market名稱用逗號分隔
執行ant deploy即可。
/**
* @author 張興業
* 郵箱:xy-zhang#163.com
* android開發進階群:278401545
*
*/
http://www.cnitblog.com/zouzheng/archive/2011/01/12/72638.html