Jetpack application architecture guide

0. Preface

Android has been in development for more than ten years, and it can be said to be a relatively mature technology. At the beginning, there were few frameworks and no specifications. All the code must be written by yourself, such as network requests, database operations, data analysis, and so on. Later, some frameworks appeared to help developers develop quickly, such as XUtils, Volley, OKHttp, EventBus, etc. With more and more frameworks, an application can have multiple technical options, which directly leads to more and more irregular application development. , Resulting in uneven application quality, which is obviously not what Google wants to see. Google subsequently launched official examples related to MVP and MVVM, the effect is very general, and the coverage is only in the architecture, the Android Jetpack launched at the Goole I/O 2018 conference is expected to solve the above problems

Some components in Android Jetpack are not launched for the first time. Among them, LifeCycle, LiveData, ViewModel, Room and other components were launched with Android Architecture Component (AAC) as early as Google I/O 2017 conference. In Goole I/ At the O 2018 conference, the Android Support Library was replaced by AndroidX, and Android Jetpack was released on the basis of Android Architecture Component. AndroidX also belongs to Android Jetpack.

Jetpack has the following characteristics

  • Reduce inconsistencies : These libraries work in a consistent manner across various Android versions and devices, helping you reduce complexity.
  • Eliminate boilerplate code : Jetpack can manage tedious activities (such as background tasks, navigation, and life cycle) so that you can focus on creating great apps.
  • Follow best practices : Android Jetpack components are built with the latest design methods, are backward compatible, and can reduce crashes and memory leaks

1.Android Jetpack classification

Android Jetpack is divided into four major parts: Architecture, UI, Foundationy and Behavior. As time increases, the Android team has added many more components to Jetpack. The latest layout is as follows.

1-1. Architecture Architecture Components

Jetppack is intended to help our developers speed up development, allowing us to focus on our own business instead of spending a lot of time to do some trivial work such as compatibility.

Architecture architecture component modules have good compatibility, and each component in the architecture component can help us cope with a certain type of problem/pain point

 

Lifecycle

It can help us easily deal with Activity/Fragment life cycle issues, allow us to deal with life cycle changes in a more decoupled way, and easily avoid memory leaks

LiveData

Notify the view when the underlying database changes. Data holding classes based on the observer pattern and life cycle perception can help us better decouple and process data. Unlike regular observables, LiveData is life cycle aware

ViewModel

Manage interface-related data in a life cycle manner, usually in conjunction with DataBinding, to provide strong support for developers to implement the MVVM architecture.

 

Data Binding Declaratively bind observable data to interface elements, usually used in conjunction with ViewModel

Room

Friendly and smooth access to SQLite database. It provides an abstraction layer based on SQLite, allowing more powerful database access

WorkManager

Provide a one-stop solution for us to perform background tasks

Navigation

Can help us build a single Activity application more conveniently and handle in-app navigation

Paging

It can help developers load and display small pieces of data at a time, and load some data on demand to reduce the use of network bandwidth and system resources

While launching the architecture components, Android also recommends an architecture suitable for Android applications. The components are organized as shown in the figure below.

Each component pays attention to its own affairs and does not interfere with each other, making our applications more decoupled and clear responsibilities

Please note that each component only depends on the components at the next level. For example, Activity and Fragment only depend on the view model. The storage area is the only class that depends on multiple other classes; in this case, the storage area depends on the persistent data model and the remote back-end data source

1-2. Foundationy (Basic Components)

The basic components provide horizontal functions, such as backward compatibility, testing, security, Kotlin language support, and it includes the following component libraries

  • Android KTX : is a set of Kotlin extensions that optimize Jetpack and Android platform APIs for Kotlin. Help developers to use Kotlin for Android development in a more concise, more pleasant, and more idiomatic way.
  • AppCompat : Provides a series of APIs starting with AppCompat to be compatible with lower versions of Android development.
  • Auto : A component that helps develop Android Auto applications, provides a standardized interface and user interaction for all vehicles, and developers don’t need to worry about vehicle-specific hardware differences (such as screen resolution, software interface, knobs, and touch controls) .
  • Benchmark : Quickly benchmark Kotlin or Java-based code from Android Studio. Measure code performance and output the benchmark analysis results to the Android Studio console.
  • Multidex (multi -dex processing) : Enable multi-dex files for applications with more than 64K methods.
  • Security : Follow security best practices to read and write encrypted files and share preferences.
  • Test : An Android testing framework for unit and runtime interface testing.
  • TV : Build applications that allow users to experience immersive content on the big screen.
  • Wear OS : Components that help develop Wear apps

1-3. Behavior

Behavioral components help developers’ applications to integrate with standard Android services (such as notifications, permissions, sharing)

  • CameraX : Help developers simplify the development of camera applications. It provides a consistent and easy-to-use API interface, suitable for most Android devices, and is backward compatible to Android 5.0 (API level 21).
  • DownloadManager (download manager) : can handle long-running HTTP downloads, and retry downloads in the event of a failure or connection change and system restart.
  • Media & playback : Backward compatible API for media playback and routing (including Google Cast).
  • Notifications : Provides a backward compatible notification API, supports Wear and Auto.
  • Permissions : Compatibility API for checking and requesting application permissions.
  • Preferences (Preferences) : Provides users with the ability to change the function and behavior of the application.
  • Sharing (sharing) : Provides a sharing operation suitable for the application operation bar.
  • Slices : A slice is a UI template that creates flexible interface elements that can display application data outside the application

1-4.UI (interface components)

The interface components can provide various Views and auxiliary programs, making the application not only easy to use, but also a pleasant experience. It contains the following component libraries

  • Animation & Transitions : This framework contains built-in animations for common effects and allows developers to create custom animations and life cycle callbacks.
  • Emoji (emoticons) : Allow users to get the latest emoticons without updating the system version.
  • Fragment : The basic unit of componentized interface.
  • Layout : The interface layout written in xml or the interface completed using Compose.
  • Palette : extract useful information from the palette

2. Introduce Jetpack related components into the application

All components are available in Jetpack  Google Maven code base found in

Open the build.gradle file of the project and add the google() code library as shown below

allprojects {
    repositories {
        google()
        jcenter()
    }
}

Then, you can add Jetpack components (such as architectural components such as LiveData and ViewModel) as follows:

    def lifecycle_version = "2.2.0"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"

Many Jetpack libraries provide  Android KTX extensions , as shown in lifecycle-livedata-ktx and lifecycle-viewmodel-ktx above. KTX extensions are built on the basis of Java-based APIs, making full use of the unique language features of Kotlin.

Based on Kotlin  and Java-based  API reference documentation page applies to all Jetpack gallery

3. Summary

Obviously, architectural components are of great practical value for us, we must understand and learn them

App practical guide: https://github.com/googlesamples/android-sunflower

developers:https://developer.android.google.cn/jetpack/getting-started?hl=zh-cn

Architecture components: https://developer.android.google.cn/topic/libraries/architecture?hl=zh-cn

 

Guess you like

Origin blog.csdn.net/PrisonJoker/article/details/113966093