Skip to content

Gradle Build Fails with "Unsupported class file major version 65"

Problem Statement

After updating to Java 17 in a Flutter project, builds fail with the error:
Unsupported class file major version 65
during the assembleDebug process. This typically occurs after updating Android Studio (Ladybug or later) and results in Gradle failing during the semantic analysis phase, even after updating Gradle to supposedly compatible versions (like 7.3.3). The core issue stems from incompatibilities between your Java version, Gradle version, and Android Gradle Plugin (AGP).

Common triggers:

  • Mismatch between Java runtime and Gradle version capabilities
  • Outdated Android Gradle Plugin in build configuration
  • Cached components referencing incorrect Java versions
  • Project Gradle settings not fully updated for Java 17+

1. Configure Flutter's JDK Path (Quick Fix)

If Flutter isn't detecting your Java 17 installation:

bash
flutter config --jdk-dir <path-to-java-sdk-17-home-directory>
flutter clean

Example paths:

  • macOS: /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
  • Windows: C:\Program Files\Java\jdk-17

2. Update Gradle and Android Gradle Plugin (AGP)

Modern Flutter projects require Gradle 8.x+ for Java 17 compatibility. Make these changes:

gradle-wrapper.properties (android/gradle/wrapper/gradle-wrapper.properties):

properties
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip

Update AGP in settings.gradle (android/settings.gradle):

groovy
id "com.android.application" version "8.1.0" apply false
// OR
id "com.android.application" version "8.7.0" apply false

Update build.gradle (android/app/build.gradle):

groovy
compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
}

kotlinOptions {
    jvmTarget = JavaVersion.VERSION_17.toString()
}

3. Clean Gradle Cache

Delete global and project-specific Gradle caches:

  1. Delete global Gradle cache:

    bash
    rm -rf ~/.gradle
  2. Clean project cache:

    bash
    cd android
    ./gradlew clean

4. Update Configuration from New Flutter Project

Create a new Flutter project and compare these files with your current project:

  • settings.gradle
  • build.gradle
  • gradle-wrapper.properties

Key differences often include:

  • Missing NDK version declarations
  • Incorrect compileOptions for Java versions
  • Outdated Kotlin configurations

Configuration for Java 17 Compatibility

After updates, your app/build.gradle should include:

groovy
android {
    ndkVersion = "27.0.12077973"  // Use Android studio's recommended version

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }
}

dependencies {
    // Required for library desugaring (if needed)
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2'
}

WARNING

Ensure these files are also updated:

  1. android/.gitignore - Add **/.cxx to avoid caching issues
  2. gradle.properties - Add org.gradle.java.home=<path-to-jdk17>

Why These Solutions Work

The "major version 65" error indicates your build tools encountered Java 21 bytecode (which uses class version 65) but expected a lower version. Since Java 17 is version 61, this implies your environment has conflicting Java installations. The solutions:

  1. Align all components to Java 17/JDK 17
  2. Use Gradle versions that support JDK 17+:
    • Gradle 7.0+ (minimal support)
    • Gradle 8.0+ (recommended for Flutter)
  3. The AGP must be compatible with Java 17 (v8.1.0+)
  4. Cache clearance removes stale artifacts from previous Java versions

TIP

After migrations, verify Java versions in the terminal:

bash
javac -version
./gradlew -v  # Check Gradle JVM version
flutter doctor -v

Complete configuration updates provide runtime consistency between Gradle, AGP, and Java—preventing bytecode version conflicts.

Persistent Issues?

If the error remains:

  1. Use Android Studio's AGP Upgrade Assistant
  2. Migrate your project to the latest Flutter template
  3. Verify environment variables:
    • JAVA_HOME points to JDK 17
    • PATH contains JDK 17 binaries first

Following these steps resolves the class version mismatch by ensuring all build components operate with JDK 17 consistently.