在开始之前,我们建议您阅读“架构组件指南”。 该指南包含一些适用于所有Android
应用程序的有用原则,并展示了如何将架构组件一起使用。
架构组件可从Google
的Maven
存储库获得。 要使用它们,请按照下列步骤操作:
一、添加Google Maven存储库
默认情况下,Android Studio项目未配置为访问此存储库。
要将其添加到项目中,请打开项目的build.gradle
文件(而不是应用程序或模块的文件)并添加google()存储库,如下所示:
allprojects {
repositories {
jcenter()
google()
}
}
二、声明依赖项
打开应用程序或模块的build.gradle文件,并添加所需的工件作为依赖项。 您可以添加所有依赖项,也可以选择子集。
AndroidX
Arch
组件的未来版本正在作为AndroidX的一部分开发,并列在每个稳定依赖项下面。 这些alpha版本的工件名称已更改为遵循AndroidX标准。
有关AndroidX重构的更多信息,以及它如何影响类包和模块ID,请参阅AndroidX重构文档。
Kotlin
Kotlin扩展模块支持多个AndroidX依赖项,标记为// use -ktx for Kotlin
,只需替换例如:
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" // use -ktx for Kotlin
同
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
更多信息,包括Kotlin扩展的文档,可以在ktx文档中找到。
注意:对于基于Kotlin的应用程序,请确保使用kapt而不是annotationProcessor。 你还应该添加kotlin-kapt插件。
三、Lifecycle
生命周期的依赖关系,包括LiveData和ViewModel。
dependencies {
def lifecycle_version = "1.1.1"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
// alternatively - just ViewModel
implementation "android.arch.lifecycle:viewmodel:$lifecycle_version" // use -ktx for Kotlin
// alternatively - just LiveData
implementation "android.arch.lifecycle:livedata:$lifecycle_version"
// alternatively - Lifecycles only (no ViewModel or LiveData).
// Support library depends on this lightweight import
implementation "android.arch.lifecycle:runtime:$lifecycle_version"
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" // use kapt for Kotlin
// alternately - if using Java8, use the following instead of compiler
implementation "android.arch.lifecycle:common-java8:$lifecycle_version"
// optional - ReactiveStreams support for LiveData
implementation "android.arch.lifecycle:reactivestreams:$lifecycle_version"
// optional - Test helpers for LiveData
testImplementation "android.arch.core:core-testing:$lifecycle_version"
}
AndroidX
dependencies {
def lifecycle_version = "2.0.0-beta01"
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
// alternatively - just ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" // use -ktx for Kotlin
// alternatively - just LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
// alternatively - Lifecycles only (no ViewModel or LiveData). Some UI
// AndroidX libraries use this lightweight import for Lifecycle
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // use kapt for Kotlin
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
// optional - ReactiveStreams support for LiveData
implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version" // use -ktx for Kotlin
// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
}
四、Room
Room的依赖关系,包括测试Room迁移和Room RxJava
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"
}
AndroidX
dependencies {
def room_version = "2.0.0-beta01"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" // use kapt for Kotlin
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// Test helpers
testImplementation "androidx.room:room-testing:$room_version"
}
五、Paging
Paging的依赖性
dependencies {
def paging_version = "1.0.0"
implementation "android.arch.paging:runtime:$paging_version"
// alternatively - without Android dependencies for testing
testImplementation "android.arch.paging:common:$paging_version"
// optional - RxJava support, currently in release candidate
implementation "android.arch.paging:rxjava2:1.0.0-rc1"
}
AndroidX
dependencies {
def paging_version = "2.0.0-beta01"
implementation "androidx.paging:paging-runtime:$paging_version"
// alternatively - without Android dependencies for testing
testImplementation "androidx.paging:paging-common:$paging_version"
// optional - RxJava support
implementation "androidx.paging:paging-rxjava2:$paging_version"
}
六、Navigation
导航的依赖关系。
Navigation类已经在androidx.navigation包中,但目前依赖于Support Library 27.1.1和相关的Arch组件版本。 具有AndroidX依赖关系的导航版本将在未来发布。
dependencies {
def nav_version = "1.0.0-alpha05"
implementation "android.arch.navigation:navigation-fragment:$nav_version" // use -ktx for Kotlin
implementation "android.arch.navigation:navigation-ui:$nav_version" // use -ktx for Kotlin
// optional - Test helpers
androidTestImplementation "android.arch.navigation:navigation-testing:$nav_version" // use -ktx for Kotlin
}
安全的args
对于Safe args,请在顶级build.gradle文件中添加以下类路径
buildscript {
repositories {
google()
}
dependencies {
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha05"
}
}
并在您的应用程序或模块build.gradle文件中
apply plugin: "androidx.navigation.safeargs"
七、WorkManager
WorkManager的依赖关系。
WorkManager类已经在androidx.work包中,但目前依赖于Support Library 27.1和相关的Arch组件版本。 具有AndroidX依赖关系的WorkManager版本将在未来发布。
WorkManager需要compileSdk版本28或更高版本。
dependencies {
def work_version = "1.0.0-alpha07"
implementation "android.arch.work:work-runtime:$work_version" // use -ktx for Kotlin
// optional - Firebase JobDispatcher support
implementation "android.arch.work:work-firebase:$work_version"
// optional - Test helpers
androidTestImplementation "android.arch.work:work-testing:$work_version"
}