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

MacosFlutterAndroid Studio

Macos Problem Overview


I have installed Android Studio Canary 2020.3.1.22 and trying to run Flutter project on Apple Silicon(ARM) Mac. Unfortunately, it is giving me this error when I try to run default flutter counter app.

Here is the error I am getting:

Could not open settings generic class cache for settings file '/Users/khamidjonkhamidov/StudioProjects/dummy/android/settings.gradle' (/Users/khamidjonkhamidov/.gradle/caches/6.7/scripts/f0emg6u6oecmxqzgk5g9nn4ui).
> BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 61

Gradle version: 6.7 but I tried 7+ JDK version 17

I would really appreciate your help)

Macos Solutions


Solution 1 - Macos

According to the official grade docs: Java 17 and later versions are not yet supported.

You can check compatibility here.

So I have installed Java11 from Azul.

p.s. don't forget to change jdk version in Android studio

Preferences -> Build -> Build Tools -> Gradle -> Gradle JDK

Solution 2 - Macos

Basically, I installed jdk using brew install java which was not compatible with my current gradle I guess. So

  1. I uninstalled java first using: brew uninstall java
  2. installed JDK 8 or JDK 11 from azul.
  3. Installed gradle: gradle-6.9-all.zip

When done, everything worked smoothly.

Solution 3 - Macos

First, you can execute this command: /usr/libexec/java_home -V, to retrieve all installed jdsk:

[~]$ /usr/libexec/java_home -V

Matching Java Virtual Machines (4):
    17.0 (x86_64) "Oracle Corporation" - "OpenJDK 17.0" /Users/ciccio/Library/Java/JavaVirtualMachines/openjdk-17.0/Contents/Home
    14.0.1 (x86_64) "Oracle Corporation" - "OpenJDK 14.0.1" /Users/ciccio/Library/Java/JavaVirtualMachines/openjdk-14.0.1/Contents/Home
    11.0.12.1 (x86_64) "Amazon.com Inc." - "Amazon Corretto 11" /Users/ciccio/Library/Java/JavaVirtualMachines/corretto-11.0.12/Contents/Home
    10.0.2 (x86_64) "Oracle Corporation" - "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
    1.8.0_302 (x86_64) "Amazon" - "Amazon Corretto 8" /Users/ciccio/Library/Java/JavaVirtualMachines/corretto-1.8.0_302/Contents/Home

Now, imagine you want to remove the version 17:

[~]$ java -version
openjdk version "17" 2021-09-14
OpenJDK Runtime Environment Homebrew (build 17+0)
OpenJDK 64-Bit Server VM Homebrew (build 17+0, mixed mode, sharing)

Go into the path of the version you want to remove (in this case "/Users/ciccio/Library/Java/JavaVirtualMachines/openjdk-17.0/Contents/Home"), and delete entire folder: "/Users/ciccio/Library/Java/JavaVirtualMachines/openjdk-17.0".

Once removed, go back on terminal and use:

[~]$ /usr/libexec/java_home -v 14.0.1 --exec javac -version
javac 14.0.1

to force the new version to use (14.0.1).

Next check if is correct:

[~]$ java -version
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment (build 14.0.1+7)
OpenJDK 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)
[~]$

Your Gradle now is back to work.

Solution 4 - Macos

Got the same error while I upgraded my build.gradle to Java 17. And the fix is as simple as we think:

Gradle starts supporting Java17 only from 7.3 release

Here's the complete reference for Java vs Gradle compatibility: https://docs.gradle.org/current/userguide/compatibility.html

Upgraded my gradle to 7.3 in gradle-wrapper.properties.

https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip

Some of you may experience now the Gradle dependency cache corrupt error after this like me. So better delete the gradle-wrapper.jar and reinstall it using the command:

./gradlew wrapper

Everything will work perfectly fine from here.

In case if you still face Gradle corrupt issue, please check whether you are using latest version of IDE especially Intellij.

Solution 5 - Macos

I found a way to fix this error without messing with the locally installed Java version or Gradle. Here is what I did:

  1. If you are developing a Flutter project and got this error, go to File -> Close Project.
  2. Then re-open from the android folder (omit this if you received from a pure Android project).
  3. Now, Gradle may detect the issue on its own, and it will take a while to check. After it is done, it may provide upgrade steps in an upgrade assistant window at the bottom of the window along with logcat, build, terminal etc… which you need to accept and tell it to execute. Once it finishes, the error is resolved, and you are good to go.
  4. If it does not seem to do anything on its own, then please open the Project Structure tab. Now select the latest Gradle version available that does not contain -rc (you don't want these, most of the time they are not stable releases).
  5. Now select the JDK version that Gradle uses by going to Gradle (sidebar on the right of the window) -> Wrench Icon -> Gradle Settings. Select a compatible JDK version according to the Gradle project's documentation, as found HERE. Current latest stable version of Gradle is 7.4 with maximum supported JDK version 17 (also latest I believe if you use something like openJDK which I use), but Android Studio version at 7.1 so be careful.

Tested on MacBook Air M1 running macOS Monterey 12.2.1.

Solution 6 - Macos

I had this issue when I set the target for a new IntelliJ Kotlin project to be Java 17. My fix was to:

  • set the target to 9 in build.gradle.kt
  • close the project
  • delete the .gradle and .idea directories from the project folder
  • remove the project from the recent projects list
  • open the gradle file from IntelliJ and get it to rebuild everything

Solution 7 - Macos

Open the Gradle settings and change the Gradle JVM to the same JDK version you are using. (I am using 14.0.2) This worked for me.

Solution 8 - Macos

I could fix it by doing this:

$ sudo apt-get install openjdk-8-jdk

$ sudo nano ~/.bashrc 

in the last row i attached:

$ export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

After that it was working but i had to restart Intellij

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKhamidjon KhamidovView Question on Stackoverflow
Solution 1 - MacosneberaaView Answer on Stackoverflow
Solution 2 - MacosKhamidjon KhamidovView Answer on Stackoverflow
Solution 3 - MacoselpView Answer on Stackoverflow
Solution 4 - MacosArunSelvam P MView Answer on Stackoverflow
Solution 5 - MacosNick the Community ScientistView Answer on Stackoverflow
Solution 6 - MacosTim LaversView Answer on Stackoverflow
Solution 7 - MacosS. KafetzopoulosView Answer on Stackoverflow
Solution 8 - MacosKolone13View Answer on Stackoverflow