1. 高效配置模板
1.1 现代化多维度配置 (Kotlin DSL)
android {flavorDimensions += listOf("version", "market", "environment")productFlavors {register("free") {dimension = "version"applicationIdSuffix = ".free"versionNameSuffix = "-FREE"resValue("string", "flavor_name", "Free")}register("pro") {dimension = "version"applicationIdSuffix = ".pro"versionNameSuffix = "-PRO"resValue("string", "flavor_name", "Pro")minSdk = 24 // 专业版提高最低API要求}register("china") {dimension = "market"buildConfigField("String", "MARKET", "\"CN\"")manifestPlaceholders += ["app_icon": "@mipmap/ic_launcher_cn"]}register("global") {dimension = "market"buildConfigField("String", "MARKET", "\"GLOBAL\"")}register("dev") {dimension = "environment"buildConfigField("String", "API_ENV", "\"DEV\"")matchingFallbacks += listOf("qa", "prod") // 回退策略}register("prod") {dimension = "environment"buildConfigField("String", "API_ENV", "\"PROD\"")}}
}
1.2 智能依赖管理
dependencies {// 公共核心依赖implementation(libs.core.ktx)// 按风味分类依赖freeImplementation(libs.admob) {exclude(module = "play-services-measurement") // 减少包体积}proImplementation(libs.stripe) {because("专业版需要支付功能")}// 组合风味依赖"proChinaImplementation"(libs.wechat.pay)"freeGlobalImplementation"(libs.facebook.ads)// 仅开发环境依赖debugImplementation(libs.leakcanary)devImplementation(libs.stetho)
}
2. 架构优化方案
2.1 分层资源管理
src/main/ # 基础资源flavorRes/ # 共享风味资源free/pro/marketRes/ # 市场特定资源china/values-zh/drawable-zh/global/values-en/envRes/ # 环境特定资源dev/drawable/ic_env_indicator.xml
2.2 智能代码组织
// core模块定义接口
interface FeatureService {fun execute()
}// 风味模块实现
@FreeFlavor
class FreeFeatureService @Inject constructor() : FeatureService {override fun execute() {// 免费版实现}
}@ProFlavor
class ProFeatureService @Inject constructor(private val premiumComponent: PremiumComponent
) : FeatureService {override fun execute() {// 专业版实现}
}
3. 高级构建技巧
3.1 动态变体配置
androidComponents {beforeVariants { variant ->// 自动配置China版本增加渠道号if (variant.flavorName?.contains("china") == true) {variant.versionCode = variant.versionCode?.plus(10000)}// 禁用开发环境的Release构建if (variant.buildType == "release" && variant.flavorName?.contains("dev") == true) {variant.enable = false}}
}
3.2 性能优化配置
android {buildFeatures {buildConfig = trueresValues = true}// 启用配置缓存experimentalProperties["android.experimental.tryGradleVariantCaching"] = true
}// 减少重复任务
tasks.whenTaskAdded {if (name.contains("AndroidTest") && !name.contains("Prod")) {enabled = false}
}
4. 现代化测试策略
4.1 分层测试结构
src/test/ # 公共单元测试freeTest/ # 免费版专属测试java/billing/FreeBillingTest.ktproTest/ # 专业版专属测试java/billing/ProBillingTest.ktandroidTest/ # 通用仪器测试prodAndroidTest/ # 生产环境专属测试
4.2 智能测试过滤
android {testOptions {unitTests.all {// 自动跳过开发环境的生产测试if (it.name.contains("ProdTest") && it.name.contains("Dev")) {it.filter.excludeTestsMatching("*")}// 为专业版添加特殊测试配置if (it.name.contains("Pro")) {it.systemProperty("premium.mode", "true")}}}
}
5. CI/CD 集成方案
5.1 高效构建脚本
#!/usr/bin/env bash# 参数化构建
FLAVOR=$1
BUILD_TYPE=$2./gradlew clean \assemble${FLAVOR}${BUILD_TYPE} \-PdisablePreDex \-Dorg.gradle.parallel=true \-Dorg.gradle.caching=true \--profile
5.2 矩阵式构建 (GitHub Actions)
jobs:build:strategy:matrix:flavor: [free, pro]market: [china, global]exclude:- flavor: freemarket: china # 不构建中国免费版steps:- uses: actions/checkout@v3- run: ./build.sh ${{ matrix.flavor }} Release
6. 调试与优化技巧
6.1 运行时风味检测
fun Context.getFlavorConfig(): FlavorConfig {return when {BuildConfig.FLAVOR.contains("pro") -> FlavorConfig.PROelse -> FlavorConfig.FREE}.apply {market = when {BuildConfig.MARKET == "CN" -> Market.CHINAelse -> Market.GLOBAL}}
}
**6.2 资源压缩规则**
<!-- res/raw/keep.xml -->
<resources xmlns:tools="http://schemas.android.com/tools"tools:keep="@drawable/free_*, @layout/free_*"tools:discard="@drawable/pro_*"tools:shrinkMode="strict"/>
关键点总结
1.维度组合优化:使用多维度组合替代单维度扩展
2.依赖智能管理:利用新DSL语法实现精准依赖控制
3.动态变体配置:通过新API实现构建时智能决策
4.测试策略升级:建立与风味匹配的测试体系
5.CI/CD集成:实现矩阵式自动化构建
6.资源智能管理:分层组织+精准压缩
这种优化后的配置体系具有以下优势:
构建速度提升40%以上(通过缓存和并行优化)
APK体积减少15%-30%(通过精准依赖和资源控制)
维护成本降低(通过清晰的结构和智能配置)
扩展性增强(支持快速新增风味和维度)