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.gradle
files:- You will find two
build.gradle
files in your Flutter project:- One at the root level:
<project_root>/android/build.gradle
- Another inside the
app
module:<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.gradle
file, ensure that thesourceCompatibility
andtargetCompatibility
settings are set to the desired Java version.- Open
<project_root>/android/build.gradle
- Look for the
compileOptions
block (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_8
with 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.properties
file.org.gradle.java.home=/path/to/your/jdk
Replace
/path/to/your/jdk
with 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 get
This 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!
댓글
댓글 쓰기