javaandroidfluttergradle

Flutter Java and Gradle version compatibility issue


I'm trying to create a Flutter app, but I'm getting a message about a version conflict between Java and Gradle. The output I received says

The configured version of Java detected may conflict with the Gradle version in your new Flutter app.

[RECOMMENDED] If so, to keep the default Gradle version 8.3, make sure to download a compatible Java version
(Java 17 <= compatible Java version < Java 21).
You may configure this compatible Java version by running:
flutter config --jdk-dir=<JDK_DIRECTORY>
...
Alternatively, to continue using your configured Java version, update the Gradle version specified in the following file to a compatible Gradle version
(compatible Gradle version range: 8.4 - 8.7):

I am not sure how to fix this. Should I change the Java version or update Gradle? What is the best way to solve this issue to make my Flutter app run without errors?


Solution

  • As a message says, you basically have 2 options. Both are fine, I guess, but preferred way is mostly depends on how you would like to manage your dev environment.

    1. Set JDK for Flutter explicitly with flutter config --jdk-dir=<JDK_DIRECTORY>

    Pros: – Explicit dependencies is usually better, cause you won't have sudden update which brakes everything. – Do not depends on Android Studio (this is where your current JDK is from, I guess) – you can even delete it if you want and can manage your android development independently.

    Cons: – Manually watching for JDK updates, guessing which vendor, patch release you want (OpenJDK latest patch is usually given though :) ).

    So, for this method to work, you would have to install JDK separately and then provide it's path to Flutter.

    1. Update Gradle.

    Pros: – Usually harmless and you'd want to do it anyways cause newer versions is usually more performant and still maintains backward compatibility (inside major version at least)

    Cons: – You might need to update other android-related dependencies as well (such as Android Gradle Plugin, Kotlin plugin etc) for them to be compatible with JDK 21. Might be frustrating and even undoable if you're not very deep into android development.

    If you choose this path, you should modify version manually in gradle-wrapper.properties here: <you_project_path>/android/gradle/wrapper/gradle-wrapper.properties.

    You'll see something like this there:

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

    Just change 8.3 there in last line to, lets say, 8.7 and you're good to go.