Search code examples
kotlingradledependenciesgradle-kotlin-dsl

Gradle "could not resolve project" even though paths set correctly


I have this folder structure:

├── build.gradle.kts
├── processor
│   ├── build.gradle.kts
├── processor-api
│   ├── build.gradle.kts
├── settings.gradle.kts
└── src
    ├── main
    └── test

This is what my processor module build.gradle.kts looks like:

plugins {
    `java-library`
    kotlin("jvm") version "1.9.10"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

//buildscript {
//    dependencies {
//        classpath(kotlin("gradle-plugin", version = "1.9.10"))
//    }
//}

dependencies {
    implementation(project(":processor-api"))
    implementation("com.google.devtools.ksp:symbol-processing-api:1.9.10-1.0.13")
}

tasks.test {
    useJUnitPlatform()
}

This is what my processor-api module build.gradle.kts looks like:

plugins {
    kotlin("jvm") version "1.9.10"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

This is what the : (main) module build.gradle.kts looks like:

plugins {
    kotlin("jvm") version "1.9.10"
    id("com.google.devtools.ksp") version "1.9.10-1.0.13"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation(project(":processor-api"))
    ksp(project(":processor"))
    testImplementation("org.jetbrains.kotlin:kotlin-test:1.9.10")
}

tasks.test {
    useJUnitPlatform()
}

kotlin {
    jvmToolchain(8)
    sourceSets.main {
        kotlin.srcDir("build/generated/ksp/main/kotlin")
    }
    sourceSets.test {
        kotlin.srcDir("build/generated/ksp/test/kotlin")
    }
}

And this is what my settings.gradle.kts file looks like:

rootProject.name = "leetcode-kotlin"
include("processor", "processor-api")

So, what I'm trying to achieve here is that both processor and main are dependent on processor-api, and that the main module uses the processor module as its annotation processor (which isn't that important to this question but I thought I'd mention that as well). Now, if I run the build task from the main module, I get this error:

Could not determine the dependencies of task ':compileJava'.
> Could not resolve all task dependencies for configuration ':compileClasspath'.
   > Could not resolve project :processor-api.
     Required by:
         project :
      > No matching variant of project :processor-api was found. The consumer was configured to find a library for use during compile-time, compatible with Java 8, preferably in the form of class files, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' but:
          - Variant 'apiElements' capability org.example:processor-api:1.0-SNAPSHOT declares a library for use during compile-time, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 8
          - Variant 'mainSourceElements' capability org.example:processor-api:1.0-SNAPSHOT declares a component, and its dependencies declared externally:
              - Incompatible because this component declares a component of category 'verification' and the consumer needed a library
              - Other compatible attributes:
                  - 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 8)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)
                  - Doesn't say anything about its usage (required compile-time)
                  - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'jvm')
          - Variant 'runtimeElements' capability org.example:processor-api:1.0-SNAPSHOT declares a library for use during runtime, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 8
          - Variant 'testResultsElementsForTest' capability org.example:processor-api:1.0-SNAPSHOT:
              - Incompatible because this component declares a component of category 'verification' 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 8)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)
                  - Doesn't say anything about its usage (required compile-time)
                  - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'jvm')

The main module cannot resolve processor-api and I have no idea why.


Solution

  • I got this fixed by removing this line from build.gradle.kts of the main module:

    jvmToolchain(8)
    

    Gradle didn't like that the other modules used JDK 17 and the main module used JDK 8.