How to import Room Persistence Library to an Android project

48,932

Solution 1

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'

Solution 2

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

Solution 3

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

Solution 4

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"

Solution 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"
Share:
48,932
Francisco Durdin Garcia
Author by

Francisco Durdin Garcia

Android developer for @BQreaders. "If it were easy, everybody would do it"

Updated on September 07, 2020

Comments

  • Francisco Durdin Garcia
    Francisco Durdin Garcia almost 4 years

    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?