? ? ? ? ??很多的應用中需要加上應用推廣的統計,如果一個一個的去生成不同渠道包的應用,效率低不說,還有可能不小心弄錯了分發渠道,使用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