Adding Components to your Project
Architecture Components are available from Google's Maven repository. To use them, follow these steps:
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() google() } }
Add Architecture Components
Open the build.gradle
file for your app or module and add the artifacts that you need as dependencies. You can add all dependencies, or choose a subset.
Note: For Kotlin based apps, make sure you use kapt
instead of annotationProcessor
. You should also add the kotlin-kapt
plugin.
Main Dependencies
Includes Lifecycles, LiveData, ViewModel, Room, and Paging.
It also includes test helpers for testing LiveData as well as testing Room migrations.
dependencies {
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.1.1"
// alternatively, just ViewModel
implementation "android.arch.lifecycle:viewmodel:1.1.1"
// alternatively, just LiveData
implementation "android.arch.lifecycle:livedata:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
// Room (use 1.1.0-beta3 for latest beta)
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
// Paging
implementation "android.arch.paging:runtime:1.0.0-rc1"
// Test helpers for LiveData
testImplementation "android.arch.core:core-testing:1.1.1"
// Test helpers for Room
testImplementation "android.arch.persistence.room:testing:1.0.0"
}
Java 8 Support for Lifecycles
If your app uses Java 8, we recommend using this library instead of android.arch.lifecycle:compiler
.
dependencies {
// Java8 support for Lifecycles
implementation "android.arch.lifecycle:common-java8:1.1.1"
}
Optional Dependencies for RxJava and ReactiveStreams
Adds optional support for Room RxJava and LiveData ReactiveStreams.
dependencies {
// RxJava support for Room (use 1.1.0-beta3 for latest beta)
implementation "android.arch.persistence.room:rxjava2:1.0.0"
// ReactiveStreams support for LiveData
implementation "android.arch.lifecycle:reactivestreams:1.1.1"
// RxJava support for Paging
implementation "android.arch.paging:rxjava2:1.0.0-alpha1"
}
Optional Dependencies for Guava
Adds support for Guava's Optional
and ListenableFuture
types in Room @Dao
queries.
dependencies {
// Guava support for Room
implementation "android.arch.persistence.room:guava:1.1.0-beta3"
}
Alternative import for lightweight Lifecycles only
Support library depends on this lightweight import. It provides Lifecycles without LiveData or ViewModel.
dependencies {
// Lifecycles only (no ViewModel or LiveData)
implementation "android.arch.lifecycle:runtime:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
}
For more information, see Add Build Dependencies.