多渠道配置
//多渠道打包
productFlavors{
xiaomi{
applicationId "com.chl.example"
versionCode 11
versionName '1.1'
//占位符,它允许我们动态替换我们在AndroidManifest文件里定义的占位符
manifestPlaceholders.put("CHANNEL_VALUE",'xiaomi1')
}
huawei{
applicationId "com.chl.example"
versionCode 12
versionName '1.2'
manifestPlaceholders = [CHANNEL_VALUE:'huawei1']
}
}
定制渠道包名称
//定制生成apk名称
applicationVariants.all { variant ->
if (variant.buildType.name.equals('release')) {
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 获取渠道包versionname
def showversionName = variant.productFlavors[0].versionName
if (showversionName==null){
showversionName = defaultConfig.versionName
}
//渠道包VersionCode
def showVersionCode = variant.productFlavors[0].versionCode
if (showVersionCode == null){
showVersionCode = defaultConfig.versionCode
}
//获取渠道号的名称
def showProductFlavorName = variant.productFlavors[0].name
def fileName = "gradleDemo_v${showversionName}_${showVersionCode}_${releaseTime()}_${showProductFlavorName}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
其中的发布时间定义如下
def releaseTime() {
return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}
占位符说明
设置多个渠道的时候,需要用到Gradle的manifestPlaceholders,
具体比如 manifestPlaceholders.put(“CHANNEL_VALUE”,’huawei1’)
或者manifestPlaceholders = [CHANNEL_VALUE:’huawei1’]
CHANNEL_VALUE是AndroidManifest设置的meta-data的value,
<meta-data android:name="channel_key"
android:value="${CHANNEL_VALUE}"/>
${VALUE}的值会被manifestPlaceholders的值替换。
查看源码可以看到manifestPlaceholders其实是一个Map
public void setManifestPlaceholders(Map<String, Object> manifestPlaceholders) {
this.mManifestPlaceholders.clear();
this.mManifestPlaceholders.putAll(manifestPlaceholders);
}
最终结果:
就是这么简单粗暴方便!!!
完整的app/build.gradle:
apply plugin: 'com.android.application'
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
//默认配置,如果没有其他的配置覆盖,就会使用这里的。
defaultConfig {
applicationId "com.chl.example"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true//是否启动混淆
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//定制生成apk名称
applicationVariants.all { variant ->
if (variant.buildType.name.equals('release')) {
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 获取渠道包versionname
def showversionName = variant.productFlavors[0].versionName
if (showversionName==null){
showversionName = defaultConfig.versionName
}
def showVersionCode = variant.productFlavors[0].versionCode
if (showVersionCode == null){
showVersionCode = defaultConfig.versionCode
}
//获取渠道号的名称
def showProductFlavorName = variant.productFlavors[0].name
def fileName = "gradleDemo_v${showversionName}_${showVersionCode}_${releaseTime()}_${showProductFlavorName}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
//多渠道打包
productFlavors{
xiaomi{
applicationId "com.chl.example"
versionCode 11
versionName '1.1'
//占位符,它允许我们动态替换我们在AndroidManifest文件里定义的占位符
manifestPlaceholders.put("CHANNEL_VALUE",'xiaomi1')
}
huawei{
applicationId "com.chl.example"
versionCode 12
versionName '1.2'
manifestPlaceholders = [CHANNEL_VALUE:'huawei1']
}
}
}
dependencies {
compile fileTree(dir: 'libs',includes: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile project(':TestLibrary')
}