52

I recently saw the new feature announced on Google I/O Room Persistence Library to work with Sqlite databases on Android. I have been looking to the official documentation and I don't find which dependencies I should import to my gradle file on my Android project. Someone can give me a hand?

4

13 回答 13

54

It's possible to find the dependencies on the example codelab for the new architecture components.

Root :

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

For Room:

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

For Lifecycle dependencies:

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

Adding Rxjava2 objects as result for our queries:

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

Test migrations:

  testImplementation'android.arch.persistence.room:testing:1.0.0-alpha1'
于 2017-05-18T11:46:05.720 回答
7

On your project root build.gradle you have to add Google's maven repository:

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

And then on the build.gradle of the Module you should add:

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'

Add this if you want to use RxJava2 Publisher and Flowable objects as a result of your queries

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

And finally add also this dependency to test migrations

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

Update: The libraries are still marked as Alpha1 so I guess the version number will be updated soon, maybe using 1.0.+ until there is a final version could be a good idea have been updated and as definded on the documentation you can use room 1.1.1 using this dependencies:

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"
}
于 2017-05-18T22:06:04.973 回答
5

Try this to compile Room Persistence library

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

And add this in root level build gradle

allprojects {
repositories {
    jcenter()
    maven {
        url "https://maven.google.com"
    }
}
于 2017-05-18T11:50:12.773 回答
5

Android doc:

Add the Google Maven repository Android Studio projects aren't configured to access this repository by default.

To add it to your project, open the build.gradle file for your project (not the ones for your app or module) and add the highlighted line as shown below:

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

Add Architecture Components

Open the build.gradle file for your app or module and add the artifacts that you need as dependencies:

For Lifecycles, LiveData, and ViewModel, add:

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"

For Room, add:

implementation "android.arch.persistence.room:runtime:1.0.0-alpha1"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"
于 2017-05-19T20:53:56.737 回答
5

open build.gradle(Project: projectName) and add this lines if it doesn't exist.

This enables access to google repository

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

next open build.gradle(Module:app) and add this two lines to existing dependencies

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

paste this line outside of the dependencies or android scope at the top where kotlin plugins are applied

apply plugin: 'kotlin-kapt'

add this lines to dependencies

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

here you will find the latest version. enter image description here

于 2018-08-18T07:49:53.393 回答
5

For androidX and 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"
于 2018-12-16T07:34:45.277 回答
3
    * 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;

}
于 2017-12-01T07:00:45.683 回答
2

This works with the latest version of Android Studio 3.1.

Under Gradle Scripts folder, in the Project build.gradle, add:

allprojects {
    repositories {

...

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

In the Module build.gradle, add:

dependencies {

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

}

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

于 2017-12-01T19:04:51.357 回答
1

Current (2018 02 14) versions are

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'
于 2018-02-14T10:14:29.590 回答
0

Open the build.gradle file for your project (not the ones for your app or module) and add:

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

Open the build.gradle file for your app or module and add the artifacts that you need as dependencies:

dependencies {

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

}

Reference: Android Adding Components

于 2017-11-11T07:28:48.453 回答
0

As of July 2019 if you wish to use Room with Kotlin, AndroidX, Coroutines or RxJava add lines below.

   // 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
于 2019-07-28T11:42:10.100 回答
0

Bouncing off @Thracian's answer, here's what I had to do while following this documentation:

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'

Also within android {} I had to add:

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

Despite what I've read, with Kotlin you still must use annotationProcessor

于 2019-07-28T17:16:32.270 回答
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"
于 2020-01-13T14:29:37.963 回答