路径配置正确但Gradle提示无法解析processor-api项目
问题:主模块无法解析processor-api依赖,编译失败
项目目录结构
├── build.gradle.kts ├── processor │ ├── build.gradle.kts ├── processor-api │ ├── build.gradle.kts ├── settings.gradle.kts └── src ├── main └── test
各模块配置文件
processor模块 build.gradle.kts
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() }
processor-api模块 build.gradle.kts
plugins { kotlin("jvm") version "1.9.10" } group = "org.example" version = "1.0-SNAPSHOT" repositories { mavenCentral() }
主模块 build.gradle.kts
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") } }
settings.gradle.kts
rootProject.name = "leetcode-kotlin" include("processor", "processor-api")
编译错误日志
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')
原因分析
错误核心在于版本不兼容:主模块指定了Java 8工具链,但processor-api未配置Java版本,默认使用Java 17编译,导致Gradle无法匹配到兼容的依赖变体。另外processor-api未应用java-library插件,作为被其他模块依赖的库,这会影响Gradle生成正确的依赖发布变体。
解决方案
1. 修改processor-api模块配置
添加java-library插件并统一Java 8工具链:
plugins { `java-library` kotlin("jvm") version "1.9.10" } group = "org.example" version = "1.0-SNAPSHOT" repositories { mavenCentral() } kotlin { jvmToolchain(8) }
2. 修改processor模块配置
同样添加Java 8工具链配置,保证全模块版本统一:
plugins { `java-library` kotlin("jvm") version "1.9.10" } group = "org.example" version = "1.0-SNAPSHOT" repositories { mavenCentral() } dependencies { implementation(project(":processor-api")) implementation("com.google.devtools.ksp:symbol-processing-api:1.9.10-1.0.13") } tasks.test { useJUnitPlatform() } kotlin { jvmToolchain(8) }
完成以上修改后,所有模块统一使用Java 8编译,processor-api作为标准Java库提供正确的依赖变体,主模块即可正常解析依赖并完成编译。
内容的提问来源于stack exchange,提问作者Dj Sushi
相关产品推荐
相关产品推荐

