How to change java version on gradle of flutter
To change the Java version used by Gradle in a Flutter project, you need to modify the build.gradle files of your Flutter project. Here's how you can do that:
Steps:
Locate the
build.gradlefiles:- You will find two
build.gradlefiles in your Flutter project:- One at the root level:
<project_root>/android/build.gradle - Another inside the
appmodule:<project_root>/android/app/build.gradle
- One at the root level:
- You will find two
Update the Java version in
android/build.gradle: In the rootbuild.gradlefile, ensure that thesourceCompatibilityandtargetCompatibilitysettings are set to the desired Java version.- Open
<project_root>/android/build.gradle - Look for the
compileOptionsblock (or create it if it's not present). - Update the Java version like this:
allprojects { repositories { google() mavenCentral() } } subprojects { afterEvaluate { project -> if (project.hasProperty("android")) { android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 // Change to desired version targetCompatibility JavaVersion.VERSION_1_8 // Change to desired version } } } } }Replace
JavaVersion.VERSION_1_8with your desired version (VERSION_11,VERSION_17, etc.).- Open
Update
android/gradle.properties(if needed): You may also need to specify the Java version in thegradle.propertiesfile.org.gradle.java.home=/path/to/your/jdkReplace
/path/to/your/jdkwith the path to the desired JDK on your system.Sync the project: After making the changes, sync your project by running the following command in the Flutter project root:
flutter clean flutter pub getThis will clean the build files and refresh the project with the new Java settings.
Verify the changes:
To ensure Gradle is using the correct Java version, you can check the Java version by running:
./gradlew -v
This will display the Java version being used by Gradle.
Let me know if you encounter any issues!
댓글
댓글 쓰기