Java - Fix Gradle No Matching Variant of Project Error

This post is about how to fix 'No matching variant of project' error when running a Java project using gradle.

When running a Java project using gradle, you may get an error indicating that a variant of a library was not found. Below is the example.

  Could not determine the dependencies of task ':project-one:MyApplication.main()'.
  > Could not resolve all task dependencies for configuration ':project-one:runtimeClasspath'.
     > Could not resolve project :commons.
       Required by:
           project :project-one
           project :project-one > project :project-one-api
        > No matching variant of project :commons was found. The consumer was configured to find a runtime of a library compatible with Java 11, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally but:
            - Variant 'apiElements' capability com.domain:commons:1.0-SNAPSHOT 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 17 and the consumer needed a runtime of a component compatible with Java 11
                - Other compatible attribute:
                    - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)

  * Try:
  > Run with --stacktrace option to get the stack trace.
  > Run with --info or --debug option to get more log output.
  > Run with --scan to get full insights.

The above error usually happens because the Java version that runs the project is different from the one used by gradle. It's quite common after upgrading the Java version, but the gradle config has not been updated. For the error above, the Java SDK is set to version 17, but the gradle still uses Java 11. The fixes below should work for any version.

Update Gradle JVM Config

If you use IntelliJ, go to Build, Execution, Deployment -> Build Tools -> Gradle. Find Gradle JVM field and select the correct Java SDK version.

Then, try to re-run the project.

Change Compatibility Versions in build.gradle

In the build.gradle of your project, find sourceCompatibility and change it to the correct version. Do the same for targetCompatibility if it's presence.

  sourceCompatibility = '17'
  targetCompatibility = '17'

For Android projects, those values are inside the compileOptions block.

  android {
      compileSdkVersion 33
      compileOptions {
          sourceCompatibility JavaVersion.VERSION_17
          targetCompatibility JavaVersion.VERSION_17

After that, try to re-run the project.


The 'No matching variant of project' is a common error that you will get if the gradle uses a different Java SDK version. The solution is by correcting the version in Gradle JVM config or build.gradle. If it doesn't work, try to invalidate the cache and restart your IDE.