Skip to content

Unsupported Class File Major Version 61 Error in Gradle

Problem

The "Unsupported class file major version 61" error occurs when there's a compatibility mismatch between your Java Development Kit (JDK) version and Gradle version. This error typically appears after updating Android Studio, Java, or when working on Apple Silicon (ARM) Macs.

The error message appears as:

BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 61

This indicates that Gradle cannot process class files created by a newer Java version than it supports.

Root Cause

Java class files contain a "major version" number that indicates which Java version created them. Gradle versions have specific compatibility requirements with Java versions:

  • Java 17 produces class files with major version 61
  • Java 18 produces version 62
  • Java 19 produces version 63
  • Java 20 produces version 64
  • Java 21 produces version 65

If your Gradle version doesn't support the Java version you're using, this compatibility error occurs.

Solutions

The most straightforward solution is to update your Gradle version to match your Java version.

Steps:

  1. Check your current Java version:

    bash
    java -version
  2. Consult the Gradle-Java compatibility matrix to find the appropriate Gradle version.

  3. Update android/gradle/wrapper/gradle-wrapper.properties:

    properties
    # Change distributionUrl to match compatible Gradle version
    distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
  4. Update android/build.gradle dependencies:

    groovy
    dependencies {
        classpath 'com.android.tools.build:gradle:8.5.0'
    }

Compatibility Reference

Java VersionMinimum Gradle Version
82.0
115.0
177.3
218.5
238.10

2. Configure Flutter to Use Specific JDK

If you have multiple JDK versions installed, configure Flutter to use the compatible one:

bash
# Set Flutter to use a specific JDK directory
flutter config --jdk-dir '/path/to/your/jdk'

# Example:
flutter config --jdk-dir '/Library/Java/JavaVirtualMachines/jdk-17.0.12/Contents/Home'

Verify the change took effect:

bash
flutter doctor -v

Look for the "Java binary at:" line in the Android toolchain section to confirm the correct JDK is being used.

3. Clean Gradle Cache

Sometimes the Gradle cache becomes corrupted, especially after downgrading Android Studio:

  1. Delete the Gradle cache directory (typically ~/.gradle/caches)
  2. Alternatively, through Android Studio:
    • Go to Settings > Build, Execution, Deployment > Build Tools > Gradle
    • Find the "Gradle user home" directory and delete it
    • Restart Android Studio

4. Configure JDK in Android Studio

Set the appropriate JDK in Android Studio:

  1. Open your Android project in Android Studio
  2. Go to File > Project Structure
  3. Under SDK Location, set the JDK location
  4. Alternatively, go to Settings > Build, Execution, Deployment > Build Tools > Gradle
  5. Set the Gradle JDK to a compatible version

5. Set JDK Version in gradle.properties

For project-specific JDK configuration, add this to your gradle.properties file:

properties
org.gradle.java.home=/path/to/your/jdk

Platform-Specific Guidance

Apple Silicon (ARM) Mac Users

Apple Silicon Macs may require additional configuration:

  1. Install ARM-compatible JDK versions (Azul Zulu or Oracle JDK)
  2. Ensure you're using native ARM versions of all tools
  3. Check for Rosetta-related issues if using x86_64 binaries

Windows Users

Windows users should:

  1. Set JAVA_HOME environment variable correctly
  2. Ensure PATH includes the correct Java version
  3. Check Android Studio's embedded JDK configuration

Verification

After applying fixes, verify your configuration:

bash
# Check Java version
java -version

# Check Gradle version
./gradlew --version

# Check Flutter configuration
flutter doctor -v

All should report compatible versions according to the Gradle-Java compatibility matrix.

Prevention

To avoid future compatibility issues:

  1. Check compatibility before updating Java or Android Studio
  2. Use project-specific JDK configuration when possible
  3. Keep Gradle versions updated regularly
  4. Document the JDK-Gradle combination used in your project

Additional Resources

WARNING

Always backup your project before making significant changes to build configurations. Test changes in a separate branch before applying them to your main development branch.