我有以下的项目结构。
build.gradle.kts:38:22: 类型不匹配:推断类型为ProjectDependency,但期望的是Action
所以问题归结为如何在kotlin sourceSet上运行kapt插件,就像在简单的kotlin项目中通过将其添加到项目的依赖项来完成一样。
root-project
│ build.gradle.kts
│
└───multiplatform-project
│ │ build.gradle.kts
│ │
│ └───src
│ │ kotlin
| | js
│
└───simple-kotlin-project
│ build.gradle.kts
总共有3个项目(3个build.gradle.kts
文件)。
Here is the root project
build.gradle.kts
plugins { kotlin("jvm") version kotlinVersion apply false kotlin("kapt") version kotlinVersion apply false kotlin("multiplatform") version kotlinVersion apply false } subprojects { apply<JavaPlugin>() if (name.contains("multiplatform-project")) { apply(plugin = "org.jetbrains.kotlin.multiplatform") } else { apply(plugin = "kotlin") } apply(plugin = "kotlin-kapt") dependencies { implementation(kotlin("stdlib")) implementation(kotlin("reflect")) implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0-M2") implementation("org.slf4j:slf4j-api:1.7.25") implementation("ch.qos.logback:logback-core:1.2.3") implementation("ch.qos.logback:logback-classic:1.2.3") testImplementation("junit:junit:$jUnitVersion") } }
Here is the simple kotlin project that uses kapt and that works
val jacksonVersion: String by project dependencies { implementation("com.fasterxml.jackson.core:jackson-core:$jacksonVersion") implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion") implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion") implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion") kapt(project(":generator")) // THIS WORKS!!! }
Now my question is how to make this work in the multiplatorm project which looks like that
val jacksonVersion: String by project val ktorVersion: String by project kotlin { jvm { val main by compilations.getting { kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() } compileKotlinTask output } } js {} sourceSets { val jvmMain by getting { dependencies { implementation("com.fasterxml.jackson.core:jackson-core:$jacksonVersion") implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion") implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion") implementation("io.ktor:ktor-html-builder:$ktorVersion") implementation("io.ktor:ktor-server-netty:$ktorVersion") kapt(project(":generator")) // THIS DOESN'T WORK!!! } } } }
这是我在IDEA中看到的错误
这不是虚假错误。如果我运行gradle build,我会得到以下错误:build.gradle.kts:38:22: 类型不匹配:推断类型为ProjectDependency,但期望的是Action
所以问题归结为如何在kotlin sourceSet上运行kapt插件,就像在简单的kotlin项目中通过将其添加到项目的依赖项来完成一样。
configurations["kapt"].dependencies.add(project.dependencies.create("com.example:foo:1.0"))
- Felipe Lima