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
1. Update Gradle Version (Recommended)
The most straightforward solution is to update your Gradle version to match your Java version.
Steps:
Check your current Java version:
bashjava -version
Consult the Gradle-Java compatibility matrix to find the appropriate Gradle version.
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
Update
android/build.gradle
dependencies:groovydependencies { classpath 'com.android.tools.build:gradle:8.5.0' }
Compatibility Reference
Java Version | Minimum Gradle Version |
---|---|
8 | 2.0 |
11 | 5.0 |
17 | 7.3 |
21 | 8.5 |
23 | 8.10 |
2. Configure Flutter to Use Specific JDK
If you have multiple JDK versions installed, configure Flutter to use the compatible one:
# 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:
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:
- Delete the Gradle cache directory (typically
~/.gradle/caches
) - 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:
- Open your Android project in Android Studio
- Go to File > Project Structure
- Under SDK Location, set the JDK location
- Alternatively, go to Settings > Build, Execution, Deployment > Build Tools > Gradle
- 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:
org.gradle.java.home=/path/to/your/jdk
Platform-Specific Guidance
Apple Silicon (ARM) Mac Users
Apple Silicon Macs may require additional configuration:
- Install ARM-compatible JDK versions (Azul Zulu or Oracle JDK)
- Ensure you're using native ARM versions of all tools
- Check for Rosetta-related issues if using x86_64 binaries
Windows Users
Windows users should:
- Set
JAVA_HOME
environment variable correctly - Ensure PATH includes the correct Java version
- Check Android Studio's embedded JDK configuration
Verification
After applying fixes, verify your configuration:
# 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:
- Check compatibility before updating Java or Android Studio
- Use project-specific JDK configuration when possible
- Keep Gradle versions updated regularly
- Document the JDK-Gradle combination used in your project
Additional Resources
- Official Gradle-Java Compatibility Guide
- Flutter Android Java-Gradle Migration Guide
- Android Studio Documentation
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.