30

I am trying to run a flutter app, and this error appeared while trying to, what I did was that I wanted to run flutter apps without android studio, so I download android SDK and JDK 17, after I ran flutter doctor and it gave me that everything is good, I tried to run and it gave me this error:

FAILURE: Build failed with an exception.

* Where:
Build file 'E:\Flutter Apps\first_app\android\build.gradle'

* What went wrong:
Could not compile build file 'E:\Flutter Apps\first_app\android\build.gradle'.
> startup failed:
  General error during semantic analysis: Unsupported class file major version 61
.............

So I tried an older version of JDK, it gave me another error, so I saw a third solution to run with JDK 17, which is to downgrade the gradle in gradle.properties, but it just gave me a fourth error, so does anyone know a final solution for this error ??

DeveloperOmar100
  • 463
  • 1
  • 4
  • 10

7 Answers7

27

Based on the answer of DeveloperOmar 100 I found the following solution:

  1. switch your Java version to a compatible version, in my case to Java version 16.0.2

    To switch it you have to set your PATH. (e.g for Mac, you can use the java_home command).

  2. check your Gradle version and update that one too, so it is compatible (see the Gradle wrapper docs) so in my case I had to change the distributionUrl property in android/gradle/wrapper/gradle-wrapper.properties:

    distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip
    
David Moles
  • 48,006
  • 27
  • 136
  • 235
paddy-p
  • 271
  • 2
  • 4
  • Using the gradle distribution distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip worked for me with Java 16.0.2 as of Dec 2021 – Vilmir Dec 12 '21 at 14:09
  • 5
    Note that as of version 7.3, Gradle supports Java 17, so setting the `distributionUrl` version to the latest 7.3.x (e.g. 7.3.3 at this writing) should take care of the problem. – David Moles Jan 25 '22 at 18:00
  • I found that I had to switch back to a compatible version of Java before I could follow the Gradle upgrade procedure at https://docs.gradle.org/7.4/userguide/upgrading_version_6.html. Also, my GRADLE_HOME environment variable may have been part of my problem. – cowang Mar 04 '22 at 13:27
  • Same error, I'm on MacOs, and JDK is: openjdk 17.0.2 2022-01-18 – thecr0w Oct 11 '22 at 03:21
6

For me, upgrading Gradle fixed the issue. I just had to do change this line in the gradle-wrapper.properties file:

distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
Valentin
  • 5,379
  • 7
  • 36
  • 50
2

SOLUTION

1, Edit gradle path: 6.7 => 7.3

enter image description here

2, Update classpath: 4.1.0 => 7.0.0

enter image description here

hgiahuyy
  • 180
  • 1
  • 5
1

I ran into the same problem when I tried out Kotlin/Compose on my windows computer. I followed the instructions at https://github.com/JetBrains/compose-jb/tree/master/tutorials/Getting_Started

I struggled with this for a few hours before I finally was able to solve all the problems I encountered.

Solution

  • In gradle settings changed Gradle JVM: 11 (version 11.0.12)

  • In build.gradle.kts I deleted

    tasks.withType { kotlinOptions.jvmTarget = "16" }

What caused the problems

I started by installing

  • latest JDK (version 17)
  • IntelliJ IDEA 2021.3.2 (Community Edition)

Then I created a 'hello world' example with following settings:

  • Project template: Compose Desktop Application uses Kotlin 1.5.31

  • Build system: Gradle Kotlin

  • Project JDK: 17 Oracle OpenJDK version 17.0.2

  • Template: Compose Desktop Module

  • Target JVM version: 16

  • Test framework: JUnit 5

When the project was created I updated build.gradle.kts

plugins {
  kotlin("jvm") version "1.6.10"
  id("org.jetbrains.compose") version "1.0.1"
}

First error at build

BUG! exception in phase 'semantic analysis' in source unit 'BuildScript' Unsupported class file major version 61

I fixed this by changing gradle settings

  • Gradle JVM: 11 (version 11.0.12)

Now the build was ok but run would fail

Second error at run

Execution failed for task ':run'. Process 'command 'C:\Program Files\Microsoft\jdk-11.0.12.7-hotspot\bin\java.exe'' finished with non-zero exit value 1

This was solved by deleting

tasks.withType<KotlinCompile> {
  kotlinOptions.jvmTarget = "16"
}

in build.gradle.kts

j.karlsson
  • 618
  • 4
  • 14
1

I had the same issue and after trying all the jdk based solutions, it turned out that my installed Gradle was outdated - 7.0.2 was installed, which doesn't support JDK 17.

You can check your Gradle version with: gradle -version

Check which Gradle you need for a specific version of Java here: https://docs.gradle.org/current/userguide/compatibility.html

After updating it to the latest version the issue went away: https://gradle.org/install/

b-bence
  • 11
  • 2
0

I have java 17 and tried to execute Junit version 4 and this is where I faced this exact error.

However I have downgraded my java version to 11, re-ran the code and viola, this error is gone.

Alternatively I could have upgraded the Junit version to 5 without disturbing the java version 17, choice is yours as per your requirements.

Suresh
  • 1,491
  • 2
  • 22
  • 27
-8

Simply remove java 17 from the system. Thats helped me

Anuar
  • 35
  • 2