Recently, I faced this annoying issue while building a Flutter project:
FAILURE: Build failed with an exception.
What went wrong:
Could not determine the dependencies of task ':#####:compileDebugKotlin'.
Unknown Kotlin JVM target: 21
No matter what I tried, setting the JDK version for Gradle in Android Studio didn’t work. Running flutter doctor
kept showing that the JDK version was 21.
Here’s what I found out:
- Flutter was using the JDK bundled with Android Studio (
jbr
directory), which is JDK 21. - The problem is that Kotlin’s JVM doesn’t support JDK 21 yet.
The Solution:
I finally came across a fix that worked! You need to explicitly tell Flutter to use a different JDK version. Here’s how to do it:
- Find the path to an older JDK version (like JDK 18) on your system.
- Run this command in your terminal:
flutter config –jdk-dir <dir> Replace with the actual path to your JDK installation. - Run flutter doctor again to confirm that Flutter is now using the correct JDK.
- Restart Android Studio and rebuild your project.
Why This Works:
By default, Flutter uses the JDK bundled with Android Studio. This command forces Flutter to use the JDK version you specify, avoiding the compatibility issue with JDK 21.
Key Tip:
If you’re using Kotlin with Flutter, stick to JDK versions like 18 or 19 until Kotlin adds support for JDK 21.
Hopefully, this helps someone else struggling with the same issue! If you’ve faced a similar problem or have a different solution, let me know! 😊