javaandroidfluttergradle

Gradle Build Fails with "Unsupported class file major version 65" After Updating to Java 17


I’m working on a Flutter project and recently updated to Java 17. Since the update, my build fails with the following error when trying to run assembleDebug:

FAILURE: Build failed with an exception.

BUG! exception in phase 'semantic analysis' in source unit 'BuildScript' Unsupported class file major version 65

It seems to be related to Gradle and Java compatibility. I’ve already tried updating the gradle-wrapper.properties to a Gradle version that supports Java 17, but the error persists. Here's what I’ve done so far:

Updated gradle-wrapper.properties to use Gradle version 7.3.3:

distributionUrl=https://services.gradle.org/distributions/gradle-7.3.3-bin.zip Ensured Java 17 is installed and set as the default JAVA_HOME on my system.

Flutter doctor output confirms that Flutter is using Java 17.

Despite these steps, the build continues to fail. Here's the full error output:

BUG! exception in phase 'semantic analysis' in source unit 'BuildScript' Unsupported class file major version 65

Steps I’ve Tried: Updating Gradle to 7.3.3 in gradle-wrapper.properties. Running flutter clean and rebuilding the project. Ensuring JAVA_HOME is pointing to the correct Java 17 installation. Environment: Flutter version: (specific version) Java version: 17 Gradle version: 7.3.3 (set in gradle-wrapper.properties)

This is my build.gradle code

compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
}

kotlinOptions {
    jvmTarget = '17'
}    

P.S. All of this started when i updated Android Studio to the Ladybug version


Solution

  •     // TODO: Remove this when flutter Updates
        // ndkVersion: flutter.ndkVersion
        ndkVersion "25.1.8937393"
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    
        kotlinOptions {
            jvmTarget = '19'
        }