【译】Google官方推出的Android架构组件系列文章(二)将Architecture Components引入工程

PS: 2018.06.24按照官网最新文档更新本文翻译

系列文章导航

  1. 【译】Google官方推出的Android架构组件系列文章(一)App架构指南
  2. 【译】Google官方推出的Android架构组件系列文章(二)将Architecture Components引入工程
  3. 【译】Google官方推出的Android架构组件系列文章(三)处理生命周期
  4. 【译】Google官方推出的Android架构组件系列文章(四)LiveData
  5. 【译】Google官方推出的Android架构组件系列文章(五)ViewModel
  6. 【译】Google官方推出的Android架构组件系列文章(六)Room持久化库

原文地址:https://developer.android.com/topic/libraries/architecture/adding-components.html

Architecture Components可以从Google的Maven仓库获得。按照下面的几个步骤来使用:

添加Google Maven仓库

Android Studio默认没有配置成可访问这个仓库。

要把它加到你的工程里,需要打开工程的根build.gradle文件(不是app或模块的那个),然后添加下面高亮的那行:

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

注意:https://maven.google.com 国内可能访问不通,可尝试替换为 https://dl.google.com/dl/android/maven2/

声明依赖

打开你的app或者模块的build.gradle文件,添加你需要的库作为依赖。你可以添加所有依赖,或者取一个子集。

AndroidX

架构组件的未来版本正作为AndroidX的一部分来进行开发,并且列在每个稳定依赖下。alpha版本的库名字已经遵循AndroidX的标准发生了变更。

有关AndroidX重构的更多信息,以及其如何影响类包和模块ID,请参阅AndroidX重构文档

Kotlin

某些标记有// use -ktx for Kotlin的AndroidX依赖库支持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

对于 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"
    // 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-alpha1"

    // 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"
    // 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的依赖, 包括testing Room migrations 和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"

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

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

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

    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的依赖。

Navigation已经在androidx.navigation包中,但目前依赖于Support Library 27.1.1和相关的Arch组件版本。AndroidX版本的Navigation版本将在未来发布。

dependencies {
    def nav_version = "1.0.0-alpha02"

    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
}

Safe args
针对 Safe args, 将下面的classpath添加到你的顶级build.gradle文件中

buildscript {
    repositories {
        google()
    }
    dependencies {
        classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha02"
    }
}

然后在应用或模块的build.gradle文件中添加:

apply plugin: "androidx.navigation.safeargs"

WorkManager

针对 WorkManager的依赖。

WorkManager类已经在androidx.work package包中,但目前依赖于Support Library 27.1.1和相关的Arch组件版本。AndroidX版本的WorkManager版本将在未来发布。

dependencies {
    def work_version = "1.0.0-alpha03"

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

更多信息,参见 Add Build Dependencies

你可能感兴趣的:(【译】Google官方推出的Android架构组件系列文章(二)将Architecture Components引入工程)