當項目須要在多渠道上線時,要打不少的渠道包,少則幾十個,多種幾百個。它們的區別通常只是渠道id或部分配置信息不一樣,這些信息都可寫在配置文件中。html
例如常見的渠道id不一樣,通常定義在AndroidManifest.xml文件中<meta-data android:value="555555" android:name="CHANNEL" /> 。node
獲取方式:android
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("CHANNEL"); if (value != null) { CHANNELID= value.toString(); } } catch (Exception e) { // } return CHANNELID; }
若是一個個的去更改渠道號生成不一樣渠道包,效率低不說,還有可能不當心弄錯了分發渠道,而使用ant能夠批量生成渠道包,很是便捷。express
1. 準備工做,ant、cocos2d-x的環境搭建(詳見:http://www.cnblogs.com/songcf/p/4040302.html)apache
2. 下載ant-contrib-1.0b3.jar。app
由於ant自己不支持循環打包,因此須要下載一個jar來支持。下載後放在ant/lib目錄下便可。less
3. 修改build.xml文件ide
1)在<import file="${sdk.dir}/tools/ant/build.xml" />以前,引入ant-contrib-1.0b3.jar庫。post
<!-- 使用第三方的ant包,使ant支持for循環--> <taskdef resource="net/sf/antcontrib/antcontrib.properties"> <classpath> <pathelement location="D:/dev_envir/apache-ant-1.9.3/lib/ant-contrib-1.0b3.jar"/> </classpath> </taskdef> <!-- version-tag: 1 --> <import file="${sdk.dir}/tools/ant/build.xml" />
2)循環打包,使用正則匹配替換渠道號。ui
<!--循環打包 --> <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" value="${out.absolute.dir}/${channel}.apk"/>--> <antcall target="release"/> <copy tofile="${gos.path}/MultAnt_${channel}.apk"> <fileset dir="${out.absolute.dir}/" includes="MultAnt-release.apk" /> </copy> <delete includeEmptyDirs="true"> <fileset dir="${out.absolute.dir}" includes="**/*"/> </delete> <echo message="==========================="/> <echo message="============OK============="/> <echo message="==========================="/> </target>
其中market_channels(渠道列表),gos.path(生成apk存放的目錄),out.absolute.dir(臨時文件目錄)定義在ant.properties文件中。
out.absolute.dir=d:/Proj_vs/MultAnt/proj.android/_temp
gos.path=d:/Proj_vs/MultAnt/proj.android/_apk
market_channels=111111,222222,333333,444444,555555
app_version=1.0
4. 最後進入proj.android目錄運行命令ant deploy等待批量打包完成。
完整build.xml以下:
<?xml version="1.0" encoding="UTF-8"?> <project name="MultAnt" default="help"> <!-- The local.properties file is created and updated by the 'android' tool. It contains the path to the SDK. It should *NOT* be checked into Version Control Systems. --> <property file="local.properties" /> <!-- The ant.properties file can be created by you. It is only edited by the 'android' tool to add properties to it. This is the place to change some Ant specific build properties. Here are some properties you may want to change/update: source.dir The name of the source directory. Default is 'src'. out.dir The name of the output directory. Default is 'bin'. For other overridable properties, look at the beginning of the rules files in the SDK, at tools/ant/build.xml Properties related to the SDK location or the project target should be updated using the 'android' tool with the 'update' action. This file is an integral part of the build system for your application and should be checked into Version Control Systems. --> <property file="ant.properties" /> <!-- if sdk.dir was not set from one of the property file, then get it from the ANDROID_HOME env var. This must be done before we load project.properties since the proguard config can use sdk.dir --> <property environment="env" /> <condition property="sdk.dir" value="${env.ANDROID_HOME}"> <isset property="env.ANDROID_HOME" /> </condition> <!-- The project.properties file is created and updated by the 'android' tool, as well as ADT. This contains project specific properties such as project target, and library dependencies. Lower level build properties are stored in ant.properties (or in .classpath for Eclipse projects). This file is an integral part of the build system for your application and should be checked into Version Control Systems. --> <loadproperties srcFile="project.properties" /> <!-- quick check on sdk.dir --> <fail message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable." unless="sdk.dir" /> <!-- 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 the actual build file. To customize existing targets, there are two options: - Customize only one target: - copy/paste the target into this file, *before* the <import> task. - customize it to your needs. - Customize the whole content of build.xml - copy/paste the content of the rules files (minus the top node) into this file, replacing the <import> task. - customize to your needs. *********************** ****** IMPORTANT ****** *********************** In all cases you must update the value of version-tag below to read 'custom' instead of an integer, in order to avoid having your file be overridden by tools such as "android update project" --> <!-- 使用第三方的ant包,使ant支持for循環--> <taskdef resource="net/sf/antcontrib/antcontrib.properties"> <classpath> <pathelement location="D:/dev_envir/apache-ant-1.9.3/lib/ant-contrib-1.0b3.jar"/> </classpath> </taskdef> <!-- version-tag: 1 --> <import file="${sdk.dir}/tools/ant/build.xml" /> <!--循環打包 --> <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" value="${out.absolute.dir}/${channel}.apk"/>--> <antcall target="release"/> <copy tofile="${gos.path}/MultAnt_${channel}.apk"> <fileset dir="${out.absolute.dir}/" includes="MultAnt-release.apk" /> </copy> <delete includeEmptyDirs="true"> <fileset dir="${out.absolute.dir}" includes="**/*"/> </delete> <echo message="==========================="/> <echo message="============OK============="/> <echo message="==========================="/> </target> </project>
對應的ant.properties
out.absolute.dir=d:/Proj_vs/MultAnt/proj.android/_temp
gos.path=d:/Proj_vs/MultAnt/proj.android/_apk
key.store=D:/Proj_vs/camellia.keystore
key.store.password=xxx
key.alias=camellia
key.alias.password=xxx
market_channels=111111,222222,333333,444444,555555
app_version=1.0