Gradle Android 多渠道打包

多渠道配置

 //多渠道打包
    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')
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值