Flutter项目配置Gradle 8.3时遇依赖解析错误求助
Flutter项目Gradle依赖解析错误解决方案(Windows 11环境)
问题背景
Windows 11环境下,使用Flutter、VSCode、Android Studio搭建项目时出现Gradle依赖解析错误,核心冲突为Java版本不兼容:当前使用Java 8(1.8.0_411),但Gradle 8.3与Android Gradle Plugin(AGP)8.1.0要求Java 11及以上版本。已尝试安装JDK、配置环境变量、设置GRADLE_HOME、手动重装Gradle等常规操作,问题未解决。
完整报错信息
Could not run phased build action using connection to Gradle distribution 'https://services.gradle.org/distributions/gradle-8.3-all.zip'. org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration 'classpath'. Could not resolve all files for configuration 'classpath'. Could not resolve com.android.tools.build:gradle:8.1.0. Required by: unspecified:unspecified:unspecified > com.android.application:com.android.application.gradle.plugin:8.1.0 No matching variant of com.android.tools.build:gradle:8.1.0 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.3' but: - Variant 'apiElements' capability com.android.tools.build:gradle:8.1.0 declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '8.3') - Variant 'javadocElements' capability com.android.tools.build:gradle:8.1.0 declares a component for use during runtime, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '8.3') - Variant 'runtimeElements' capability com.android.tools.build:gradle:8.1.0 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '8.3') - Variant 'sourcesElements' capability com.android.tools.build:gradle:8.1.0 declares a component for use during runtime, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '8.3')
当前Java版本
java version "1.8.0_411"
有效解决方案
1. 升级Java至11及以上版本(推荐使用Android Studio嵌入式JDK)
方式一:使用Android Studio自带JDK
打开Android Studio,依次进入File > Project Structure > SDK Location,在JDK Location选项中选择Embedded JDK(路径通常为Android Studio安装目录/jbr),点击OK保存。方式二:手动安装OpenJDK 11
下载OpenJDK 11的Windows LTS稳定版,安装后配置环境变量:- 设置
JAVA_HOME为JDK11的安装根目录(如C:\Program Files\OpenJDK\jdk-11.0.22) - 将
%JAVA_HOME%\bin添加到系统PATH变量最前端 - 验证:打开命令提示符输入
java -version,确认输出为openjdk version "11.x.x"
- 设置
2. 配置项目Gradle与AGP版本匹配
- 打开项目根目录下的
android/gradle/wrapper/gradle-wrapper.properties,确认distributionUrl为:distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip - 打开
android/build.gradle,确认dependencies块中的AGP版本为8.1.0(与Gradle 8.3兼容):dependencies { classpath "com.android.tools.build:gradle:8.1.0" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
3. 清理缓存并重新构建项目
- 在项目根目录执行以下操作:
手动删除flutter cleanandroid/.gradle文件夹(Windows系统直接右键删除即可),再执行:flutter pub get flutter run
内容的提问来源于stack exchange,提问作者neonzhan
相关产品推荐
相关产品推荐

