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:

  1. 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
  2. Update the Java version in android/build.gradle: In the root build.gradle file, ensure that the sourceCompatibility and targetCompatibility settings are set to the desired Java version.

    1. Open <project_root>/android/build.gradle
    2. Look for the compileOptions block (or create it if it's not present).
    3. 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.).

  3. Update android/gradle.properties (if needed): You may also need to specify the Java version in the gradle.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.

  4. 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!

댓글

이 블로그의 인기 게시물

Install and run an FTP server using Docker

Using the MinIO API via curl

PYTHONPATH, Python 모듈 환경설정

Elasticsearch Ingest API

오늘의 문장2

How to checkout branch of remote git, 깃 리모트 브랜치 체크아웃

Fundamentals of English Grammar #1

To switch to a specific tag in a Git repository

You can use Sublime Text from the command line by utilizing the subl command

티베트-버마어파 와 한어파(중국어파)의 어순 비교