当项目需要在多渠道上线时,要打很多的渠道包,少则几十个,多种几百个。它们的区别一般只是渠道id或部分配置信息不同,这些信息均可写在配置文件中。

例如常见的渠道id不同,一般定义在AndroidManifest.xml文件中<meta-data android:value="555555" 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("CHANNEL");
if (value != null) {
CHANNELID= value.toString();
}
} catch (Exception e) {
//
} return CHANNELID;
}

如果一个个的去更改渠道号生成不同渠道包,效率低不说,还有可能不小心弄错了分发渠道,而使用ant可以批量生成渠道包,非常便捷。

1. 准备工作,ant、cocos2d-x的环境搭建(详见:http://www.cnblogs.com/songcf/p/4040302.html

2. 下载ant-contrib-1.0b3.jar。

因为ant本身不支持循环打包,所以需要下载一个jar来支持。下载后放在ant/lib目录下即可。

3. 修改build.xml文件

1)在<import file="${sdk.dir}/tools/ant/build.xml" />之前,引入ant-contrib-1.0b3.jar库。

    <!-- 使用第三方的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)循环打包,使用正则匹配替换渠道号。

    <!--循环打包 -->
<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

cocos2d-x使用ant批量打包的更多相关文章

  1. 【Android开发经验】使用Ant批量打包Android应用全然指南

    本文章由Socks完毕.博客地址:http://blog.csdn.net/zhaokaiqiang1992 转载请说明. 折腾了一下午.百度了一下午,最终实现了使用Ant对Android应用的批量打 ...

  2. CygWin模拟Linux环境进行Ant批量打包

    运行环境:Windows7 + Cygwin + ant 第一种:有源码 这种方式比较 简单.利用ant打包.直接shell脚本修改 配置渠道号的文件.我们目前是用的umeng的.在AndroidMa ...

  3. Android Ant批量打包

    一.配置Ant环境变量 JAVA_HOME=/software/jdk1.6.0_24 ANT_HOME=/software/apache-ant-1.9.2 Android_Home=/softwa ...

  4. cocos2dx Android 使用ant 批量打包

    参考文章: 例子:http://www.2cto.com/kf/201305/208139.html http://blog.csdn.net/ljb_blog/article/details/127 ...

  5. Android批量打包提速 - 1分钟900个市场不是梦

    版权声明: 欢迎转载,但请保留文章原始出处 作者:GavinCT 出处:http://www.cnblogs.com/ct2011/p/4152323.html 黎明前的黑暗 使用Ant或者Gradl ...

  6. 在Android开发中使用Ant 三:批量打包

    批量打包最常用到的地方是进行产品推广时,为每个渠道打一个包.上一篇随笔中,介绍了怎样进行一次完整的打包,批量打包只要在此基础上做一次循环即可. 在打包之前要做两个准备工作,一个是读取渠道,一个是修改存 ...

  7. android ant 多渠道批量打包

    注:本文转载于:http://blog.csdn.net/zz7zz7zz/article/details/8915701 前言: 利用ant 可实现多渠道,批量打包. 正文: 思想:通过循环更改An ...

  8. 整理的Unity导出安卓工程利用ANT进行多渠道批量打包APK

    Unity导出的安卓工程利用ant进行多渠道循环批量打包 一:设置JAVA环境变量 做android开发的配置这个是基础. win7 下配置java环境变量,下面是链接 http://www.cnbl ...

  9. Android中利用ant进行多渠道循环批量打包

    公司负责Android开发的小伙伴学习能力稍微偏弱,交代给他的自动化打包的任务,弄了好久依然没有成效.无奈只好亲自出手. 没有想到过程很顺利,我完全按照如下文章的步骤进行: 主要参考: Android ...

随机推荐

  1. nodejs 5.2.0文档自翻译——Path模块

    模块方法概览 Path path.basename(p[, ext]) path.delimiter path.dirname(p) path.extname(p) path.format(pathO ...

  2. Activity生命周期与状态保存

    弹出系统对话框,程序仍部分可见 onPause 对话框消失时 onResume   调用一个新的Activity,老的Activity不可见时 onPause->onStop 从新的Activi ...

  3. Getting Started(Google Cloud Storage Client Library)

    在运行下面的步骤之前,请确保: 1.你的项目已经激活了Google Cloud Storage和App Engine,包括已经创建了至少一个Cloud Storage bucket. 2.你已经下载了 ...

  4. 恒天云单节点部署指南--OpenStack H版本虚拟机单节点部署解决方案

    本帖是openstack单节点在虚拟机上部署的实践.想要玩玩和学习openstack的小伙伴都看过来,尤其是那些部署openstack失败的小伙伴.本帖可以让你先领略一下openstack的魅力.本I ...

  5. [转]python下很帅气的爬虫包 - Beautiful Soup 示例

    原文地址http://blog.csdn.net/watsy/article/details/14161201 先发一下官方文档地址.http://www.crummy.com/software/Be ...

  6. Java访问USB设备

    最近在用Java访问RDing设备,使用的是Java HID API.使用过程中发现一个问题,由于是嵌入式小白,不知道如何向USB设备发送report.于是想到可以看看自带的软件如何访问USB的.找到 ...

  7. Android 不同应用通过SharedPreference实现共享数据

    Android不同应用之间数据的共享有许多方式,但是我觉得还是使用sharedPreference比较简单和轻量级.如果程序B想要访问程序A的sharedPreference可以通过下面的语句来实现: ...

  8. IOS下arm64汇编疑问

    之前所有关于32位下的纯汇编.s代码,在编译arm64的时候,很多错误,不得已只能用C代码. 但是arm_neon.h内部类C接口的汇编,基本上没有问题.不敢完全保证,还有待确认.关于arm64位的汇 ...

  9. homework-附加题:第12章基本数据类型阅读总结

    基本数据类型是构建其他所有数据类型的构造块,本人认为这部分是计算机编程的基础,值得得到大家的注意. 首先,在本章中作者提到了避免使用magic number.使用magic number这种做法是极其 ...

  10. iOS-default.png启动图片

    我在xcode5下写的代码,我下载了iOS6的模拟器,我用iOS6和iOS7的模拟器切换运行,有的时候可以运行有的时候不可以运行,报错: 2013-11-17 16:49:04.049 sim[474 ...