使用KSP替代Kapt后Android项目构建失败问题排查
问题:替换Kapt为KSP后项目构建失败,提示"No providers found in processor classpath"
开发Android应用时,计划使用Dagger Hilt、Glide,原本采用Kapt,因Hilt相关错误尝试替换为官方推荐的KSP,但替换后项目无法构建,错误信息如下:
> Task :app:kspDebugKotlin FAILED e: [ksp] No providers found in processor classpath. e: Error occurred in KSP, check log for detail FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:kspDebugKotlin'. > A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction > Compilation error. See log for more details * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org
项目配置
项目级build.gradle
buildscript { ext { kotlin_version = '1.9.22' } dependencies { classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.1' classpath 'com.google.gms:google-services:4.3.13' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.22" def nav_version = "2.5.0" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } repositories { mavenCentral() } } // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '8.0.2' apply false id 'com.android.library' version '8.0.2' apply false id 'org.jetbrains.kotlin.android' version '1.9.22' apply false id 'com.google.devtools.ksp' version "1.9.22-1.0.17" apply false }
模块级build.gradle
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'com.google.devtools.ksp' id 'androidx.navigation.safeargs.kotlin' id 'dagger.hilt.android.plugin' id 'kotlin-parcelize' id 'com.google.gms.google-services' } android { namespace 'com.unitytests.virtumarttest' compileSdk 33 defaultConfig { applicationId "com.unitytests.virtumarttest" minSdk 27 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } buildFeatures{ viewBinding true } } dependencies { implementation 'androidx.core:core-ktx:1.8.0' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.core:core-ktx: 1.8.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' //apply plugin :"kotlin-kapt" //loading button implementation 'br.com.simplepass:loading-button-android:2.2.0' //Glide implementation 'com.github.bumptech.glide:glide:4.13.0' //circular image implementation 'de.hdodenhof:circleimageview:3.1.0' //viewpager2 indicatior implementation 'io.github.vejei.viewpagerindicator:viewpagerindicator:1.0.0-alpha.1' //stepView implementation 'com.shuhart.stepview:stepview:1.5.1' //Android Ktx implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2' //Dagger hilt implementation "com.google.dagger:hilt-android:2.38.1" ksp "com.google.dagger:hilt-compiler:2.38.1" //Firebase implementation 'com.google.firebase:firebase-auth:21.1.0' }
问题分析与修复步骤
1. 统一Hilt版本并升级至支持KSP的版本
项目级gradle中Hilt插件版本是2.40.1,但模块依赖的Hilt核心库和编译器版本是2.38.1,版本不一致。更关键的是,Hilt从2.40版本开始才正式支持KSP,2.38.1版本的hilt-compiler没有KSP支持,导致KSP找不到处理器。
修复:修改模块级dependencies中的Hilt依赖,统一版本到2.40.1:
// Dagger hilt implementation "com.google.dagger:hilt-android:2.40.1" ksp "com.google.dagger:hilt-compiler:2.40.1"
2. 配置Glide的KSP支持(可选,若需完全替换Kapt)
当前仅替换了Hilt的Kapt依赖,Glide仍未配置KSP版本的编译器。注意Glide从4.14.0版本开始提供KSP支持,若要完全替换Kapt,需升级Glide并添加对应KSP依赖:
// Glide implementation 'com.github.bumptech.glide:glide:4.16.0' ksp 'com.github.bumptech.glide:ksp:4.16.0'
3. 移除重复的Kotlin插件配置
项目级build.gradle的buildscript中声明了kotlin-gradle-plugin,但plugins块中已声明org.jetbrains.kotlin.android插件,两者重复,可移除buildscript中的classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.22"。
4. 清理缓存并重新构建
完成上述修改后,执行Clean Project和Rebuild Project操作,确保旧缓存被清除。
内容的提问来源于stack exchange,提问作者Isuru Dissanayake
相关产品推荐
相关产品推荐

