You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle 6.9无法解析com.google.guava:guava:33.3.1-android问题求助

问题:Gradle 6.x构建时无法解析Guava 33.3.1-android依赖

环境与报错信息

使用Gradle 6.9(项目无法升级至Gradle 7及以上版本)或6.8.3,执行gradle clean build时出现以下错误:

Task :wallet:preDevDebugBuild FAILED
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':wallet:preDevDebugBuild'.
  Could not resolve all files for configuration ':wallet:devDebugCompileClasspath'.
     Could not resolve com.google.guava:guava:33.3.1-android.
     Required by:
         project :wallet
        The consumer was configured to find an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'flavor' with value 'dev', attribute 'com.android.build.gradle.internal.dependency.AndroidTypeAttr' with value 'Aar'. However we cannot choose between the following variants of com.google.guava:guava:33.3.1-android:
          - androidApiElements
          - jreApiElements

已配置信息

项目级build.gradle仓库配置

buildscript {
    repositories {
//      mavenLocal()
        //maven { url "https://repo1.maven.org/maven2" }
        
        
        mavenCentral()
        google()
    }

}

allprojects {
    repositories {
//      mavenLocal()
//      maven { url "https://repo1.maven.org/maven2" }
        mavenCentral()
        google()
    }
}

子项目依赖配置(精简后)

dependencies {
    implementation 'org.bitcoinj:bitcoinj-core:0.16.4'
    implementation 'com.google.guava:guava:33.3.1-android'
    // https://mvnrepository.com/artifact/com.google.guava/guava
    //implementation 'com.google.guava:guava:33.4.0-android'

    implementation 'com.squareup.moshi:moshi:1.11.0'
    implementation 'org.slf4j:slf4j-api:2.0.9'
    implementation 'com.github.tony19:logback-android:3.0.0'
}

问题原因

这是Gradle版本兼容性问题,而非仓库问题。Guava 33.x的Android版在Maven仓库中同时发布了适配Android和JRE的构件变体,但Gradle 6.x的属性匹配逻辑不够完善,无法根据Android项目的属性(BuildType、flavor、AndroidTypeAttr)自动筛选出正确的Android变体,导致出现二选一的冲突。

解决方案

方案1:强制指定Guava的Android变体

在子项目的build.gradle中,为Guava依赖添加变体筛选规则:

implementation('com.google.guava:guava:33.3.1-android') {
    attributes {
        attribute('com.android.build.api.attributes.BuildTypeAttr', 'debug')
        attribute('com.android.build.gradle.internal.dependency.AndroidTypeAttr', 'Aar')
    }
}

或者更简洁的方式,直接指定构件类型:

implementation('com.google.guava:guava:33.3.1-android') {
    artifact {
        type 'aar'
    }
}

方案2:降级Guava版本

选择Gradle 6.x能正确解析的旧版本Guava,比如30.1.1-android:

implementation 'com.google.guava:guava:30.1.1-android'

方案3:全局配置依赖属性规则

在项目级或子项目的build.gradle中,为所有依赖配置添加属性规则,帮助Gradle自动筛选Android变体:

configurations.all {
    attributes.attribute(
        com.android.build.gradle.internal.dependency.AndroidTypeAttr.ATTRIBUTE,
        objects.named(com.android.build.gradle.internal.dependency.AndroidTypeAttr, 'Aar')
    )
}

验证操作

执行以下命令刷新依赖缓存后重新构建:

gradle clean build --refresh-dependencies

内容的提问来源于stack exchange,提问作者JDOaktown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 04:55:15