Android多渠道打包:gradle的配置

xiaoxiao2021-02-28  64

gradle方便的命令

在gradle环境下,Android打包可以很方便的使用命名行进行自动打包流程如:

.gradlew assemble

能自动打出所有包。 但是在实际使用中,我们希望不同的打包在程序内写入不同的信息,比如修改全局的网络请求等。

代码注释讲解

android { signingConfigs { myconfig { // 定义一个签名文件 keyAlias 'xxxxxx' keyPassword 'xxxxxx' storeFile file('xxxxxx') storePassword 'xxxxxx' v2SigningEnabled falseo } } buildTypes { debug { // 其他代码.... signingConfig signingConfigs.myconfig // 使用指定签名 } release { // 其他代码.... signingConfig signingConfigs.myconfig } } productFlavors { // 定义两个不同维度的打包类型渠道:哪些渠道,哪些服务器地址 flavorDimensions 'channel', 'host' // 渠道 hqyx { dimension 'channel' manifestPlaceholders.put('PACKAGE_CHANNEL_VALUE', 'hqyx') // 程序内部标记是哪个渠道 } // 应用宝 z_tengxun { dimension 'channel' manifestPlaceholders.put('PACKAGE_CHANNEL_VALUE', 'tengxun') } z_baidu { dimension 'channel' manifestPlaceholders.put('PACKAGE_CHANNEL_VALUE', 'baidu') } // 测试的服务器地址 ttest { dimension 'host' buildConfigField('String', 'HOST', project.properties['environment.host.test']) // project.properties['environment.host.test'] 能读取gradle.properties文件 内的配置信息 } // 正式环境的服务器地址 online { dimension 'host' buildConfigField('String', 'HOST', project.properties['environment.host.online']) } } // 控制打包时输出的文件名 applicationVariants.all { variant -> variant.outputs.each { output -> if (!variant.buildType.debuggable) { def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { // 计算出新的文件名格式如:Stu_v1.3.0_02051437_develop_debug.apk def fileName = "Stu_${defaultConfig.versionName}_${new Date().format("MMddHHmm")}_${variant.flavorName}.apk" fileName = fileName.replace("_z_", "_") output.outputFile = new File(outputFile.parent, fileName) } } } } // 配置多渠道时,去除没用的Variants,比如线上环境一般不用出现在debug目录下 android.variantFilter { variant -> if (variant.buildType.name.contains('debug')) { variant.getFlavors().each() { flavor -> if (flavor.name.contains('z_')) { variant.setIgnore(true) } } } else { variant.getFlavors().each() { flavor -> if (flavor.name.contains('develop')) { variant.setIgnore(true) } } } } }

查看gradle的配置效果

点击AndroidStudio左下角的Build Variants即可查看所有的Variant 在AndroidStudio下的Terminal输入./gradlew assembleHqyxSimDebug即可输入对应的安装包

转载请注明原文地址: https://www.6miu.com/read-66431.html

最新回复(0)