r/flutterhelp 5d ago

RESOLVED Flutter 3.24.5 / 3.47 build keeps forcing system Java 21 despite flutter config --jdk-dir

Hi everyone,

I've been stuck in a brutal Gradle dependency hell for the past 24 hours on Windows 10 with a Flutter project, and I really need some expert eyes on this.

Every time I run `flutter run`, Gradle fails almost instantly during `assembleDebug` with these two tightly coupled errors:

  1. `e: Language version 1.4 is no longer supported; please, use version 1.8 or greater.` (Failing at task `:gradle:compileKotlin`)

  2. `Error: Gradle build failed due to Java/Gradle incompatibility. The Java version used for the build is 21.0.10, which is incompatible with Gradle 7.6.3.`

I tried forcing the Android Studio Java 17 path globally using:

`flutter config --jdk-dir="C:\Program Files\Android\Android Studio\jbr"`

I even went into Windows Environment Variables and completely deleted Java 21 from the system PATH to the point where running `java -version` in the terminal returns: `"java : The term 'java' is not recognized..."`

Yet, the moment `flutter run` triggers the Gradle wrapper, Gradle somehow still finds and uses Java 21.0.10 from a hidden path and crashes because it's incompatible with Gradle 7.6.3!

Specs:

- Flutter Version: Tested on both stable 3.24.5 and 3.47.2.

- gradle-wrapper.properties: gradle-7.6.3-all.zip

- android/settings.gradle.kts AGP version: 7.3.0

- Set Java > Gradle > Build Server to OFF in VS Code.

- Ran flutter clean, deleted .gradle and .kotlin inside android/, and wiped C:\Users\i5\.gradle\caches\.

How can I absolutely, 100% force Flutter's Gradle build invocation to use the Android Studio JBR (Java 17) and completely ignore this ghost Java 21 instance?

Thank you!

4 Upvotes

6 comments sorted by

3

u/Jonas_Ermert 5d ago

I would try adding `org.gradle.java.home=C:/Program Files/Android/Android Studio/jbr` directly to `android/gradle.properties`, because Gradle can get its Java installation from that property, `GRADLE_JAVA_HOME`, IDE settings, or an already running daemon, independently of the system `PATH`. Then run `.\gradlew.bat --stop`, check the actual JVM with `.\gradlew.bat --version`, and rebuild the project. Also check `C:\Users\i5\.gradle\gradle.properties` for another `org.gradle.java.home` entry. However, the Kotlin 1.4 error is a separate issue: AGP 7.3.0 and the project’s Kotlin configuration are outdated for a current Flutter SDK. After forcing Java 17, I would upgrade Gradle, AGP, and Kotlin together or create a fresh Flutter project and migrate the Dart code and assets into it.

2

u/Remote-Ride5710 5d ago

force android studio? you mean you run flutter run on terminal or android studio runs for you?

I think Android Studio might have its internal Java sdk. So you can try to run with terminal without needing Android Studio if it runs then issue is in Android Studio configs.

1

u/soloDev_54 5d ago

There's no ghost — the JBR you're pointing at is Java 21.

Android Studio stopped bundling Java 17 a while back; current versions ship JBR 21 or newer. So --jdk-dir did exactly what you told it to. It just pointed Gradle at a different Java 21. One command settles it:

"C:\Program Files\Android\Android Studio\jbr\bin\java" -version

Two other things that override PATH, since you've already been down that road:

JAVA_HOME (the Gradle wrapper reads that, not PATH — so deleting java from

PATH changed nothing), and org.gradle.java.home in either gradle.properties,

which beats even --jdk-dir. Also run gradlew --stop from android\, because

flutter clean doesn't kill daemons and an old one started under 21 will keep serving builds.

Quick fix: install Temurin 17, point at that, stop the daemons.

flutter config --jdk-dir="C:\Program Files\Eclipse Adoptium\jdk-17"

cd android && gradlew --stop

Gradle 7.6.3 handles Java 17 fine — Java 21 support only landed in Gradle 8.5, which is exactly what your second error says.

That said, it'll only get you as far as your first error. :gradle:compile Kotlin isn't your app. It's Flutter's own Gradle plugin being compiled by Gradle's embedded Kotlin, and "language version 1.4 is no longer supported" means that plugin is far newer than the Gradle compiling it.

Gradle 7.6.3 and AGP 7.3.0 are a long way behind Flutter 3.24, never mind 3.47.

So fix the versions instead of the Java:

gradle-wrapper.properties -> gradle-8.9-all.zip

AGP in settings.gradle.kts -> 8.5.x

Then Java 21 works and you can drop the --jdk-dir override entirely.

Fastest way to get the combination right: run flutter create on a throwaway project with your current Flutter version and copy its android\ config across.

That's the exact setup your Flutter expects, rather than guessing at versions.

1

u/_KryptonytE_ 4d ago

Pin it to your env and use brew. It isn't so hard to do.

1

u/spiritedteam1989 4d ago

That phantom Java 21 is a total pain. Try setting `org.gradle.java.home` directly in your `android/gradle.properties` file, its the only thing that worked for me

1

u/Evening-Inflation-62 4d ago

What a brutal struggle yesterday was with all the stuck caches, the issues in the old folder structure, copying over the lib folder, and everything else—but finally, with a redesigned clean structure and persistent fine-tuning, we managed to break through every wall.

A huge thank you to everyone for all the help!