$ java -version
java version "17.0.3.1" 2022-04-22 LTS
Java(TM) SE Runtime Environment (build 17.0.3.1+2-LTS-6)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.3.1+2-LTS-6, mixed mode, sharing)
但是当我运行 Flutter 时:
* What went wrong:
A problem occurred evaluating project ':app'.
> Failed to apply plugin 'com.android.internal.application'.
> Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
You can try some of the following options:
- changing the IDE settings.
- changing the JAVA_HOME environment variable.
- changing `org.gradle.java.home` in `gradle.properties`.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
┌─ Flutter Fix ─────────────────────────────────────────────────────────────────┐
│ [!] You need Java 11 or higher to build your app with this version of Gradle. │
│ │
│ To get Java 11, update to the latest version of Android Studio on │
│ https://developer.android.com/studio/install. │
│ │
│ To check the Java version used by Flutter, run `flutter doctor -v`. │
└───────────────────────────────────────────────────────────────────────────────┘
Exception: Gradle task assembleDebug failed with exit code 1
我可以更改 gradle.properties,例如:
org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
。
它有效,只是这个项目,每次new project
都需要再次更改。我该如何解决它。
环境:
尝试:
您是说每次创建新项目时都会出现此错误吗?或者每次你尝试运行该项目时? 不管怎样,这可能是在使用 JAVA 8 时修复的 IDE 设置,但您的 Gradle 配置为 JAVA 11,从而导致了问题。 您的堆栈跟踪已经告诉您在哪里查看:
org.gradle.java.home
中更改 gradle.properties
。所以请确保所有这些都使用相同的JAVA版本。
如果有人在将 Flutter 更新到 v3.19 时遇到此问题:
这解决了我的问题。
注意:我使用的是 mac m1。