多渠道打包:
AndroidManifest.xml文件中设置:
<meta-data
android:name="UMENG_CHANNEL"
android:value="${CHANNEL_VALUE}" />
在app的build中设置:单独配置渠道
flavorDimensions "default"
// 单独配置
productFlavors {
baidu {
manifestPlaceholders = [CHANNEL_VALUE: "baidu"]
buildConfigField "String", "CHANNEL", "\"baidu\""
}
c360 {
manifestPlaceholders = [CHANNEL_VALUE: "c360"]
buildConfigField "String", "CHANNEL", "\"360\""
}
web {
manifestPlaceholders = [CHANNEL_VALUE: "web"]
buildConfigField "String", "CHANNEL", "\"web\""
}
}
也可以统一配置渠道:
//统一配置
productFlavors {
baidu {}
c360 {}
web {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [CHANNEL_VALUE: name]
}
也可以添加自定义包输出路径和包名字:
//自定义APK名称,多渠道打包
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
variant.productFlavors.each { flavor ->
def project = "cpm"
def separator = "_"
def buildType = variant.variantData.variantConfiguration.buildType.name
def versionName = variant.versionName
def versionCode = variant.versionCode
def date = new Date()
def formattedDate = date.format('yyyyMMdd')
def apkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + separator + flavor.name + ".apk"
if (buildType == "release") {
apkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + separator + flavor.name + separator + formattedDate + ".apk"
}
output.outputFileName = apkName
}
}
}
然后刷新左边栏的gradle任务,在:app/Tasks/build下点击assembleRelease就可以生产多个包。如果想生产单个包,可以点击对应的task名字。
demo
网友评论