添加KSP与Room库后构建失败:无法找到annotation-1.3.0.jar
解决添加KSP后构建失败找不到androidx.annotation:annotation:1.3.0的问题
问题分析
添加KSP后构建失败,提示找不到androidx.annotation:annotation:1.3.0,这是因为Room编译器或KSP在依赖传递过程中未能正确拉取该依赖,或是版本兼容性问题导致依赖解析异常。
解决方案
1. 显式引入androidx.annotation依赖
通过显式添加该依赖,强制Gradle拉取指定版本的注解库:
- 修改
libs.version.toml,添加annotation版本及依赖配置:
[versions] # 保留原有版本配置,新增以下行 annotation = "1.7.0" [libraries] # 保留原有依赖配置,新增以下行 androidx-annotation = { group = "androidx.annotation", name = "annotation", version.ref = "annotation" }
- 在app模块的
build.gradle.kts的dependencies块中添加:
dependencies { // 保留原有依赖,新增以下行 implementation(libs.androidx.annotation) }
2. 验证KSP与Kotlin版本兼容性
你的Kotlin版本为2.0.0,搭配的KSP版本2.0.0-1.0.21属于官方兼容组合,但如果问题仍存在,可以尝试切换到更稳定的KSP版本(比如1.9.22-1.0.17,需同步调整Kotlin版本到1.9.22),确保版本完全匹配。
3. 清理缓存并重新构建
缓存损坏也可能导致依赖解析失败,执行以下操作:
- 在Android Studio中选择
File -> Invalidate Caches...,点击Invalidate and Restart重启IDE - 或在项目根目录终端执行命令:
./gradlew clean build --refresh-dependencies
4. 检查仓库配置
确保settings.gradle.kts中包含必要的仓库,保证依赖能正常拉取:
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } }
内容的提问来源于stack exchange,提问作者Mostafa Shafayi
相关产品推荐
相关产品推荐

