26

I am currently developing a Flutter application on my Mac and am trying to test my app on an Android emulator. To facilitate this, I have installed Android Studio along with the Android SDK. Running flutter doctor --verbose shows that everything has been set up correctly.(no warings or errors)

However, I am encountering an issue related to Java versions. When I attempt to start my app for Android, I receive an error stating that my Gradle version (Gradle-6.9.3, which I prefer not to change) is incompatible with my JDK. According to the error message, my system needs to use Java 15 to achieve compatibility.

When I run java --version, the output indicates that I am indeed using Java 15. Furthermore, I've set my $JAVA_HOME environment variable to the output of /usr/libexec/java_home -v 15, so it should be referencing the correct Java version.

However, the issue persists. When I execute flutter doctor --verbose, it returns Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694). This suggests that Flutter is using Java 17 instead of Java 15, which is referenced by $JAVA_HOME.

My hypothesis is that either Flutter or Android Studio might be using their own bundled JDK, which seems to override my system settings. I am looking for a way to change this behavior so that Flutter uses the JDK version specified in $JAVA_HOME.

In an attempt to solve this issue, I have installed Java 15 and configured the $JAVA_HOME environment variable accordingly. Additionally, I modified the Flutter command so that it sets the correct $JAVA_HOME value right before executing Flutter.

Despite these adjustments, it seems that Flutter is still ignoring the $JAVA_HOME variable and opting to use the bundled Java version instead. This is problematic because I need to use Java 15 for compatibility with other tools and libraries in my project.

I considered the possibility of completely removing Java 17 from my system as a potential solution. However, when I ran sudo /usr/bin/find / -name "*.jdk" to locate any installed JDKs, I was unable to find JDK 17. Also, running /usr/libexec/java_home -V does not show JDK 17.

I expected Flutter to use the JDK version as specified in the $JAVA_HOME environment variable. Instead, it appears to be using its own bundled version, irrespective of my system settings.

Does anyone have an idea about how to rectify this? Any advice or guidance would be greatly appreciated. Thank you!

3
  • have you tried this? Commented Jul 13, 2023 at 21:41
  • Try Android studio -> File -> Project structure. You can set the project sdk under Project tab. Commented Jul 13, 2023 at 21:59
  • Because flutter for some reason takes the JDK from the latest installation of Android Studio. Only when no AS installation is found, it will use $JAVA_HOME. I learned this the hard way today: github.com/flutter/flutter/issues/… Commented Oct 21, 2024 at 13:11

5 Answers 5

38

If Flutter is not using the JDK from the JAVA_HOME environment variable, you can explicitly set the JDK path for Flutter by using the flutter config command. Follow these steps:

Open your command prompt or terminal. Run the following command to set the JDK path for Flutter:

flutter config --jdk-dir "C:\Program Files\Java\jdk-11"

This command tells Flutter to use the specified JDK directory instead of relying on the JAVA_HOME environment variable.

Make sure to replace "C:\Program Files\Java\jdk-11" with the actual path to your JDK installation if it's different.

Sign up to request clarification or add additional context in comments.

4 Comments

you can find the flutter config at ~/.config/flutter/settings
--jdk-dir is not found on Flutter MacOS.
Whenever you answer, then try to mention the Flutter version as well. Because all the solutions does not work for all flutter versions. For example, for flutter 3.7.12, there is not such command specified in the answer.
I am using jdk17 for my JavaCard project and I had installed it for the same. It works well on JavaCard. But for flutter while doing this it says that JAVA_HOME doesn't point to a valid directory. Is it because this jdk only has the features that JavaCard requires?
28

Run

flutter config --jdk-dir $JAVA_HOME

Then restart your editor to apply the configuration change

Comments

6

Setting the jdk version manually, that gradle uses, in the gradle.properties file by adding the line org.gradle.java.home=<path ot jdk 15> worked for me. I still think it is weird that flutter completely ignores the jdk version specified by $JAVA_HOME.

Thanks for the help :)

1 Comment

Could you tell please, what file in the ~ directory should I change? Cause command: find ~ -name gradle.properties shows me a lot of files in a lot of different directories
-1

The solution that worked for me is installing the jdk version, and setting the flutter jdk configuration manually to where I extracted the JDK files, in my case I Extracted it to C:/ I Opened the Command line and I wrote this command :

flutter config --jdk-dir C:\jdk-23.0.1

Remember to replace the path with yours.

I hope that helps.

Comments

-3

After hours of retry, I finally get it. - MAC version

When downloaded, you must set the path to

flutter config --jdk-dir *JDK-folder-version*/Content/Home

(cause Flutter will search that path/bin/java to determine jdk version)

UPDATE: 19/3/2025

Newest Android Studio and flutter version already fixed that. No need to do this above method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.