Skip to main content

The Flutter and Java 21 Mystery in Android Studio (Updated)


An android inside a mobile phone looking for butterflies with a magnifying glass in his hand.

I've been struggling for several days with an issue where my Android projects in Flutter simply wouldn’t compile. Initially, I thought something was wrong with a dependency in macOS managed by brew. My friend Carlos also suspected Gradle was to blame, so I decided to spend this weekend digging into it to ensure this problem wouldn’t interfere with my week.

To my surprise, I found that Java 21 was the culprit. The issue is that Java 21 doesn’t support the version of Gradle Flutter uses by default. After a deep conversation with my good friend Félix, we managed to pinpoint the issue and come up with a solution. He suggested updating both the Gradle version in settings.gradle (use Gradle 8.1.4


And modify the distributionUrl property in the gradle-wrapper.properties file (use Gradle 8.3). 


After making those changes, the project did compile—but warnings still appeared.

The root cause is that while Flutter does support Java 21, Java 21 requires a higher Gradle version to work properly. However, Flutter isn’t fully prepared for this newer Gradle version, which is evident from the warnings that are generated even after making the necessary updates. This could mean that Google’s Android Studio team either overlooked this or pushed a Java version that forces your Gradle version into a setup that the Flutter team hasn’t fully prepared for, as indicated by the warnings.

Big thanks to Félix for his insight! Without our thorough discussion, I might still be stuck on this.



Here are some key points to keep in mind:

  • On Android Studio JellyFish 2023.3.1, everything works perfectly.
Android Studio Settings window in Gradle showing gradle versions


  • On Koala Feature Drop 2024.1.2, although it works, this warning appears:
    "Warning: This version only understands SDK XML versions up to 3 but an SDK XML file of version 4 was encountered. This can happen if you use versions of Android Studio and the command-line tools that were released at different times."
Android Studio console window with a gradle warning in Flutter

  • On Laddybug 2024.2.1, Android Studio switched from Java 17 to Java 21, and since Flutter’s default Gradle version isn’t fully compatible with Java 21, you need to update Gradle to make it work. If you don't follow the steps above you will get something like this.
Terminal of Android Studio with a flutter bug with Java version 21

If, like me, you use JetBrains Toolbox to keep your IDE up to date, this version might have already been offered to you, as it went into production on October 14, 2024. So, if you’re developing in Flutter for Android, be careful when updating Android Studio, as the Java and Gradle versions might need adjusting. I hope this saves you some headaches!

Comments

© 2020 Mobile Dev Hub