Kapt 到 KSP 的迁移错误。

15

当我尝试使用 kapt 迁移 Android 项目到 KSP 时,出现了错误信息。

错误信息:

Unable to find method ''void org.jetbrains.kotlin.gradle.tasks.KotlinCompile.<init>(org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions)''
'void org.jetbrains.kotlin.gradle.tasks.KotlinCompile.<init>(org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions)'

Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)

Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.

Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

目前,以下库正在使用 kapt

  1. Moshi
  2. Hilt
  3. Room

build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "kotlin-kapt"
    id "dagger.hilt.android.plugin"
    id "com.google.devtools.ksp" version "1.6.10-1.0.4"
}

dependencies {

    // Hilt
    implementation "com.google.dagger:hilt-android:$rootProject.hiltVersion"
    kapt "com.google.dagger:hilt-compiler:$rootProject.hiltVersion"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:$rootProject.hiltLifecycleViewModelVersion"
    kapt "androidx.hilt:hilt-compiler:$rootProject.hiltCompilerVersion"

    // KSP
    // implementation "com.google.devtools.ksp:symbol-processing-api:1.6.10-1.0.4"

    // Moshi
    implementation "com.squareup.moshi:moshi:$rootProject.moshiVersion"
    implementation "com.squareup.moshi:moshi-kotlin:$rootProject.moshiKotlinVersion"
    kapt "com.squareup.moshi:moshi-kotlin-codegen:$rootProject.moshiKotlinCodegenVersion"
    // ksp "com.squareup.moshi:moshi-kotlin-codegen:1.13.0"

    // Room
    implementation "androidx.room:room-runtime:$rootProject.roomVersion"
    implementation "androidx.room:room-ktx:$rootProject.roomVersion"
    annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion"
    ksp "androidx.room:room-compiler:$rootProject.roomVersion"
    androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
}

更多细节,请参见此处托管的代码库- https://github.com/Abhimanyu14/finance-manager

2个回答

16

如问题所述,我正在使用以下使用 kapt 的库。

  1. Moshi
  2. Hilt
  3. Room

KSP文档中,我发现 Hilt 尚未受到支持。 (截至2022年6月25日)。

因此,我需要在项目中同时使用 kapthilt


下面的 build.gradle 可以实现这一点,

plugins {
    id "kotlin-kapt"
    id "dagger.hilt.android.plugin"
    id "com.google.devtools.ksp" version "1.6.21-1.0.6"
}

// Hilt
implementation "com.google.dagger:hilt-android:$rootProject.hiltVersion"
kapt "com.google.dagger:hilt-compiler:$rootProject.hiltVersion"
//ksp "com.google.dagger:hilt-compiler:$rootProject.hiltVersion"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:$rootProject.hiltLifecycleViewModelVersion"
kapt "androidx.hilt:hilt-compiler:$rootProject.hiltCompilerVersion"
// ksp "androidx.hilt:hilt-compiler:$rootProject.hiltCompilerVersion"

// KSP
implementation "com.google.devtools.ksp:symbol-processing-api:1.6.21-1.0.6"

// Moshi
implementation "com.squareup.moshi:moshi:$rootProject.moshiVersion"
implementation "com.squareup.moshi:moshi-kotlin:$rootProject.moshiKotlinVersion"
// kapt "com.squareup.moshi:moshi-kotlin-codegen:$rootProject.moshiKotlinCodegenVersion"
ksp "com.squareup.moshi:moshi-kotlin-codegen:1.13.0"preferences:$rootProject.datastorePreferencesVersion"

// Room
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion"
// kapt "androidx.room:room-compiler:$rootProject.roomVersion"
ksp "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

库版本

roomVersion = "2.5.0-alpha02"
moshiVersion = "1.13.0"
hiltVersion = "2.42"
hiltPluginVersion = "2.40.1"
hiltLifecycleViewModelVersion = "1.0.0-alpha03"
hiltCompilerVersion = "1.0.0"

也遇到了这个错误 - Room - 模式导出目录未提供给注释处理器,因此我们无法导出模式

这个解决方案对我有帮助 - https://dev59.com/6FcP5IYBdhLWcg3wf58G#71451314

ksp {
    arg('room.schemaLocation', "$projectDir/schemas")
}

更新:KSP alpha-support 已在 Dagger/Hilt 2.48 版本中发布。 - artem
@Abhimanyu 但是当我使用Gradle目录时,如何进行配置呢? - undefined

4

我有同样的问题,看起来目前hilt还不支持KSP,所以需要在gradle文件中添加它。

plugin {
    ...
    id "kotlin-kapt"
    ...
}

替换

 ksp 'com.google.dagger:hilt-compiler:2.44'

使用

 kapt 'com.google.dagger:hilt-compiler:2.44'

在我的情况下它起作用了 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接