最近遇到了安卓多渠道打包的问题,一个包里面嵌入了多个计费,计费渠道号又往往不能自己定义,都是计费sdk那边配置好发送过来的。如果一个包里面嵌入了多个sdk就会这样了。
<!-- 友盟渠道 -->
<meta-data
android:name="UMENG_CHANNEL"
android:value="channelkey_umeng" />
<!-- 奇葩渠道 -->
<meta-data
android:name="CHID"
android:value="channelkey_qipa" />
<!-- 掌支付渠道 -->
<meta-data
android:name="zhangzhifu_CHANNELID"
android:value="channelkey_zhangzhifu" />
<!-- 应美渠道 -->
<meta-data
android:name="YM_CPID"
android:value="channelkey_yingmei" />
打包的时候每个渠道号对应的值都要修改,如果可以配置到一个表里,直接改表,一键打包就好办多了。研究了一天多,参考了这两篇文章:一、http://www.aiuxian.com/article/p-1675466.html 二、http://www.aiuxian.com/article/p-1675467.html 具体实现方式第二篇文章已经说得很清楚了,我就稍微简单说一下啦;我主要就是修改了local.properties 修改了这个market_channels=UMENG_CHANNEL:a_001,CHID:q_001,zhangzhifu_CHANNELID:z_001,YM_CPID:y_001;UMENG_CHANNEL:a_002,CHID:q_002,zhangzhifu_CHANNELID:z_002,YM_CPID:y_002; 还有就是 custom_rules.xml 了。主要就是正则表达式的修改<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<!-- 渠道包打包脚本 ant deploy -->
<project name="custom_rules" >
<!-- 引用支持循环的jar包ant-contrib-1.0b3.jar -->
<taskdef resource="net/sf/antcontrib/antcontrib.properties" >
<classpath>
<!-- 写绝对路径,不然会出错 -->
<pathelement location="D:/Program Files (x86)/Android/apache-ant-1.9.5-bin/apache-ant-1.9.5/lib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<!-- 循环打包,target名为deploy,构建的时候必须选择deploy -->
<target name="deploy" >
<!-- 进来的时候先clean一下项目 -->
<antcall target="clean" />
<!-- for循环内容传进来的参数为market_channels,以";"为分隔构建list,循环读取list,并将读取到的字符串以参数channel_item传进modify_manifest中 -->
<foreach
delimiter=";"
list="${market_channels}"
param="channel_item"
target="modify_manifest">
</foreach>
</target>
<target name="modify_manifest">
<!-- 友盟渠道 -->
<!-- 按正则匹配获取渠道名字,赋给UMENG_CHANNEL -->
<propertyregex
input="${channel_item}"
override="true"
property="UMENG_CHANNEL"
regexp="(.*):(.*),(.*):(.*),(.*):(.*),(.*):(.*)"
select="\1" />
<!-- 按正则匹配获取渠道号码,赋给channelkey_umeng -->
<propertyregex
input="${channel_item}"
override="true"
property="channelkey_umeng"
regexp="(.*):(.*),(.*):(.*),(.*):(.*),(.*):(.*)"
select="\2" />
<!-- 奇葩渠道 -->
<propertyregex
input="${channel_item}"
override="true"
property="CHID"
regexp="(.*):(.*),(.*):(.*),(.*):(.*),(.*):(.*)"
select="\3" />
<propertyregex
input="${channel_item}"
override="true"
property="channelkey_qipa"
regexp="(.*):(.*),(.*):(.*),(.*):(.*),(.*):(.*)"
select="\4" />
<!-- 掌支付渠道 -->
<propertyregex
input="${channel_item}"
override="true"
property="zhangzhifu_CHANNELID"
regexp="(.*):(.*),(.*):(.*),(.*):(.*),(.*):(.*)"
select="\5" />
<propertyregex
input="${channel_item}"
override="true"
property="channelkey_zhangzhifu"
regexp="(.*):(.*),(.*):(.*),(.*):(.*),(.*):(.*)"
select="\6" />
<!-- 应美渠道 -->
<propertyregex
input="${channel_item}"
override="true"
property="YM_CPID"
regexp="(.*):(.*),(.*):(.*),(.*):(.*),(.*):(.*)"
select="\7" />
<propertyregex
input="${channel_item}"
override="true"
property="channelkey_yingmei"
regexp="(.*):(.*),(.*):(.*),(.*):(.*),(.*):(.*)"
select="\8" />
<!-- 将AndroidManifest.xml中的android:name="channelname"替换成为android:name="${channelname}",这里的${channelname}是我们上面正则匹配出来的内容 -->
<!-- 友盟渠道替换 -->
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:name="UMENG_CHANNEL""
replace="android:name="${UMENG_CHANNEL}""/>
<!-- 将AndroidManifest.xml中的android:value="channelkey"替换成为android:value="${channelkey}" -->
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:value="channelkey_umeng""
replace="android:value="${channelkey_umeng}"" />
<!-- 奇葩渠道替换 -->
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:name="CHID""
replace="android:name="${CHID}""/>
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:value="channelkey_qipa""
replace="android:value="${channelkey_qipa}"" />
<!-- 掌支付渠道替换 -->
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:name="zhangzhifu_CHANNELID""
replace="android:name="${zhangzhifu_CHANNELID}""/>
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:value="channelkey_zhangzhifu""
replace="android:value="${channelkey_zhangzhifu}"" />
<!-- 应美渠道替换 -->
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:name="YM_CPID""
replace="android:name="${YM_CPID}""/>
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:value="channelkey_yingmei""
replace="android:value="${channelkey_yingmei}"" />
<!-- 用release打包 -->
<antcall target="release" />
<!-- 拷贝渠道包到指定目录下,out.absolute.dir在自带的build.xml中定义了,为bin目录,如果要修改,可以在local.properties中添加一行out.absolute.dir=指定目录 -->
<copy
file="${out.absolute.dir}/${ant.project.name}-release.apk"
tofile="${out.absolute.dir}/out/${projectName}_v${version}_${channelkey_umeng}_${time}_未加固.apk" />
<!-- 友盟渠道替换回来 -->
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:name="${UMENG_CHANNEL}""
replace="android:name="UMENG_CHANNEL"" />
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:value="${channelkey_umeng}""
replace="android:value="channelkey_umeng"" />
<!-- 奇葩渠道替换 回来-->
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:name="${CHID}""
replace="android:name="CHID"" />
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:value="${channelkey_qipa}""
replace="android:value="channelkey_qipa"" />
<!-- 掌支付渠道替换 回来-->
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:name="${zhangzhifu_CHANNELID}""
replace="android:name="zhangzhifu_CHANNELID"" />
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:value="${channelkey_zhangzhifu}""
replace="android:value="channelkey_zhangzhifu"" />
<!-- 应美渠道替换 回来-->
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:name="${YM_CPID}""
replace="android:name="YM_CPID"" />
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:value="${channelkey_yingmei}""
replace="android:value="channelkey_yingmei"" />
</target>
</project>
</pre><pre name="code" class="html">然后按照上述第二个链接里的所说的ant打包会看到,生成的相应的渠道包,用APK改之理解开包后会发现渠道号已经都被修改了。
<img src="https://img-blog.csdn.net/20160621180522553?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />