如何将Room Persistence Library导入Android项目

53

我最近看到了Google I/O宣布的新功能,即Room Persistence Library,可用于在Android上使用Sqlite数据库。我一直在查看官方文档,但我没有找到应该导入到我的Android项目gradle文件中的依赖关系。有人能帮帮我吗?

13个回答

0

截至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

你知道在Kotlin和JAVA代码中使用Room时,我们是否需要同时使用kapt '...room-compiler'和annotationProcessor '...room-compiler'吗? - MikeOscarEcho

0
借鉴@Thracian的回答,我在遵循这份文档时需要做的是:

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


0
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"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接