添加Product Flavors后Gradle无法识别本地模块问题排查
问题:多模块Android应用Flavor依赖报错
我有一个包含2个Product Flavors的多模块Android应用,尝试指定仅在其中一个Flavor中引入本地模块。以下是:app模块的build.gradle文件:
plugins { alias libs.plugins.android.application alias libs.plugins.kotlin.android id 'kotlin-kapt' alias libs.plugins.android.hilt } android { namespace 'com.salmakhd.android.fibo' compileSdk 34 defaultConfig { applicationId "com.salmakhd.android.fibo" minSdk 23 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary true } } buildTypes { release { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' signingConfig signingConfigs.debug applicationIdSuffix ".release" } benchmark { initWith buildTypes.release minifyEnabled true signingConfig signingConfigs.debug matchingFallbacks = ['release'] debuggable false } } compileOptions { coreLibraryDesugaringEnabled true // determines available syntax features sourceCompatibility JavaVersion.VERSION_1_8 // determines available JVMs that can run the byte code targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } buildFeatures { buildConfig true compose true } composeOptions { kotlinCompilerExtensionVersion compose_compiler_version } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } flavorDimensions 'user' productFlavors { general { dimension 'user' applicationIdSuffix '.general' versionNameSuffix '-general' minSdk 23 signingConfig signingConfigs.debug proguardFiles 'proguard-rules.pro' } coach { dimension 'user' applicationIdSuffix '.coach' versionNameSuffix '-coach' targetSdk 33 minSdk 23 signingConfig signingConfigs.debug proguardFiles 'proguard-rules.pro' } } } dependencies { coachImplementation project(":core:data") coachImplementation project(":screen:authentication") implementation project(path: ':core:analytics') . . . }
但其中三个依赖均提示“Unrecognized Dependency notation”错误。我遵循了官方文档的结构,使用的是Gradle 8.4,请问问题可能出在哪里?
解决方法
以下是几种可能的原因及对应的修复方案:
混淆Groovy DSL与Kotlin DSL语法
如果你的:app模块构建文件是Kotlin DSL格式(文件名是build.gradle.kts),那么依赖声明必须用括号包裹project()调用,正确写法如下:coachImplementation(project(":core:data")) coachImplementation(project(":screen:authentication")) implementation(project(":core:analytics"))如果是Groovy DSL格式(文件名是
build.gradle),当前写法本身没问题,但要排查是否存在其他语法错误导致解析失败。未定义
compose_compiler_version变量
你的composeOptions块中使用了compose_compiler_version变量,但脚本中没有定义它,这会导致Gradle解析整个构建脚本失败,进而误判依赖声明的语法。可以在build.gradle顶部添加变量定义:def compose_compiler_version = "1.5.10" // 替换为你使用的Compose编译器版本或者在项目根目录的
gradle.properties文件中添加:compose_compiler_version=1.5.10模块路径错误或未在settings.gradle中注册
检查模块路径的拼写、大小写是否正确,同时确认所有依赖的模块都已在项目根目录的settings.gradle中注册,比如:include ':core:data' include ':screen:authentication' include ':core:analytics'
内容的提问来源于stack exchange,提问作者Salma KD
相关产品推荐
相关产品推荐

