按照渠道识别型分类:
META-INF渠道识别型:网页版、Python版、BAT脚本、Jar版、
Manifest渠道识别型:Gradle版
Android多渠道打包,有网页版,Gradle版,Python版、BAT脚本、Jar版…
Gradle多渠道和自动化打包(深入篇)
Gradle版
- 支持manifest渠道配置
- 使用简单不易出错
manifest渠道配置
- 原来我的项目是这样添加
android {
productFlavors {
xiaomi {}
360 {}
baidu {}
wandoujia {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [CHANNEL_VALUE: name]
}
}
- 后来是这样添加的
productFlavors { //多渠道打包
wandoujia { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"] }
qihu360 { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qihu360"] }
baidu { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"] }
xiaomi { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"] }
tencent { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "tencent"] }
huawei { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "huawei"] }
meizu { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "meizu"] }
yingyonghui { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "yingyonghui"] }
}
- 如今我是这样写的 在项目根目录添加两个文件:
channels.properties文件:添加渠道,选择默认渠道
#默认渠道
channel.default=paojiao
#全部渠道列表
channel.list=baidu,qihu360,wandoujia,xiaomi
channels.gradle 文件:读入渠道列表,多渠道打包,修改apk名字
apply plugin: 'com.android.application'
// load properties
Properties properties = new Properties()
properties.load(project.rootProject.file('channels.properties').newDataInputStream())
String channelDefault = properties.getProperty('channel.default')
String channleList = properties.getProperty('channel.list')
String[] channelArray = channleList.split(',')
def getProductName() {//获取产品的名字
return "app"
}
def releaseTime() {//获取当前系统的时间
return new Date().format("yyyy-MM-dd-hh-mm", TimeZone.getTimeZone("UTC"))
}
android {
defaultConfig {
multiDexEnabled true // dex突破65535的限制
manifestPlaceholders = [CHANNEL_VALUE: channelDefault]// 默认是umeng的渠道
}
productFlavors {
for (int i = 0 ; i < channelArray.size(); i++) {
def channel = channelArray[i]
"${channel}"{
manifestPlaceholders = [CHANNEL_VALUE: channel]
}
}
}
applicationVariants.all { variant -> //修改生成的apk名字,格式为 app名_版本号_打包时间_渠道名_release.apk
variant.outputs.each { output ->
def oldFile = output.outputFile
if (variant.buildType.name.equals('release')) {
def releaseApkName = getProductName() + "_v${defaultConfig.versionName}_${releaseTime()}_" + variant.productFlavors[0].name + '_release.apk'
output.outputFile = new File(oldFile.parent, releaseApkName)
}
}
}
}
- 在module build.gradle引入channels.gradle配置文件
apply from: "https://raw.githubusercontent.com/MutiChannelPackup/gradle-build/master/channels.gradle"
或者将channels.gradle下载到本地引入
apply from: "../channels.gradle"
- 在manifest中添加
<meta-data android:name="UMENG_CHANNEL" android:value="${CHANNEL_VALUE}" />
<meta-data android:name="JPUSH_CHANNEL" android:value="${CHANNEL_VALUE}" />
...
-
执行打包./gradlew assembleRelease或通过android studio->generate signed apk打包
-
在module目录下可以看到所有的渠道包