build插件升级到gradle3.0+ 多渠道打包applicationVariants、compile语法更换

随着AndroidStudio升级到3.0+,自带的build插件也从2.x升级到3.x时代,有升级就会有调整,下面把遇到的语法改变列出:

针对依赖资源库: compile =》 implementation
    dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:recyclerview-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    }
    =》
    dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
   }

----------
针对修改生成的apk文件的名称:
 //修改生成的最终文件名
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                File outputDirectory = new File(outputFile.parent);
                def fileName
                if (variant.buildType.name == "release") {
                    // 输出apk名称
                    fileName = "Xxx_v${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
                } else {
                    fileName = "Xxx_v${defaultConfig.versionName}_${"Test"}_debug.apk"
                }
                output.outputFile = new File(outputDirectory, fileName)
            }
        }
    }
=>
applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                def fileName
                if (variant.buildType.name == "release") {
                    // 输出apk名称为
                    fileName = "Xxx_v${variant.versionName}_${variant.productFlavors[0].name}.apk"
                } else {
                    fileName = "Xxx_v${variant.versionName}_${"Test"}_debug.apk"
                }
                outputFileName = fileName
            }

        }
    }

----------
另外增加了一个flavorDimensions属性(维度的意思)
flavorDimensions "channel"  // 或者 "api", "channel"
给多渠道打包又增加了一个维度,组合翻翻了。
 productFlavors {
        googleplay { dimension "channel" }
        qh360 { dimension "channel" }
        tengxun { dimension "channel" }
}

花式 简洁写法 配上渠道名
productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }

普通写法
 productFlavors {
    googleplay { 
        dimension "channel"
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "googleplay"] }           
    qh360 { 
        dimension "channel"
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qh360"]   }    
   tengxun { 
        dimension "channel"
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "tengxun"] }    
    }

再使用一下其他维度  api
 例:       minApi21 {
            dimension "api"
            minSdkVersion '21'
            versionCode 10000  + android.defaultConfig.versionCode
            versionNameSuffix "-minApi21"
       }

猜你喜欢

转载自blog.csdn.net/u012982629/article/details/81066179