如何将 Room Persistence Library 导入 Android 项目

How to import Room Persistence Library to an Android project

我最近在 Google I/O Room Persistence Library 上看到了与 Android 上的 Sqlite 数据库一起工作的新功能。我一直在寻找 official documentation,但没有找到应该将哪些依赖项导入到 Android 项目的 gradle 文件中。有人可以帮帮我吗?

可以找到新架构组件对 the example codelab 的依赖。

根目录:

allprojects {
repositories {
    jcenter()
    maven {
        url "https://maven.google.com"
    }
}

房间:

  implementation 'android.arch.persistence.room:runtime:1.0.0-alpha1'
  annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-alpha1'

对于生命周期依赖项:

  implementation 'android.arch.lifecycle:extensions:1.0.0-alpha1'
  annotationProcessor 'android.arch.lifecycle:compiler:1.0.0-alpha1'

添加 Rxjava2 对象作为我们查询的结果:

  implementation 'android.arch.persistence.room:rxjava2:1.0.0-alpha1'

测试迁移:

  testImplementation'android.arch.persistence.room:testing:1.0.0-alpha1'

试试这个来编译 Room Persistence 库

implementation 'android.arch.persistence.room:runtime:1.1.1';
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1';

并在根级构建中添加这个 gradle

allprojects {
repositories {
    jcenter()
    maven {
        url "https://maven.google.com"
    }
}

在您的项目 root build.gradle 中,您必须添加 Google 的 maven 存储库:

allprojects {
    repositories {
        jcenter()
        maven {
            // For Room Persistence Library
            url "https://maven.google.com"
        }
    }
}

然后在 模块 build.gradle 上你应该添加:

compile 'android.arch.persistence.room:runtime:1.0.0-alpha1'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-alpha1'
compile 'android.arch.lifecycle:extensions:1.0.0-alpha1'
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0-alpha1'
compile 'android.arch.persistence.room:rxjava2:1.0.0-alpha1'
testCompile'android.arch.persistence.room:testing:1.0.0-alpha1'

如果您想使用 RxJava2 PublisherFlowable 对象作为查询的结果,请添加此选项

compile 'android.arch.persistence.room:rxjava2:1.0.0-alpha1'

最后还将此依赖项添加到 测试迁移

testCompile'android.arch.persistence.room:testing:1.0.0-alpha1'

更新: 库仍然标记为Alpha1所以我猜版本号会很快更新,也许使用 1.0.+ 直到有 final 版本可能是个好主意 已经更新并且 as definded on the documentation 你可以使用 room 1.1.1使用此依赖项:

dependencies {
   def room_version = "1.1.1"

   implementation "android.arch.persistence.room:runtime:$room_version"
   annotationProcessor "android.arch.persistence.room:compiler:$room_version" // use kapt for Kotlin

   // optional - RxJava support for Room
   implementation "android.arch.persistence.room:rxjava2:$room_version"

   // optional - Guava support for Room, including Optional and ListenableFuture
   implementation "android.arch.persistence.room:guava:$room_version"

   // Test helpers
   testImplementation "android.arch.persistence.room:testing:$room_version"
}

Android doc:

添加 Google Maven 存储库 Android 默认情况下,Studio 项目未配置为访问此存储库。

要将其添加到您的项目中,请打开项目的 build.gradle 文件(不是您的应用程序或模块的文件)并添加突出显示的行,如下所示:

allprojects {
repositories {
    jcenter()
    maven { url 'https://maven.google.com' }
    }
}

添加架构组件

打开您的应用程序或模块的 build.gradle 文件并添加您需要的工件作为依赖项:

对于生命周期、LiveData 和 ViewModel,添加:

implementation "android.arch.lifecycle:runtime:1.0.0-alpha1"
implementation "android.arch.lifecycle:extensions:1.0.0-alpha1"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha1"

对于房间,添加:

implementation "android.arch.persistence.room:runtime:1.0.0-alpha1"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"

打开 您的项目 build.gradle 文件(不是您的应用程序或模块的文件)并添加:

allprojects {
    repositories {
        google()
        jcenter()
    }
}

打开您的应用程序或模块build.gradle文件并添加您需要的工件作为依赖项:

dependencies {

    implementation "android.arch.persistence.room:runtime:1.0.0"
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0"

}

参考:Android Adding Components

    * Add these in project level gradle

    allprojects {
        repositories {
            jcenter()
            maven { url 'https://maven.google.com' }
        }
    }


    ext {
        buildToolsVersion = "25.0.2"
        supportLibVersion = "25.3.1"
        archRoomVersion = "1.0.0-alpha1"
    }


    * Add these in module level gradle dependencies

dependencies {

     compile 'android.arch.persistence.room:runtime:' + rootProject.archRoomVersion;
        annotationProcessor 'android.arch.persistence.room:compiler:' + rootProject.archRoomVersion;

}

这适用于最新版本的 Android Studio 3.1。

在 Gradle 脚本文件夹下, 在 项目 build.gradle 中,添加:

allprojects {
    repositories {

...

        maven { url 'https://maven.google.com' }
    }
}

模块 build.gradle中,添加:

dependencies {

...
    implementation "android.arch.persistence.room:runtime:1.0.0"
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0"

}

来源:https://developer.android.com/topic/libraries/architecture/adding-components.html

当前(2018 02 14)版本是

compile 'android.arch.persistence.room:rxjava2:1.1.0-alpha1'
compile 'android.arch.persistence.room:runtime:1.1.0-alpha1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.0-alpha1'

compile 'android.arch.lifecycle:extensions:1.1.0'
annotationProcessor 'android.arch.lifecycle:compiler:1.1.0'

打开 build.gradle(Project: projectName) 并添加此行(如果不存在)。

这允许访问 google 存储库

allprojects {
repositories {
    jcenter()
    google()
 }
}

接下来打开build.gradle(Module:app)并将这两行添加到现有依赖项

For Java

def room_version = "1.1.0"
implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version"

For Kotlin

将此行粘贴到应用 kotlin 插件的顶部的依赖项或 android 范围之外

apply plugin: 'kotlin-kapt'

将此行添加到依赖项

def room_version = "1.1.0"
implementation "android.arch.persistence.room:runtime:$room_version"
kapt "android.arch.persistence.room:compiler:$room_version"

to keep yourself updated with the recent dependencies version visit mvn repository for room

在这里您可以找到最新版本。

对于 androidX 和 kapt

def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
kapt "androidx.room:room-compiler:$room_version"

自 2019 年 7 月起,如果您想将 Room 与 Kotlin、AndroidX、Coroutines 或 RxJava 一起使用,请在下面添加行。

   // 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

跳出@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

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"