I have been struggling to add Room dependencies for a couple of days now(not proud). There is always an error no matter what i try. I am not the most experienced though.
几天来,我一直在努力添加Room Dependents(并不自豪)。无论我怎么尝试,总会有错误。不过,我不是最有经验的人。
I added the below from the Android docs site.
我从Android文档站点添加了以下内容。
`val room_version = "2.5.2"
`val Room_Version=“2.5.2”
implementation("androidx.room:room-runtime:$room_version")
annotationProcessor("androidx.room:room-compiler:$room_version")
//1
// To use Kotlin annotation processing tool (kapt)
kapt("androidx.room:room-compiler:$room_version")
//2
// To use Kotlin Symbol Processing (KSP) (2)
ksp("androidx.room:room-compiler:$room_version")
// optional - Kotlin Extensions and Coroutines support for Room
implementation("androidx.room:room-ktx:$room_version")`
All depencdencies can resolve except what is marked 1 & 2.
For 1, i get
Type mismatch. Required: KaptOptions Found: String
除标记为1和2的依赖项外,所有依赖项都可以解析。对于1,我得到类型不匹配。必需:找到KaptOptions:字符串
For 2, i get
Unresolved reference: ksp
对于2,我得到了未解析的引用:KSP
I added these to my plugins from searches i did but to no avail.
` id("com.google.devtools.ksp") version "1.8.21-1.0.11"
kotlin("android")
我把这些从搜索中添加到我的插件中,但无济于事。` id(“com.google.devtools.ksp”)version“1.8.21-1.0.11”kotlin(“android”)
// Apply the Room and Kotlin Kapt plugins
kotlin("kotlin-kapt")
id("androidx.room.room-compiler")`
更多回答
Note you do not need both, just one or the other
注意:你不需要两个都需要,只需要其中一个
@ryankuck Yes. i did one at a time
@ryankuck是的。我一次做一个
Adding <id("com.google.dagger.hilt.android") version "2.47" apply false> to my plugins has my app running now. But i don't think that's an ideal solution. Or is it?
将添加到我的插件后,我的应用程序现在可以运行了。但我不认为这是一个理想的解决方案。还是真的是这样?
优秀答案推荐
I had the same issue 3 months ago, my solution was this:
3个月前我也遇到过同样的问题,我的解决方案是:
At the top of the build.gradle (:app)
inside plugins
在build.gradle(:应用程序)内部插件的顶部
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}
And then in your dependencies
, same file
然后在您的依赖项中,相同的文件
/* ROOM */
def roomVersion = "2.5.2" //this was 2.5.1 back then
implementation "androidx.room:room-ktx:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"
Finally, check if Android Studio wrote a Room dependency for you before and delete it:
最后,检查Android Studio之前是否为您编写了Room Dependent并将其删除:
implementation 'androidx.room:room-common:2.5.2'
Documentation is here
文档在此处
What properly worked was following the migration guide at
Migrate from kapt to KSP
正确的做法是遵循从KAPT迁移到KSP的迁移指南
I used
我以前
ksp("androidx.room:room-compiler:$room_version")
Added the below to the project level build.grradle
将以下内容添加到项目级构建中。grradle
plugins {
id("com.google.devtools.ksp") version "1.8.10-1.0.9" apply false}
Added the below to the module level build.gradle
在模块级别build.gradle中添加了以下内容
plugins {
id("com.google.devtools.ksp")}
And remove everything added for the kapt
并删除为Kapt添加的所有内容
- Open your project
build.gradle
.
- Add the plugin
com.google.devtools.ksp
with the correct version:
...
plugins {
...
// x.y.z must be THE SAME for all the kotlin/ksp plugins.
// (My version is 1.9.10)
id "org.jetbrains.kotlin.jvm" version 'x.y.z' apply false
id "org.jetbrains.kotlin.android" version 'x.y.z' apply false
...
// Choose an appropriate version from:
// https://github.com/google/ksp/releases
id "com.google.devtools.ksp" version 'x.y.z-a.b.c' apply false
}
- Open your module
build.gradle
where you add room
library.
- Add
com.google.devtools.ksp
to plugins
:
plugins {
...
id 'com.google.devtools.ksp'
}
- Replace kapt by ksp:
...
dependencies {
// delete kapt "androidx.room:room-compiler:$room_version"
ksp "androidx.room:room-compiler:$room_version"
}
更多回答
我是一名优秀的程序员,十分优秀!