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

VS Code中Flutter项目Gradle编译报错:Java Home路径不匹配

问题描述

在VS Code中构建Flutter应用时,build.gradle突然报错:

Could not run phased build action using connection to Gradle distribution 'https://services.gradle.org/distributions/gradle-7.5-all.zip'.
The newly created daemon process has a different context than expected.
It won't be possible to reconnect to this daemon. Context mismatch: 
Java home is different.
Wanted: DefaultDaemonContext[uid=null,javaHome=/usr,daemonRegistryDir=/Users/rijuljain/.gradle/daemon,pid=82555,idleTimeout=null,priority=NORMAL,daemonOpts=--add-opens,java.base/java.util=ALL-UNNAMED,--add-opens,java.base/java.lang=ALL-UNNAMED,--add-opens,java.base/java.lang.invoke=ALL-UNNAMED,--add-opens,java.base/java.util=ALL-UNNAMED,--add-opens,java.prefs/java.util.prefs=ALL-UNNAMED,--add-opens,java.prefs/java.util.prefs=ALL-UNNAMED,--add-opens,java.base/java.nio.charset=ALL-UNNAMED,--add-opens,java.base/java.net=ALL-UNNAMED,--add-opens,java.base/java.util.concurrent.atomic=ALL-UNNAMED,-Xmx1536M,-Dfile.encoding=utf8,-Duser.country=IN,-Duser.language=en,-Duser.variant]
Actual: DefaultDaemonContext[uid=ccff97f5-924e-42fe-8259-9cdc23997e5b,javaHome=/Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home,daemonRegistryDir=/Users/rijuljain/.gradle/daemon,pid=82896,idleTimeout=10800000,priority=NORMAL,daemonOpts=--add-opens,java.base/java.util=ALL-UNNAMED,--add-opens,java.base/java.lang=ALL-UNNAMED,--add-opens,java.base/java.lang.invoke=ALL-UNNAMED,--add-opens,java.base/java.util=ALL-UNNAMED,--add-opens,java.prefs/java.util.prefs=ALL-UNNAMED,--add-opens,java.prefs/java.util.prefs=ALL-UNNAMED,--add-opens,java.base/java.nio.charset=ALL-UNNAMED,--add-opens,java.base/java.net=ALL-UNNAMED,--add-opens,java.base/java.util.concurrent.atomic=ALL-UNNAMED,-Xmx1536M,-Dfile.encoding=utf8,-Duser.country=IN,-Duser.language=en,-Duser.variant]Java(0)

当前Java版本:

java 17.0.2 2022-01-18 LTS

项目build.gradle配置:

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.15'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

.gradle/gradle-wrapper.properties配置:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip

已在环境变量中配置JAVA_HOME:

export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home"
解决方案

1. 终止所有Gradle守护进程

Gradle可能残留了使用旧Java路径的守护进程,执行命令强制终止:

./gradlew --stop

Windows系统使用:

gradlew --stop

2. 确认VS Code读取到正确的JAVA_HOME

  • 关闭所有VS Code窗口,重新打开项目
  • 在VS Code终端执行以下命令验证配置:
echo $JAVA_HOME
java -version

如果输出不符:

  • macOS/Linux:检查~/.bash_profile、~/.zshrc等配置文件,确保export JAVA_HOME命令正确,执行source ~/.zshrc(对应你的shell)刷新配置后重启VS Code
  • Windows:在系统环境变量中设置JAVA_HOME,重启VS Code

3. 在项目中显式指定Java路径

在项目的android/gradle.properties文件中添加配置,强制Gradle使用指定JDK:

org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home

4. 清理缓存并重新构建

执行以下命令清理缓存后重新构建:

flutter clean
cd android && ./gradlew clean
cd .. && flutter pub get
flutter build apk

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 21:50:26