移动开发:Android Ant一键自动打多渠道包(xmltask)
build.xml
===========================================================================
<?xml version="1.0" encoding="UTF-8"?>
<project
name="tzb-ard-application"
default="help" >
<!-- ################################################################## -->
<!-- ################################################################## -->
<!-- 引入ant扩展命令包ant-contrib,使ant支持for循环 -->
<taskdef resource="net/sf/antcontrib/antcontrib.properties" >
<classpath>
<pathelement location="${ant.home}/exlib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<!-- 引入ant扩展命令包xmltask,编辑xml文档 -->
<taskdef
name="xmltask"
classname="com.oopsconsultancy.xmltask.ant.XmlTask" >
<classpath>
<pathelement location="${ant.home}/exlib/xmltask.jar" />
</classpath>
</taskdef>
<!-- ################################################################## -->
<!-- ################################################################## -->
<!-- 环境配置参数(sdk.dir) -->
<property file="local.properties" />
<!-- 项目配置参数(proguard、target、library) -->
<property file="project.properties" />
<!-- 编译配置参数(keystore、channel) -->
<property file="ant.properties" />
<!-- ################################################################## -->
<!-- ################################################################## -->
<!-- 系统中的环境变量配置 -->
<property environment="env" />
<!-- SDK目录 -->
<condition
property="sdk.dir"
value="${env.ANDROID_HOME}" >
<isset property="env.ANDROID_HOME" />
</condition>
<fail message="sdk.dir is missing." unless="sdk.dir" />
<!-- SDK指定平台目录 -->
<property name="sdk.platform.dir" value="${sdk.dir}/platforms/android-23" />
<!-- SDK中tools目录 -->
<property name="sdk.tools" value="${sdk.dir}/tools" />
<!-- SDK指定平台中tools目录 -->
<property name="sdk.platform.tools" value="${sdk.dir}/platform-tools" />
<!-- ################################################################## -->
<!-- ################################################################## -->
<import file="custom_rules.xml" optional="true" />
<import file="${sdk.dir}/tools/ant/build.xml" />
<!-- ################################################################## -->
<!-- ################################################################## -->
<!-- 应用名称 -->
<property
name="app.name"
value="${ant.project.name}" />
<!-- 获取当前时间 -->
<tstamp>
<format
pattern="yyyyMMddHHmm"
property="current_time" />
<format
pattern="yyyyMMdd"
property="current_date" />
<format
pattern="yyMMdd"
property="app.version.code" />
</tstamp>
<!-- 输出日志 -->
<record
name="ant.log"
append="no"
loglevel="verbose" />
<!-- ################################################################## -->
<!-- ################################################################## -->
<!-- 渠道包打包脚本 ant deploy 1、清理编译目录 2、修改接口地址 3、修改版本信息 4、修改Key信息5、修改渠道信息6、打包7、输出 -->
<target name="deploy" depends="clean,modify_service,modify_version,modify_key">
<!-- 5、修改渠道信息 -->
<foreach
delimiter=","
list="${app.market.channels.release}"
param="channel_info"
target="modify_manifest" >
</foreach>
</target>
<!-- 渠道包打包脚本 ant prepare 1、清理编译目录 2、修改接口地址 3、修改版本信息 4、修改Key信息5、修改渠道信息6、打包7、输出 -->
<target name="prepare" depends="clean,modify_service_prepare,modify_version,modify_key_test">
<!-- 5、修改渠道信息 -->
<foreach
delimiter=","
list="${app.market.channels.test}"
param="channel_info"
target="modify_manifest" >
</foreach>
</target>
<!-- 渠道包打包脚本 ant test 1、清理编译目录 2、修改接口地址 3、修改版本信息 4、修改Key信息5、修改渠道信息6、打包7、输出 -->
<target name="test" depends="clean,modify_service_test,modify_version,modify_key_test">
<!-- 5、修改渠道信息 -->
<foreach
delimiter=","
list="${app.market.channels.test}"
param="channel_info"
target="modify_manifest" >
</foreach>
</target>
<!-- 渠道包打包脚本 ant test 1、清理编译目录 2、修改接口地址 3、修改版本信息 4、修改Key信息5、修改渠道信息6、打包7、输出 -->
<target name="debug" depends="clean,modify_service_debug,modify_version,modify_key_test">
<!-- 5、修改渠道信息 -->
<foreach
delimiter=","
list="${app.market.channels.test}"
param="channel_info"
target="modify_manifest" >
</foreach>
</target>
<!-- ################################################################## -->
<!-- ################################################################## -->
<!-- 修改AppConfig.java中的接口地址 -->
<target name="modify_service" >
<!-- 修改接口地址 -->
<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >
<regexp pattern="public static final String COMM_URL_HEADER = "(.*)";" />
<substitution expression="public static final String COMM_URL_HEADER = "${app.service.url.release}";" />
<fileset
dir=""
includes="src\com\tziba\mobile\ard\AppConfig.java" />
</replaceregexp>
</target>
<!-- 修改AppConfig.java中的接口地址-预生产 -->
<target name="modify_service_prepare" >
<!-- 修改接口地址 -->
<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >
<regexp pattern="public static final String COMM_URL_HEADER = "(.*)";" />
<substitution expression="public static final String COMM_URL_HEADER = "${app.service.url.prepare}";" />
<fileset
dir=""
includes="src\com\tziba\mobile\ard\AppConfig.java" />
</replaceregexp>
</target>
<!-- 修改AppConfig.java中的接口地址-测试 -->
<target name="modify_service_test" >
<!-- 修改接口地址 -->
<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >
<regexp pattern="public static final String COMM_URL_HEADER = "(.*)";" />
<substitution expression="public static final String COMM_URL_HEADER = "${app.service.url.test}";" />
<fileset
dir=""
includes="src\com\tziba\mobile\ard\AppConfig.java" />
</replaceregexp>
</target>
<!-- 修改AppConfig.java中的接口地址-调试 -->
<target name="modify_service_debug" >
<!-- 修改接口地址 -->
<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >
<regexp pattern="public static final String COMM_URL_HEADER = "(.*)";" />
<substitution expression="public static final String COMM_URL_HEADER = "${app.service.url.debug}";" />
<fileset
dir=""
includes="src\com\tziba\mobile\ard\AppConfig.java" />
</replaceregexp>
</target>
<!-- 修改AndroidManifest.xml中的版本 -->
<target name="modify_version" >
<xmltask source="AndroidManifest.xml" dest="AndroidManifest.xml" encoding="utf-8" >
<!-- 修改版本Name -->
<attr path="//manifest" attr="android:versionName" value="${app.version}" />
<!-- 修改版本Code -->
<attr path="//manifest" attr="android:versionCode" value="${app.version.code}" />
</xmltask>
</target>
<!-- 修改AndroidManifest.xml中的Key -->
<target name="modify_key" >
<xmltask source="AndroidManifest.xml" dest="AndroidManifest.xml" encoding="utf-8" >
<!-- 修改友盟Key -->
<attr path="//manifest/application/meta-data[@android:name='UMENG_APPKEY']" attr="android:value" value="${app.umeng.appkey.release}" />
<!-- 修改信鸽KeyId -->
<attr path="//manifest/application/meta-data[@android:name='XG_V2_ACCESS_ID']" attr="android:value" value="${app.xg.access.id.release}" />
<!-- 修改信鸽Key -->
<attr path="//manifest/application/meta-data[@android:name='XG_V2_ACCESS_KEY']" attr="android:value" value="${app.xg.access.key.release}" />
</xmltask>
</target>
<!-- 修改AndroidManifest.xml中的Key -->
<target name="modify_key_test" >
<xmltask source="AndroidManifest.xml" dest="AndroidManifest.xml" encoding="utf-8" >
<!-- 修改友盟Key -->
<attr path="//manifest/application/meta-data[@android:name='UMENG_APPKEY']" attr="android:value" value="${app.umeng.appkey.test}" />
<!-- 修改信鸽KeyId -->
<attr path="//manifest/application/meta-data[@android:name='XG_V2_ACCESS_ID']" attr="android:value" value="${app.xg.access.id.test}" />
<!-- 修改信鸽Key -->
<attr path="//manifest/application/meta-data[@android:name='XG_V2_ACCESS_KEY']" attr="android:value" value="${app.xg.access.key.test}" />
</xmltask>
</target>
<!-- 修改AndroidManifest.xml中的渠道 -->
<target name="modify_manifest" depends="get_channel">
<!-- 修改渠道 -->
<xmltask source="AndroidManifest.xml" dest="AndroidManifest.xml" encoding="utf-8" >
<!-- 修改友盟Key -->
<attr path="//manifest/application/meta-data[@android:name='UMENG_CHANNEL']" attr="android:value" value="${channel}" />
</xmltask>
<!-- 6、打包release -->
<antcall target="release" />
<!-- 7、输出渠道包到bin/out目录下 ${out.absolute.dir}/-->
<copy file="bin/${app.name}-release.apk" tofile="apk/${app.apk.name}_${app.version}_${current_date}${channelId}.apk" />
</target>
<!-- 获取渠道 -->
<target name="get_channel" >
<!-- 渠道Id -->
<propertyregex
input="${channel_info}"
property="channelId"
regexp="(.*):"
select="\1" />
<!-- 渠道名称 -->
<propertyregex
input="${channel_info}"
property="channel"
regexp=":(.*)"
select="\1" />
</target>
</project>
==============================================================================
ant.properties
#########################################
#Apk name
app.apk.name=ard_app
#Version
app.version=1.1.4
#service.url.release
app.service.url.release=https://app.tziba.com
#service.url.prepare
app.service.url.prepare=http://192.168.1.1:8803
#service.url.test
app.service.url.test=http://192.168.1.2:8803
#service.url.debug
app.service.url.debug=http://192.168.1.3:8080
#########################################
#app.umeng.appkey.test
app.umeng.appkey.test=11111111111111111111
#app.umeng.appkey.release
app.umeng.appkey.release=22222222222222222222
#app.market.channels.test
app.market.channels.test=C000:testMarket
#app.market.channels.release
app.market.channels.release=C100:officialWebsite,C101:tencentMarket,C102:360Market,C103:baiduMarket,C104:hiapkMarket,C105:91Market,C106:appsMarket,C107:wandoujiaMarket,C108:appchinaMarket,C109:mumayiMarket,C110:nduoaMarket,C111:anzhiMarket,C112:sogouMarket,C113:gfanMarket,C114:xiaomiMarket,C115:flymMarket,C116:jinliMarket,C117:huaweiMarket,C118:oppomobileMarket,C119:vivoMarket,C120:lenovoMarket,C121:suningMarket
#app.xg.access.id.test
app.xg.access.id.test=2100123456
#app.xg.access.key.test
app.xg.access.key.test=AAAAAAAAAAAA
#app.xg.access.id.release
app.xg.access.id.release=2100123456
#app.xg.access.key.release
app.xg.access.key.release=AAAAAAAAAAAA
#########################################
#Apk keystore
key.store=./ard_application.keystore
#Apk keystore password
key.store.password=12345
#Apk keystore alias
key.alias=ard_app
#Apk keystore alias password
key.alias.password=12345