我最近看到了Google I/O宣布的新功能,即Room Persistence Library,可用于在Android上使用Sqlite数据库。我一直在查看官方文档,但我没有找到应该导入到我的Android项目gradle文件中的依赖关系。有人能帮帮我吗?
我最近看到了Google I/O宣布的新功能,即Room Persistence Library,可用于在Android上使用Sqlite数据库。我一直在查看官方文档,但我没有找到应该导入到我的Android项目gradle文件中的依赖关系。有人能帮帮我吗?
截至2019年7月,如果您想要在Kotlin、AndroidX、Coroutines或RxJava中使用Room,请添加以下行。
// Room
implementation 'androidx.room:room-runtime:' + rootProject.roomVersion
// For Kotlin use kapt instead of annotationProcessor
kapt 'androidx.room:room-compiler:' + rootProject.roomVersion
// optional - Kotlin Extensions and Coroutines support for Room
implementation 'androidx.room:room-ktx:' + rootProject.roomVersion
// optional - RxJava support for Room
implementation 'androidx.room:room-rxjava2:' + rootProject.roomVersion
https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin/#13
/* Room */
implementation 'androidx.room:room-runtime:2.1.0'
kapt 'androidx.room:room-runtime:2.1.0'
implementation 'androidx.room:room-compiler:2.1.0'
kapt 'androidx.room:room-compiler:2.1.0'
annotationProcessor 'androidx.room:room-compiler:2.1.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha02'
kapt 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha02'
implementation 'androidx.room:room-ktx:2.1.0'
kapt 'androidx.room:room-ktx:2.1.0'
implementation 'android.arch.lifecycle:extensions:1.1.1'
kapt 'android.arch.lifecycle:extensions:1.1.1'
同时在 android {}
中我需要添加:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
尽管我已经阅读过的内容,但在Kotlin中仍然必须使用annotationProcessor
def room_version = "2.2.3"
def lifecycle_version = "2.1.0"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" // ViewModel and LiveData
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"