0%

Android-Jetpack组件概述

概述

Jetpack是一系列的工具库,可帮助开发者更轻松地编写优质应用。这些组件可帮助您遵循最佳做法、让您摆脱编写样板代码的工作并简化复杂任务,以便您将精力集中放在所需的代码上。Jetpack包含与平台API解除捆绑的androidx.*软件包库。这意味着,它可以提供向后兼容性,且比Android平台的更新频率更高,以此确保您始终可以获取最新且最好的Jetpack组件版本。

官方介绍

JetPack是一系列库和工具的集合,它更多是Google的一个提出的一个概念,或者说态度。其组成部分如下:

JetPack组成

根据官网的说法,应用了这些组件后,一个应用的架构应该如下所示:

final-architecture

依赖

androidx.lifecycle

lifecycle依赖项,目前最新版本为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
dependencies {
def lifecycle_version = "2.2.0"
def arch_version = "2.1.0"

// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
// Lifecycles only (without ViewModel or LiveData)
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

// Saved state module for ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"

// Annotation processor
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 - helpers for implementing LifecycleOwner in a Service
implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"

// optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"

// optional - ReactiveStreams support for LiveData
implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version"

// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$arch_version"
}

总结

相关使用和原理参考:

结合上述文章,将这些组件整合如下图:

Android-Jetpack组件