Gradle升级至8.8+后,:app:[flavor]ReleaseOssLicensesTask任务执行失败
Gradle 8.8+升级后bundleRelease时OssLicensesTask变体匹配失败解决方法
升级Gradle至8.8+后,执行./gradlew bundleRelease时,:app:productionReleaseOssLicensesTask任务失败,核心问题是:消费者要求依赖具备com.android.build.api.attributes.ProductFlavor:env=production属性,但:common模块的所有变体均未声明该属性,导致无法完成变体匹配。
问题根源
:app模块定义了env产品风味(包含production取值),但:common模块未同步定义对应的env风味维度及取值。- Gradle 8.8+对变体属性匹配的严格性大幅提升,此前版本允许的“缺失风味属性的依赖兼容”被禁用,触发匹配失败。
- 额外隐患:报错显示AGP版本为8.3.2,与Gradle 8.9存在版本兼容性问题(Gradle 8.8+需搭配AGP 8.4+),会加剧变体匹配异常。
解决方案
方案1:为:common模块同步添加env风味
如果:common模块需要跟随:app的环境区分,在:common模块的构建脚本中添加对应风味配置:
// Groovy DSL android { flavorDimensions += "env" productFlavors { production { dimension "env" // 可按需添加applicationIdSuffix等额外配置 } // 若app还有其他env风味(如staging),需同步添加 } }
// Kotlin DSL android { flavorDimensions += "env" productFlavors { create("production") { dimension = "env" } // 同步app的其他env风味 } }
方案2:让OssLicensesTask忽略env属性匹配
如果:common模块不需要env风味,在:app模块的构建脚本中修改依赖配置,跳过该属性的匹配检查:
// Groovy DSL dependencies { implementation(project(":common")) { attributes { attribute(com.android.build.api.attributes.ProductFlavorAttr.of("env"), null) } } }
// Kotlin DSL dependencies { implementation(project(":common")) { attributes { attribute(ProductFlavorAttr.of("env"), null) } } }
也可以针对OssLicensesTask单独配置变体解析规则:
// Groovy DSL tasks.withType(com.google.android.gms.oss.licenses.OssLicensesTask) { variantResolver { eachVariant { variant -> variant.dependencies.forEach { dep -> if (dep.name == ":common") { dep.attributes { attribute(com.android.build.api.attributes.ProductFlavorAttr.of("env"), null) } } } } } }
方案3:统一AGP与Gradle版本
Gradle 8.8+需要搭配AGP 8.4及以上版本使用,将项目根目录构建脚本中的AGP版本升级至8.4.0或更高:
// 项目根目录build.gradle buildscript { dependencies { classpath 'com.android.tools.build:gradle:8.4.0' } }
内容的提问来源于stack exchange,提问作者Nico
相关产品推荐
相关产品推荐

