[英]Android Room in Kotlin 1.7.0
When updating to Kotlin 1.7.0, since it's required by the latest version of Jetpack Compose, I found out that Room was no longer working.当更新到 Kotlin 1.7.0 时,由于最新版本的 Jetpack Compose 需要它,我发现 Room 不再工作。 I was using kapt
as my annotation processor, and the compiler was throwing error messages such as:我使用kapt
作为注释处理器,编译器抛出错误消息,例如:
[*] error: Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.
The fix was to migrate from kapt
to ksp
.解决方法是从kapt
迁移到ksp
。 KSP is the theoretical successor of kapt, and it's developed by Google. KSP 是 kapt 的理论继承者,由 Google 开发。 To do so, first I had to import the library into my classpath:为此,首先我必须将库导入到我的类路径中:
buildscript {
...
repositories {
...
classpath "com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:1.7.0-1.0.6"
...
}
}
Check the latest version from MVN Repository just in case there has been an update, but as the time of writing this the latest version for Kotlin 1.7.0 is 1.7.0-1.0.6
.检查MVN Repository的最新版本以防有更新,但在撰写本文时,Kotlin 1.7.0 的最新版本是1.7.0-1.0.6
。
Now, in my module's build.gradle
:现在,在我的模块的build.gradle
:
plugins {
...
// Remove the old kapt plugin
id 'org.jetbrains.kotlin.kapt'
// Add the new KSP plugin
id 'com.google.devtools.ksp'
...
}
...
android {
...
defaultConfig {
...
// Replace old compile options
javaCompileOptions {
annotationProcessorOptions {
arguments += [ "room.schemaLocation": "$projectDir/schemas".toString() ]
}
}
// With the new KSP arg method
ksp {
arg("room.schemaLocation", "$projectDir/schemas".toString())
}
...
}
...
}
...
dependencies {
...
// Replace all kapt calls with ksp
// kapt "androidx.room:room-compiler:$room_version"
ksp "androidx.room:room-compiler:$room_version"
}
Now Room should be working correctly in Kotlin 1.7.0!现在 Room 应该在 Kotlin 1.7.0 中正常工作了!
You can update the room version to 2.5.0-alpha02
, which supports Kotlin 1.7.0, but it's still in alpha.您可以将房间版本更新为2.5.0-alpha02
,它支持 Kotlin 1.7.0,但仍处于 alpha 版本。 Check the latest release here enter link description here在此处查看最新版本 在此处输入链接描述
Upgrading Room to stable version 2.4.3 has fixed this issue (for me) - I can now build the project with Kotlin 1.7.10.将 Room 升级到稳定版本 2.4.3 已解决此问题(对我而言) - 我现在可以使用 Kotlin 1.7.10 构建项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.