113

I've just created a brand new material 3 compose project and this pops up:

> No matching variant of com.android.tools.build:gradle:7.4.0 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.5' but:
          - Variant 'apiElements' capability com.android.tools.build:gradle:7.4.0 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
          - Variant 'javadocElements' capability com.android.tools.build:gradle:7.4.0 declares a runtime of a component, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
          - Variant 'runtimeElements' capability com.android.tools.build:gradle:7.4.0 declares a runtime of a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
          - Variant 'sourcesElements' capability com.android.tools.build:gradle:7.4.0 declares a runtime of a component, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')

Didn't change anything, everything is the default.

Juncu
  • 3,704
  • 4
  • 7
  • 27

7 Answers7

239

Ended up changing Gradle JDK to 11.

File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle

Marcello B.
  • 4,177
  • 11
  • 45
  • 65
Juncu
  • 3,704
  • 4
  • 7
  • 27
  • 5
    Electric Eel is weird. First I had this with a Flutter project: https://stackoverflow.com/questions/51281702/unable-to-find-bundled-java-version-on-flutter/75119315#comment132597544_75119315 and then now with an Android project this seems to be also a problem with JDK version. Why did it reset those? – Csaba Toth Jan 16 '23 at 23:34
  • Looks like this problem is per project. I need to correct it with all the projects. This is madness. – Csaba Toth Jan 18 '23 at 18:08
  • 5
    This is madness! I'm having this issue as well. – jenndotcodes Jan 24 '23 at 01:20
  • 6
    Note that on macOS the settings are under the "Android Studio" menu. I just selected "Embedded JDK" here, which is the JDK bundled with Android Studio. – Frederik Feb 02 '23 at 12:20
  • Change Gradle JDK to JDK 11 – fmag Feb 23 '23 at 14:19
  • 1
    While creating a new project in Electic Eel variant of Android Studio, the mentioned issues arrise. It is basically compelling the developer to use JDK 11 instead of 1.8. Problem will be fixed after doing the same. Thanks – Deepak Rattan Mar 10 '23 at 06:56
  • 1
    If you are facing an issue in your Flutter project even after updating the Gradle JDK version, then you can try resolving the issue by restarting your Android Studio. To do this, follow these steps: Go to the "File" menu in Android Studio. Click on "Invalidate Caches". Click on "Invalidate and Restart". This should restart Android Studio and clear any cached data that may be causing the issue. After restarting, try running your Flutter project again to see if the issue has been resolved. This worked for me, so it may work for you too. – Muhammad Afzal Mar 13 '23 at 09:57
  • 3
    Encountered this while trying to run `reactivecircus/android-emulator-runner@v2` on github actions. Adding `actions/setup-java@v3` before running the emulator runner and setup JDK to 11 solved the issue. – dumbfingers Mar 20 '23 at 12:01
  • I understood that this is related to the project. But why we only changed it to JDK 11 and not earlier? – V Surya Kumar May 18 '23 at 08:09
  • For me other versions didn't seem to work either. – Juncu May 18 '23 at 11:07
  • For me with Android Studio Flamingo 2022.2.1 Patch 2, change to JDK 11 made Gradle happy, but app terminated on startup, complaining that it needed JDK 17. Sorry I did not note the exact exception. Then I switched back to JDK 17, and now builds and starts ok. – ozzylee Jul 06 '23 at 13:07
  • I faced the same problem for react native expo also. I opened the Android studio, did this operation (downloaded the gradle jde etc.), and then I closed the Android studio. After this operation, I run 'npx expo run:android' command. It worked. – gkhanacer Aug 20 '23 at 15:44
36

For Mac users (as @Frederik pointed out), 'settings' is 'Preferences': Preferences location

And then:
enter image description here
Finishing:
enter image description here

quin
  • 435
  • 6
  • 11
15

If on Kotlin Multiplatform (KMM) upgrade to Java 17.

When you create a new KMM project Android Studio's Gradle build setting auto-defaults to Java 1.8 (or whatever is specified for JAVA_HOME or org.gradle.java.jome).

If you upgrade to Java 11 or Java 13 it'll build successfully and run on Android Studio but it won't Build or Run successfully on Xcode.

The way around (it seems) is to go up one more version to Java 17 (Amazon's corretto-17 in my case)

corretto-17

It seemed like using a version lower than 17 was breaking the expected Gradle plugin version in Xcode (but only on Xcode) - which introduces scope issues with the shared folder.


Tested when creating a new project with CocoaPods and with Regular Framework.

Tested on Android Studio Flamingo alongside Xcode version 14.3

chornge
  • 2,021
  • 3
  • 28
  • 37
  • 1
    Had the same issue when trying to publish my package to `jitpack.io`, and upgrading the jdk in `jitpack.yml` with `openjdk17` indeed helped with the issue :D – KuhakuPixel Jul 04 '23 at 04:58
9

Needed to do two steps...

Change Gradle JDK to 11.

File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle

then

File -> Repair IDE. Ran through each of the steps until it restarted and then started downloading a load of files. Build worked after this.

mto1
  • 113
  • 2
  • 5
4

If you make adjustments like this in the latest version Android Studio Flamingo | 2022.2.1, you will not have any problems.

build.gradle(project)

plugins {
    id 'com.android.application' version '8.0.0' apply false
    id 'com.android.library' version '8.0.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}

build.gradle(:app)

...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
...

Gradle JDK settings:

enter image description here

Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
3

File > Manage IDE Settings > Restore Default Setting

This worked for me on Electric Eel

2

It also happened to me and the only option I found was to reinstall the project. When reinstalling I exported the settings and when it happens again I import them.