早在去年Architecture Components刚出来的时候,就简单的翻译过这些内容.隔了这么久了,东西也有不少的变化,自己用的也少,但是看形势感觉大势所趋的样子,因此好好的在学一下。要学习Architecture Component,的先学会如何使用它,进而才能去理解并深入。所以现在还是第一个阶段——使用阶段.本文大部分内容基本都是翻译。您也可以直接去官网直接看原文。本篇介绍Adding Components。
1. 添加谷歌Maven仓库
打开项目根目录中的build.gradle文件,添加google()
如下所示:
allprojects {
repositories {
jcenter()
google()
}
}
2. 声明依赖
在你的app或者module中的build.gradle文件中添加组件依赖。你可以添加所有的依赖或者只是选择其中的一部分。
1. AndroidX
后面,此结构组件将作为AndroidX的一部分,现在一些组件的名字在alpha 版本中也已经有些改变,在下面列出了一些稳定的版本。
2. Kotlin
Kotlin的拓展模块添加几个依赖关系的标记,使用-ktx结尾。例如:
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" // use -ktx for Kotlin
替换成
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
小贴士:基于kotlin的app,应该使用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.lifecycl: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.0"
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 alpha
implementation "android.arch.paging:rxjava2:1.0.0-alpha1"
}
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依赖,AndroidX的版本还未发布
dependencies {
def nav_version = "1.0.0-alpha01"
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, 在你的 top level build.gradle
文件中,添加 classpath。
buildscript {
repositories {
google()
}
dependencies {
classpath "androidx.navigation:safe-args-gradle-plugin:1.0.0-alpha01"
}
}
在你app或者module的build.gradle文件中
apply plugin: 'androidx.navigation.safeargs'
WorkManager
WorkManager的依赖,AndroidX版本将在以后发布。
dependencies {
def work_version = "1.0.0-alpha01"
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"
}