Fixing "Could not open settings generic class cache" Gradle Error
Problem Statement
The "Could not open settings generic class cache" error occurs when building Android projects in Flutter, React Native, or standard Android development. This error typically appears with the message:
BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 60
This error indicates a Java version compatibility issue between your installed JDK, Android Studio's bundled Java runtime, and your project's Gradle version.
Root Cause
The error occurs due to version mismatches in three key components:
- Java Development Kit (JDK) installed on your system
- Android Studio's bundled Java runtime (jbr folder in newer versions)
- Gradle version specified in your project
When these components have incompatible versions, Gradle cannot properly compile and cache the project settings, resulting in the class cache error.
Solutions
Method 1: Check and Align Java Versions (Recommended)
First, identify your current Java version:
java -version
Check the compatibility between your Java version and Gradle using this table:
Java Version | Minimum Gradle Version |
---|---|
8 | 2.0+ |
9 | 4.3+ |
10 | 4.7+ |
11 | 5.0+ |
12 | 5.4+ |
13 | 6.0+ |
14 | 6.3+ |
15 | 6.7+ |
16 | 7.0+ |
17 | 7.3+ |
18 | 7.5+ |
19 | 7.6+ |
20 | 8.1+ |
21 | 8.4+ |
WARNING
Android development currently works best with Java 11 or 17. Higher versions may cause compatibility issues with some Android tooling.
Method 2: Configure Android Studio's Java Home
If your system has multiple Java versions, configure Android Studio to use a compatible version:
- Locate your Android Studio installation directory
- Find the
jbr
folder (Java runtime bundled with Android Studio) - Set the
JAVA_HOME
environment variable to point to this directory:
# Windows
JAVA_HOME=C:\Program Files\Android\Android Studio\jbr
# macOS/Linux
JAVA_HOME=/Applications/Android Studio.app/Contents/jbr
Method 3: Specify Java Home in Gradle Properties
Add this line to your android/gradle.properties
file:
org.gradle.java.home=C:/Program Files/Java/jdk-17
Replace the path with your actual JDK installation directory.
Method 4: Update Gradle Version
If your Java version is higher than your Gradle version supports, update your Gradle wrapper:
- Open
android/gradle/wrapper/gradle-wrapper.properties
- Update the distribution URL to a compatible version:
# For Java 17 compatibility
distributionUrl=https://services.gradle.org/distributions/gradle-7.5-all.zip
- Update the Gradle plugin in
android/build.gradle
:
dependencies {
classpath "com.android.tools.build:gradle:7.2.0"
}
Method 5: Sync Project with Gradle Files in Android Studio
Sometimes, simply opening the Android module in Android Studio can resolve the issue:
- Open Android Studio
- Select
File > Open
and navigate to your project'sandroid
folder - Wait for Gradle to sync and download dependencies
- Select
Build > Rebuild Project
- Try running your Flutter/React Native command again
Step-by-Step Troubleshooting Guide
Follow these steps to systematically resolve the issue:
- Check Java version with
java -version
- Verify Android Studio Java runtime by examining the
release
file in thejbr
directory - Ensure Gradle compatibility with your Java version using the compatibility table
- Update environment variables to point to compatible Java versions
- Clean your project with
flutter clean
or./gradlew clean
- Rebuild your project
TIP
For Flutter projects, running flutter doctor
can help identify configuration issues with your Android toolchain.
Prevention
To avoid this issue in the future:
- Use Java 11 or 17 for Android development
- Keep Android Studio and its components updated
- Regularly check Gradle and Java compatibility when updating either
- Consider using JDK version managers for switching between Java versions
Create Java version switching aliases (Linux/macOS)
# Add to your .bashrc or .zshrc
alias changeJavaTo8='sudo update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/bin/java'
alias changeJavaTo11='sudo update-alternatives --set java /usr/lib/jvm/java-11-openjdk-amd64/bin/java'
alias changeJavaTo17='sudo update-alternatives --set java /usr/lib/jvm/java-17-openjdk-amd64/bin/java'
Conclusion
The "Could not open settings generic class cache" error is ultimately a compatibility issue that can be resolved by ensuring your Java version, Android Studio's runtime, and Gradle version are all aligned. The most reliable approach is to use Java 11 or 17 with a compatible Gradle version, as these are well-supported by Android's build tools.
By following the methods outlined above, you should be able to resolve this build error and continue with your Android development workflow.