如下build.gradle文件所示,
1.先定义productFlavors 渠道channelA和channelB,
2.然后通过getTaskNames 获取当前编译的渠道,并赋值给变量 channel
3.dependencies 中判断当前 channel 依赖不同的mudule或者jar
--------------------build.gradle文件
def channel = ""
gradle.startParameter.getTaskNames().each { task ->
if (task.toLowerCase().contains("channelA")) {
channel = "channelA"
} else if (task.toLowerCase().contains("channelB")) {
channel = "channelB"
}
}
android {
productFlavors {
channelA {
buildConfigField("String", "PROJECT_NAME", "\"channelA\"")
versionCode rootProject.ext.android["versionCode"]
versionName rootProject.ext.android["versionName"]
}
channelB {
buildConfigField("String", "PROJECT_NAME", "\"channelB\"")
versionCode rootProject.ext.android["versionCode"]
versionName rootProject.ext.android["versionName"]
}
}
}
dependencies {
println("**********channel:" + channel)
if (channel == "channelA") {
//依赖不同的module
} else if (channel == "channelB") {
//依赖不同的module
}
}