You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android项目kapt迁移至KSP时提示找不到KotlinCompile构造方法错误

构建报错现象
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.

IDE提示的三类可能诱因(Gradle依赖缓存损坏、Gradle守护进程状态异常、第三方插件版本不兼容)对应的常规排查方案(重新下载依赖同步、停止所有Gradle守护进程、关闭IDE终止全部Java进程)均无法解决该问题。

问题场景

该问题出现在将原有使用kapt的Android项目迁移至KSP的构建过程中,项目内原有3个依赖使用kapt处理:

  • Moshi
  • Hilt
  • 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"
}
问题根因

该报错和Gradle缓存损坏、守护进程异常无关,核心原因是KSP插件版本与项目当前使用的Kotlin Gradle插件版本不匹配:
KSP有严格的版本绑定规则,版本号格式为<对应适配的Kotlin版本>-<KSP自身迭代版本号>,必须和项目引入的Kotlin Gradle插件版本完全对应,否则插件初始化时会调用对应Kotlin版本中不存在的KotlinCompile构造方法,直接触发该方法找不到的报错。
当前配置中硬编码的KSP版本为1.6.10-1.0.4,仅适配Kotlin 1.6.10版本,只要项目实际使用的Kotlin版本不是1.6.10,就会触发该报错。

解决步骤
  • 第一步:先在项目根目录的build.gradle文件中确认当前项目使用的Kotlin Gradle插件的精确版本号
  • 第二步:替换模块中声明的KSP插件版本,选择和当前Kotlin版本严格对应的KSP发布版本,禁止跨Kotlin版本混用KSP
  • 第三步:优化KSP插件声明方式,不要在业务模块的build.gradle中硬编码KSP版本,将KSP插件声明移到根目录build.gradle的plugins块中并标记apply false,业务模块仅引用插件id即可,避免多模块版本不一致
  • 第四步:逐一对齐注解处理器的支持版本后再替换kapt为ksp,避免半迁移状态下的兼容问题:
    • Hilt 2.48以下版本不支持KSP,低版本需继续保留kapt配置
    • Moshi 1.13.0及以上版本才支持KSP,替换时需将moshi-kotlin-codegen版本升级到对应区间
    • Room 2.4.0及以上版本才支持KSP,替换时需保证room-compiler版本和Room runtime版本完全一致
  • 第五步:版本全部对齐后,执行./gradlew clean清理旧构建产物,再重新同步项目构建即可

内容的提问来源于stack exchange,提问作者Abhimanyu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 05:51:27