The Quick Fix (TL;DR)
The problem stems from a language gap. Your project is trying to use a Java version (JDK) that your current Gradle version doesn't understand yet. To get back to coding immediately, follow these steps:
- Open Settings (or Settings/Preferences on macOS).
- Head over to Build, Execution, Deployment > Build Tools > Gradle.
- In the Gradle JDK section, pick a lower version like JDK 17 or JDK 11. Alternatively, bump your Gradle version up to match your JDK.
Decoding the Root Cause
Java uses "major version" numbers as internal IDs. When Gradle throws the unsupported class file major version 65 error, it's essentially saying: "I found code compiled for Java 21, but I only know how to handle older versions."
Check this list to see which Java version your error is complaining about:
- Major version 66: Java 22
- Major version 65: Java 21
- Major version 61: Java 17
- Major version 55: Java 11
- Major version 52: Java 8
Mismatches like this usually pop up after you update Android Studio. They also happen when you import a fresh project or manually install a new JDK that Android Studio grabs by mistake.
Method 1: Adjust the JDK in Android Studio Settings
Usually, the easiest fix is to point Android Studio toward a compatible JDK. You don't always need the latest Java version to build an Android app.
- Press Ctrl+Alt+S (Windows/Linux) or Cmd+, (macOS) to open Settings.
- Navigate to Build, Execution, Deployment > Build Tools > Gradle.
- Find the Gradle JDK dropdown.
- If your Gradle version is stuck in the 7.x range, switch the JDK to Embedded JDK or JDK 17.
- Hit Apply and click Try Again on the sync banner.
Method 2: Upgrade Your Gradle Version
Do you actually need Java 21 features? If so, you have to upgrade Gradle. For example, Java 21 (version 65) requires at least Gradle 8.5 to function correctly.
- In your project tree, open
gradle/wrapper/gradle-wrapper.properties. - Locate the
distributionUrlline. - Update the version number to a compatible release.
# Old version (Does not support Java 21)
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
# Updated version (Supports Java 21)
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
Sync the project again. Gradle will automatically download the required files. Your "major version 65" error should vanish.
Method 3: Fixing Terminal and JAVA_HOME
Sometimes the IDE works fine, but running ./gradlew assembleDebug in the terminal fails. This happens because the terminal relies on your system's JAVA_HOME variable, ignoring Android Studio's internal settings.
Run this command to see what your terminal is using:
java -version
If it returns Java 21 but your project requires Java 17, you need to redirect JAVA_HOME. On macOS or Linux, you can swap it temporarily for your current session:
export JAVA_HOME=`/usr/libexec/java_home -v 17`
./gradlew build
On Windows, set the path to your Android Studio JBR folder:
set JAVA_HOME=C:\Program Files\Android\Android Studio\jbr
.\gradlew build
Compatibility Cheat Sheet
Use this map to keep your environment stable. It shows the minimum Gradle version required for each Java release.
Java Version
Required Gradle Version
17
7.3+
18
7.5+
19
7.6+
20
8.3+
21
8.5+
Confirming the Fix
Once you've changed the settings, ensure a clean slate. Go to File > Invalidate Caches... and select Invalidate and Restart. This clears out any lingering metadata from the previous failed builds.
After the restart, click the Elephant icon (Sync Project with Gradle Files). Check the Build tab at the bottom. A green checkmark means you're back in business.
Still seeing errors? Double-check your Module-level build.gradle file. Ensure your sourceCompatibility and targetCompatibility match your chosen JDK.
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
}

