Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 311
Khamidjon Khamidov Avatar asked Jul 30 '21 23:07

Khamidjon Khamidov


5 Answers

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

like image 162
neberaa Avatar answered Oct 23 '22 06:10

neberaa


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.

like image 21
Khamidjon Khamidov Avatar answered Oct 23 '22 06:10

Khamidjon Khamidov


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.

like image 14
ArunSelvam P M Avatar answered Oct 23 '22 05:10

ArunSelvam P M


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.

like image 13
elp Avatar answered Oct 23 '22 04:10

elp


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.

like image 4
Nick the Community Scientist Avatar answered Oct 23 '22 05:10

Nick the Community Scientist