赞
踩
1、多渠道配置
2、不同渠道不同签名配置
3、不同渠道不同资源文件配置
4、不同渠道不同依赖配置
1、命令行打包
2、IDE 打包
- android {
- compileSdkVersion 29
- buildToolsVersion "29.0.3"
-
- defaultConfig {
- applicationId "com.test.moduledemo"
- minSdkVersion 21
- targetSdkVersion 29
- versionCode 1
- versionName "1.0"
- }
-
- flavorDimensions "versionCode"
-
- productFlavors {
- xiaomi{
- applicationId = “com.test.xiaomi"
- //不同渠道配置不同参数
- buildConfigField "int", "TEST_VALUE", "1"
- buildConfigField "String", "TEST_NAME", "\"xiaomi\""
- }
- huawei{
- applicationId = "com.test.huawei"
- //不同渠道配置不同参数
- buildConfigField "int", "TEST_VALUE", "2"
- buildConfigField "String", "TEST_NAME", "\"huawei\""
- }
- productFlavors.all {//遍历productFlavors多渠道,设置渠道号(xiaomi 、huawei)
- flavor -> flavor.manifestPlaceholders.put("CHANNEL", name)
- }
- }
- applicationVariants.all { variant ->
- // 打包完成后输出路径
- def name = ((project.name != "app") ? project.name : rootProject.name.replace(" ", "")) +
- "_" + variant.flavorName +
- "_" + variant.buildType.name +
- "_" + variant.versionName +
- "_" + new Date().format('yyyyMMddhhmm') + ".apk"
- //相对路径app/build/outputs/apk/huawei/release/
- def path = "../../../../../apk/" //相当于路径 app/apk/
- variant.outputs.each { output ->
- def outputFile = output.outputFile
- if (outputFile != null && outputFile.name.endsWith('.apk')) {
- //指定路径输出
- output.outputFileName = new File(path, name)
- }
- }
- // 在打包完成后还可以做一些别的操作,可以复制到指定目录,或者移动文件到指定目录
- variant.assemble.doLast {
- File out = new File(“${project.rootDir}/apk”)
- variant.outputs.forEach { file ->
- //复制apk到指定文件夹
- //copy {
- // from file.outputFile
- // into out
- //}
- //把文件移动到指定文件夹
- ant.move file: file.outputFile,
- todir: "${project.rootDir}/apk"
- }
- }
- }
- //多渠道签名的配置
- signingConfigs {
- test {
- storeFile file("../test.keystore")
- storePassword 'test'
- keyAlias 'test'
- keyPassword 'test'
- v1SigningEnabled true
- v2SigningEnabled true
- }
- xiaomi {
- storeFile file("../xiaomi.keystore")
- storePassword 'xiaomi'
- keyAlias 'xiaomi'
- keyPassword 'xiaomi'
- v1SigningEnabled true
- v2SigningEnabled true
- }
- huawei {
- storeFile file("../huawei.keystore")
- storePassword 'huawei'
- keyAlias 'huawei'
- keyPassword 'huawei'
- v1SigningEnabled true
- v2SigningEnabled true
- }
- }
- buildTypes {
- debug {
- // debug这里设置不起作用,可能是编译器的问题?
- // productFlavors.xiaomi.signingConfig signingConfigs.test
- // productFlavors.huawei.signingConfig signingConfigs.test
- }
- release {
- productFlavors.xiaomi.signingConfig signingConfigs.xiaomi
- productFlavors.huawei.signingConfig signingConfigs.huawei
- }
- }
- //不同渠道不同资源文件配置
- sourceSets{
- xiaomi.res.srcDirs 'src/main/res-xiaomi'
- huawei.res.srcDirs 'src/main/res-huawei'
- }
- //不同渠道不同的依赖文件
- dependencies {
- xiaomiApi('xxxxxxx')
- huaweiImplementation('xxxxxxxx')
- }
- }

- android {
- flavorDimensions "versionCode"
-
- productFlavors {
- xiaomi{
- applicationId = "com.test.xiaomi"
- //不同渠道配置不同参数
- buildConfigField "int", "TEST_VALUE", "1"
- buildConfigField "String", "TEST_NAME", "\"xiaomi\""
- }
- huawei{
- applicationId = "com.test.huawei"
- //不同渠道配置不同参数
- buildConfigField "int", "TEST_VALUE", "2"
- buildConfigField "String", "TEST_NAME", "\"huawei\""
- }
- productFlavors.all {//遍历productFlavors多渠道,设置渠道号(xiaomi 、huawei)
- flavor -> flavor.manifestPlaceholders.put("CHANNEL", name)
- }
- }
- // ............ 更多配置
- }

在主模块(app)的 build.gradle 下引用该 flavors.gradle 文件即可apply from: rootProject.file('flavors.gradle')
如果项目较为复杂,有可能通过 buildConfigField 设置不同的渠道包,不同的信息字段有可能失效,则把
buildConfigField "int", "TEST_VALUE", "1"
换成
manifestPlaceholders.put("TEST_VALUE", 1)
然后再 AndroidManifest.xml 里添加
- <application>
- <meta-data
- android:name="TEST_VALUE"
- android:value="${TEST_VALUE}" />
- </application>
在 代码通过一下操作获取其值:
- ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(getPackageName(),
- PackageManager.GET_META_DATA);
- int testValue = applicationInfo.metaData.getInt("TEST_VALUE");
Windows下: gradlew assembleRelease
Mac 下:./gradlew assembleRelease
assembleRelease 是打所有渠道的 Release 包
assembleDebug 是打所有渠道的 Debug 包
还可以打指定渠道的包:
gradlew assembleXiaoMiRelease assembleHuaWeiRelease
(空格隔开要打的渠道包的任务名称即可,任务名称可以通过点击 android studio 右边的 Gradle 根据图中目录查看)
多渠道打包
image
image
image
image
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。