该系列文章是对Android推出的架构组件相关文章,按作者自己理解来翻译的,同时标记有作者自己一些简单笔记。如果读者发现文中有翻译不准确的地方,或者理解错误的地方,请不吝指教。
源自Google官方 Adding Components to your Project 一文的翻译与归纳
其他相关链接:
Android Jetpack Components
[TOC]
前言
在开始前,我们建议先阅读 Guide to App Architecture 一文。该指南包含一些适用于所有Android应用的原则,同时展示了如何使用结合使用框架组件。
添加 Google Maven 库
打开 app 或 module 的 build.gradle
文件,添加所需依赖库。你可以选择添加所有依赖库,也可以只选择一部分。
Futures
依赖 Futures:
dependencies {
def futures_version = "1.0.0-alpha02"
implementation "androidx.concurrent:concurrent-futures:$futures_version"
}
Lifecycle
依赖 LifeCycle,包括 LiveData 和 ViewModel。
AndroidX
dependencies {
def lifecycle_version = "2.0.0"
// 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"
}
AndroidX 之前
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"
}
Room
依赖 Room,包括 testing Room migrations 和 Room RxJava。
AndroidX
dependencies {
def room_version = "2.1.0-alpha02"
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"
}
AndroidX 之前
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"
}
Paging
依赖 Paging
AndroidX
dependencies {
def paging_version = "2.1.0-beta01"
implementation "androidx.paging:paging-runtime:$paging_version" // use -ktx for Kotlin
// alternatively - without Android dependencies for testing
testImplementation "androidx.paging:paging-common:$paging_version" // use -ktx for Kotlin
// optional - RxJava support
implementation "androidx.paging:paging-rxjava2:$paging_version" // use -ktx for Kotlin
}
AndroidX 之前
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
implementation "android.arch.paging:rxjava2:$paging_version"
}
Navigation
依赖 Navigation
Navigation 类已经在 androidx.navigation 包中,但目前依赖于 Support Library 27.1.1 和相关框架组件版本。未来将发布依赖于 AndroidX 的 Navigation 版本
dependencies {
def nav_version = "1.0.0-alpha07"
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
// this library depends on the Kotlin standard library
androidTestImplementation "android.arch.navigation:navigation-testing:$nav_version"
}
Safe args
对于 Safe args,在 Project 的build.gradle
文件中添加以下classpath
buildscript {
repositories {
google()
}
dependencies {
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha07"
}
}
同时在你的 app 或者 module 的build.gradle
中添加
apply plugin: "androidx.navigation.safeargs"
WorkManager
依赖 WorkManager
WorkManager 类已经在 androidx.work 包中,但目前依赖于 Support Library 27.1.1 和相关框架组件版本。未来将发布依赖于 AndroidX 的 Navigation 版本
WorkManager 需要 compileSdk
版本大于等于28
dependencies {
def work_version = "1.0.0-alpha11"
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
网友评论