1.在AndroidManifest.xml中配置渠道占位符
<application>
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}" />
</application>
2.在主项目的build.gradle中配置脚本
- 写法一
android {
productFlavors {
GooglePlay {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "googlePlay"]
}
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
umeng {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
}
}
}
- 写法二
productFlavors {
GooglePlay {}
xiaomi {}
umeng {}
}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
3.修改打包后apk文件名
//获取产品的名字
def getProductName() {
return "heart"
}
//获取当前系统的时间
def releaseTime() {
return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}
android{
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
//这里根据不同的构建类型修改了文件名,避免在debug模式下也生成多个渠道包
if (variant.buildType.name.equals('release')) {
def releaseApkName = "${getProductName()}_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}_release.apk"
output.outputFile = new File(outputFile.parent, releaseApkName)
} else {
def debugApkName = "app-debug-v${defaultConfig.versionName}.apk"
output.outputFile = new File(outputFile.parent, debugApkName)
}
}
}
}