多模块Android项目双模块ProductFlavours配置及构建问题咨询
Android多模块Product Flavor兼容配置方案
核心问题原因
不同模块的Product Flavor维度定义不一致,导致Gradle无法自动匹配跨模块的变体依赖,最终构建失败。解决思路是统一所有模块的Flavor维度顺序,并通过missingDimensionStrategy处理模块未定义的维度。
配置步骤与示例
1. 公共模块ModuleA配置(Library)
ModuleA仅维护环境维度(sit/prod/preprod),并通过buildConfigField注入对应环境的BaseURL:
plugins { id 'com.android.library' } android { namespace 'com.example.modulea' compileSdk 34 defaultConfig { minSdk 24 targetSdk 34 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" } // 定义核心维度:环境 flavorDimensions "environment" productFlavors { sit { dimension "environment" buildConfigField "String", "BASE_URL", "\"https://sit.example.com/api/\"" } prod { dimension "environment" buildConfigField "String", "BASE_URL", "\"https://prod.example.com/api/\"" } preprod { dimension "environment" buildConfigField "String", "BASE_URL", "\"https://preprod.example.com/api/\"" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'androidx.core:core-ktx:1.12.0' implementation 'androidx.appcompat:appcompat:1.6.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
2. 主应用模块ModuleB配置(Application)
ModuleB需要同时支持环境维度和主题维度(RED/GREEN等),需与ModuleA保持维度顺序一致,并通过missingDimensionStrategy处理其他模块未定义的主题维度:
plugins { id 'com.android.application' } android { namespace 'com.example.moduleb' compileSdk 34 defaultConfig { applicationId "com.example.moduleb" minSdk 24 targetSdk 34 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } // 统一维度顺序:环境优先,主题次之 flavorDimensions "environment", "theme" // 处理其他模块无主题维度的情况,默认匹配RED主题 missingDimensionStrategy "theme", "RED" productFlavors { // 环境维度Flavor,与ModuleA完全一致,仅用于维度匹配 sit { dimension "environment" } prod { dimension "environment" } preprod { dimension "environment" } // 主题维度Flavor,可添加主题专属配置 RED { dimension "theme" resValue "string", "theme_name", "Red Theme" } GREEN { dimension "theme" resValue "string", "theme_name", "Green Theme" } BLUE { dimension "theme" resValue "string", "theme_name", "Blue Theme" } YELLOW { dimension "theme" resValue "string", "theme_name", "Yellow Theme" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { // Gradle自动根据维度匹配对应变体的ModuleA implementation project(':ModuleA') // 依赖其他模块,同样自动匹配维度 implementation project(':ModuleC') implementation project(':ModuleD') implementation project(':ModuleE') implementation project(':ModuleF') implementation 'androidx.core:core-ktx:1.12.0' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.11.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
3. 其他依赖模块(ModuleC/ModuleD/ModuleE/ModuleF)配置(Library)
这些模块仅依赖ModuleA,只需维护环境维度,并通过missingDimensionStrategy忽略主题维度:
plugins { id 'com.android.library' } android { namespace 'com.example.modulec' compileSdk 34 defaultConfig { minSdk 24 targetSdk 34 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" } flavorDimensions "environment" productFlavors { sit { dimension "environment" } prod { dimension "environment" } preprod { dimension "environment" } } // 忽略主题维度,默认匹配RED missingDimensionStrategy "theme", "RED" buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation project(':ModuleA') implementation 'androidx.core:core-ktx:1.12.0' implementation 'androidx.appcompat:appcompat:1.6.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
构建操作
在Android Studio的Build Variants面板中,为ModuleB选择目标变体(如sitREDDebug、prodGREENRelease),Gradle会自动匹配所有依赖模块对应的环境变体,完成跨模块Flavor的组合构建。
内容的提问来源于stack exchange,提问作者Brahmam M
相关产品推荐
相关产品推荐

