移动开发:Android Ant一键自动打多渠道包
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>
<!-- ################################################################## -->
<!-- ################################################################## -->
<!-- 环境配置参数(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信息 -->
<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信息 -->
<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信息 -->
<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信息 -->
<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" >
<!-- 修改版本Code -->
<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >
<regexp pattern="android:versionCode="(.*)"" />
<substitution expression="android:versionCode="${app.version.code}"" />
<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>
<!-- 修改版本Name -->
<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >
<regexp pattern="android:versionName="(.*)"" />
<substitution expression="android:versionName="${app.version}"" />
<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>
</target>
<!-- 修改AndroidManifest.xml中的Key -->
<target name="modify_key" >
<!-- 修改友盟Key -->
<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >
<regexp pattern="<!-- UM_KEY_S -->([^..]*)<!-- UM_KEY_E -->"/>
<substitution expression="<!-- UM_KEY_S --> <meta-data android:name="UMENG_APPKEY" android:value="${app.umeng.appkey.release}"/> <!-- UM_KEY_E -->" />
<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>
<!-- 修改信鸽KeyId -->
<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >
<regexp pattern="<!-- XG_ID_S -->([^..]*)<!-- XG_ID_E -->"/>
<substitution expression="<!-- XG_ID_S --> <meta-data android:name="XG_V2_ACCESS_ID" android:value="${app.xg.access.id.release}"/> <!-- XG_ID_E -->" />
<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>
<!-- 修改信鸽Key -->
<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >
<regexp pattern="<!-- XG_KEY_S -->([^..]*)<!-- XG_KEY_E -->"/>
<substitution expression="<!-- XG_KEY_S --> <meta-data android:name="XG_V2_ACCESS_KEY" android:value="${app.xg.access.key.release}"/> <!-- XG_KEY_E -->" />
<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>
</target>
<!-- 修改AndroidManifest.xml中的Key -->
<target name="modify_key_test" >
<!-- 修改友盟Key -->
<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >
<regexp pattern="<!-- UM_KEY_S -->([^..]*)<!-- UM_KEY_E -->"/>
<substitution expression="<!-- UM_KEY_S --> <meta-data android:name="UMENG_APPKEY" android:value="${app.umeng.appkey.test}"/> <!-- UM_KEY_E -->" />
<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>
<!-- 修改信鸽KeyId -->
<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >
<regexp pattern="<!-- XG_ID_S -->([^..]*)<!-- XG_ID_E -->"/>
<substitution expression="<!-- XG_ID_S --> <meta-data android:name="XG_V2_ACCESS_ID" android:value="${app.xg.access.id.test}"/> <!-- XG_ID_E -->" />
<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>
<!-- 修改信鸽Key -->
<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >
<regexp pattern="<!-- XG_KEY_S -->([^..]*)<!-- XG_KEY_E -->"/>
<substitution expression="<!-- XG_KEY_S --> <meta-data android:name="XG_V2_ACCESS_KEY" android:value="${app.xg.access.key.test}"/> <!-- XG_KEY_E -->" />
<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>
</target>
<!-- 修改AndroidManifest.xml中的渠道 -->
<target name="modify_manifest" depends="get_channel">
<!-- 修改渠道 -->
<replaceregexp
byline="false"
encoding="utf-8"
flags="g" >
<regexp pattern="<!-- UM_CHL_S -->([^..]*)<!-- UM_CHL_E -->"/>
<substitution expression="<!-- UM_CHL_S --> <meta-data android:name="UMENG_CHANNEL" android:value="${channel}"/> <!-- UM_CHL_E -->" />
<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>
<!-- 5、打包release -->
<antcall target="release" />
<!-- 输出渠道包到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