添加com.helpscout:beacon依赖后构建失败,AGP版本适配疑问
在多模块Android项目中,仅向:common:base模块的build.gradle添加依赖com.helpscout:beacon:5.0.1后,构建失败,抛出如下错误:
Execution failed for task ':app:dataBindingMergeDependencyArtifactsStaging'. > Could not resolve all files for configuration ':app:stagingRuntimeClasspath'. > Could not resolve com.squareup.okhttp3:okhttp-bom:4.9.2. Required by: project :app > project :common:base > com.helpscout:beacon:5.0.1 > No matching variant of com.squareup.okhttp3:okhttp-bom:4.9.2 was found. The consumer was configured to find a library for use during runtime, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'staging', attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '8.2.1', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but: - Variant 'apiElements' capability com.squareup.okhttp3:okhttp-bom:4.9.2: - Incompatible because this component declares a platform for use during compile-time and the consumer needed a library for use during runtime - Other compatible attributes: - Doesn't say anything about com.android.build.api.attributes.AgpVersionAttr (required '8.2.1') - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'staging') - Doesn't say anything about its target Java environment (preferred optimized for Android) - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'androidJvm') - Variant 'enforcedApiElements' capability com.squareup.okhttp3:okhttp-bom-derived-enforced-platform:4.9.2: - Incompatible because this component declares an enforced platform for use during compile-time and the consumer needed a library for use during runtime - Other compatible attributes: - Doesn't say anything about com.android.build.api.attributes.AgpVersionAttr (required '8.2.1') - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'staging') - Doesn't say anything about its target Java environment (preferred optimized for Android) - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'androidJvm') - Variant 'enforcedRuntimeElements' capability com.squareup.okhttp3:okhttp-bom-derived-enforced-platform:4.9.2 declares a component for use during runtime: - Incompatible because this component declares an enforced platform and the consumer needed a library - Other compatible attributes: - Doesn't say anything about com.android.build.api.attributes.AgpVersionAttr (required '8.2.1') - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'staging') - Doesn't say anything about its target Java environment (preferred optimized for Android) - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'androidJvm') - Variant 'runtimeElements' capability com.squareup.okhttp3:okhttp-bom:4.9.2 declares a component for use during runtime: - Incompatible because this component declares a platform and the consumer needed a library - Other compatible attributes: - Doesn't say anything about com.android.build.api.attributes.AgpVersionAttr (required '8.2.1') - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'staging') - Doesn't say anything about its target Java environment (preferred optimized for Android) - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'androidJvm')
项目未直接使用OkHttp,也未声明该依赖。项目使用AGP 8.2.0、Gradle 8.2,而Help Scout的beacon依赖使用AGP 8.0.2、Gradle 8.1。将AGP降级至8.1.1后问题解决,但不清楚:
- 为何仅该依赖引发问题?
- AGP版本差异是否为真正原因?
- 问题的本质是什么?
核心矛盾:BOM依赖的解析逻辑收紧
OkHttp的okhttp-bom是一个平台(Platform)依赖,作用是统一管理OkHttp系列库的版本。AGP 8.2.x对依赖解析做了严格调整:它会校验每个依赖的"能力(Capability)",明确区分"平台"和"普通库"。当项目用AGP 8.2.x时,运行时类路径要求所有依赖都是"普通库",但okhttp-bom是平台,不符合要求,因此报错。为什么只有这个依赖出问题?
因为com.helpscout:beacon:5.0.1的依赖配置有误——它把okhttp-bom当成普通库直接引入,而不是用Gradle规定的platform()或enforcedPlatform()方式引入BOM。在AGP 8.1.x及更早版本,这种错误配置不会触发严格校验,所以能正常构建;但AGP 8.2.x收紧了规则,直接暴露了这个问题。AGP版本差异是直接诱因
降级AGP到8.1.1后问题消失,本质是退回到了对依赖能力校验更宽松的版本,暂时绕过了beacon依赖的配置错误。但这不是根本解决办法,真正的问题出在beacon依赖本身的依赖声明上。自定义BuildType的推波助澜
错误日志里提到的staging自定义BuildType也是一个因素:AGP 8.2.x对自定义BuildType下的依赖属性传递校验更严格,而okhttp-bom没有携带BuildTypeAttr这类属性,进一步加剧了不兼容情况。
内容的提问来源于stack exchange,提问作者krdzy

