93

In my build.gradle file I upgraded the version of one dependency (namely: com.db:microservice-commons). After loading the gradle changes, I get the following error message:

> Build file 'C:\Projects\Container\mschmidt\mount\booking-service\standalone\build.gradle' line: 50

   > A problem occurred evaluating project ':standalone'.
   > Could not resolve all files for configuration ':standalone:runtimeClasspath'.
   > Could not resolve com.db:microservice-commons:2.4.1.
     Required by:
         project :standalone
         project :standalone > project :service
      > No matching variant of com.db:microservice-commons:2.4.1 was found. The consumer was configured to find a runtime of a library compatible with Java 10, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally but:
          - Variant 'apiElements' capability com.db:microservice-commons:2.4.1 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 10
              - Other compatible attribute:
                  - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
          - Variant 'runtimeElements' capability com.db:microservice-commons:2.4.1 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 10
              - Other compatible attribute:
                  - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
          - Variant 'samplessources' capability com.db:microservice-commons:2.4.1:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
                  - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
                  - Doesn't say anything about its target Java version (required compatibility with Java 10)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about its usage (required a runtime)

Line 50+ of file 'C:\Projects\Container\mschmidt\mount\booking-service\standalone\build.gradle' looks as follows:

manifest {
    def manifestClasspath = configurations.runtimeClasspath.collect { it.getName() }.join(' ')
    attributes 'Archiver-Version': 'Plexus Archiver'
    attributes 'Build-Jdk': org.gradle.internal.jvm.Jvm.current()
    attributes 'Main-Class': 'com.db.service.standalone.Standalone', 'Class-Path': manifestClasspath
    }
}

I have no clue what to make of this. Project JDK and Gradle JVM are set to Java 11. This post here: The consumer was configured to find a runtime of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally but: ... pertains to the same problem but didn't help me.

Any ideas what's going on here?

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Thomas_SO
  • 1,883
  • 2
  • 10
  • 20

11 Answers11

81

With IntelliJ IDEA, I had to change which JDK Gradle used for running. The original value was JDK 11, I changed it to JDK 17.

Screenshot

jndietz
  • 1,216
  • 1
  • 11
  • 12
  • 3
    Had the same problem with Android Studio (Electric Eel) in a new project. Updated the `Gradle JDK` to 11 from the default 8 to fix the project sync. – qix Feb 14 '23 at 08:47
  • 1
    Thank you! I checked all my project specific JDK version configs and didn't find anything that still pointed to 11, and it was driving me insane. But this fixed the issue for me. – bcody May 26 '23 at 07:15
  • 1
    thank you isr, it was this. – rpajaziti Sep 01 '23 at 13:38
33

I found the problem and solved it.

I changed two lines defining the compatibility in the build.gradle file.

From:

compileOptions {
    sourceCompatibility 10
    targetCompatibility 10
}

To:

compileOptions {
    sourceCompatibility 11
    targetCompatibility 11
}
Thomas_SO
  • 1,883
  • 2
  • 10
  • 20
  • 62
    Our team also ran into this issue. It was caused by the Gradle version of IntelliJ using another JVM. To solve this go to the preferences: Build, Execution, Deployment > Build Tools > Gradle > Gradle JVM and change the jvm version there – jvwilge Sep 02 '22 at 08:36
  • 4
    thanks @jvwilge , just spent an hour or so scratching my head after declaring sourceCompatibility/targetCompatibility and still facing this issue! – Renato Sep 09 '22 at 12:10
  • 3
    likewise, thanks @jvwilge, been fighting it for hours. – Evan Knowles Sep 20 '22 at 11:40
  • 2
    Thanks @jvwilge I was confused why it still mentions Java 8 even though I have set the JDK to 11 in Project Settings. – Aiono Sep 27 '22 at 11:52
  • 1
    @jvwilge You're my hero – dks209 Dec 01 '22 at 22:56
  • @jvwilge You're my hero – HalfLegend Dec 08 '22 at 02:50
  • 2
    @jvwilge thanks man! consider please set you comment as an answer in order to help other people that don't see comments. – Vrian7 Jan 11 '23 at 12:44
23

Change the java version which gradle use. As the picture shown below, choose java 11.Then it is ok. enter image description here

dong liu
  • 251
  • 2
  • 3
3

it is works for me change the code in the app/build.gradle file to this:

android {
    compileSdkVersion 33

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    ...
}

or you can change it in the File/project structure

enter image description here

enter image description here

Karzan Kamal
  • 1,074
  • 10
  • 9
2

in my case, it wasn't about Java Version. it still remain 1.8 (8). if you're using the latest version of Kotlin, you should update your Gradle Version to 6.7.1 or higher in Project Structure. I recommend changing Gradle Plugin Version to the latest Staible Version.

A.R.B.N
  • 1,035
  • 2
  • 10
  • 20
2

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 10

The meaning of this error is gradle wants to run at SDK 11 and SDK provided by your project structure is of version 10

I also faced the same issue while setting up the project in intelliJ,

"Incompatible because this component declares an API of a component compatible with Java 17 and the consumer needed a runtime of a component compatible with Java 11"

Solution : I downloaded Java SE Development kit 17 and integrate JDK 17 with project structure. Its worked.

Aman
  • 23
  • 3
2

With IntelliJ IDEA, for me it worked with file -> invalidate caches -> invalidate and restart after correcting the version of Gradle JDK/JVM.

johnnyasd12
  • 636
  • 7
  • 11
1

Well, I faced a similar issue

Incompatible because this component declares a component for use during compile-time, compatible with Java 18 and the consumer needed a component for use during runtime, compatible with Java 17

Context: I imported my colleague's project which incorporates Gradle in the project.

Upon opening it in VS code, I saw red squiggly lines. And although VS code's awesome awareness of detecting the build tool, the programming language and automatically suggesting the relevant plugins was a great help for a Gradle newbie like me, but some errors still persisted. And one of them was this.

After skimming through basic concepts like Projects, Plugins, Tasks in the official docs, I could understand the error.

Fix And the fix which worked for me was that in the Gradle Build script (build.gradle) in my main project, a property sourceCompatibility was set to 17. And I had build the project using jdk 18.

This property ensures to use a specific Java version when compiling Java source. When I change this to 18, the project could be built.

0

The Java version of my JAVA_HOME configuration was 10 before. After changing to Java11, it will be fine.

lzy187
  • 41
  • 5
0

create a file in root system.properties and add there java.runtime.version=17

Josef Dula
  • 11
  • 1
0

For Kotlin programmers using IntelliJ IDEA: I encountered this error when setting up a new project in IntelliJ. I had to specify the Java version inside of build.gradle, like so:

Change this

kotlin {
    jvmToolchain(8) // The culprit is here. Targeting the wrong, older, Java version.
}

to this

kotlin {
    jvmToolchain(17)
}
Payne Miller
  • 134
  • 1
  • 8