Android Jetpack (1): Introduction to Jetpack

Jetpack official website

Android Jetpack

In 2018, Google I/O released a series of practical tools to assist android developers, collectively known as Jetpack, to help developers build excellent Android applications. Android Jetpack is perfectly compatible with the Kotlin language, and using Android KTX can greatly save the amount of code.

Some components in Jetpack are not launched for the first time. Among them, LifeCycle, LiveData, ViewModel, Room and other components were launched along with Android Architecture Component (AAC) at the Google I/O 2017 conference, but the promotion effect is average. A year later, Google released Jetpack on the basis of AAC, and released other tools to solve the problems of Android technology selection disorder and irregular development.

Jetpack is a set of libraries, tools, and guides that help developers write quality applications more easily. These components can help you follow best practices, free you from writing boilerplate code, and simplify complex tasks so you can focus on the code you need.

Jetpack includes the androidx.* package library unbundled with the platform API. This means that it can provide backward compatibility and is updated more frequently than the Android platform to ensure that you always get the latest and greatest version of Jetpack components.

Now in the Google app market, most apps have begun to use Android Jetpack. Through Android Jetpack, we can reduce our BUG and let us focus more on building the application itself.

Composition of JetPack

Jetpack components cover the following 4 areas: Architecture, Foundation, Behavior and UI.

  • Architecture : Architecture components can help you design applications that are robust, testable, and easy to maintain.
  • Foundation (foundation) : Foundation components can provide horizontal functions, such as backward compatibility, testing, and Kotlin language support.
  • Behavior : Behavior components help your app integrate with standard Android services (such as notifications, permissions, sharing, and Google Assistant).
  • UI (Interface) : Interface components can provide widgets and auxiliary programs, making your application not only easy to use, but also a pleasant experience.

Insert picture description here

Insert picture description here

Insert picture description here

Why use Android Jetpack?

1. Rapid development

Components can be used individually (but these components are built for collaborative work), while using Kotlin language features to help you improve work efficiency.

2. Eliminate boilerplate code

Android Jetpack can manage tedious activities (such as background tasks, navigation, and lifecycle management) so that you can focus on how to make your app stand out.

3. Build high-quality powerful applications

Android Jetpack components are built around modern design practices and are backward compatible to reduce crashes and memory leaks.

Insert picture description here

Architecture (architecture component)

Architecture components can help developers design applications that are robust, testable, and easy to maintain. It contains the following component libraries:

  • Data Binding: The data binding library is a support library. With this library, the interface components in the layout can be bound to the data source in the application in a declarative manner.
  • Lifecycles: It is convenient to manage the life cycle of Activity and Fragment, helping developers write lighter and easier to maintain code.
  • ViewModel: Store and manage UI-related data in a life cycle aware manner.
  • LiveData: is an observable data holder class. Unlike regular observables, LiveData is lifecycle aware.
  • Navigation: Handles everything needed for in-app navigation.
  • Room: Room persistence library provides an abstraction layer on SQLite to help developers access SQLite databases more friendly and smoothly.
  • Paging: Help developers load and display small pieces of data at once. Loading some data on demand can reduce network bandwidth and system resource usage.
  • WorkManager: Even if the application exits or the device restarts, you can easily schedule the delayed asynchronous tasks that are expected to run.

Foundation (basic component)

The basic components provide horizontal functions, such as backward compatibility, testing, and Kotlin language support. It contains the following component libraries:

  • Android KTX: Android KTX is a set of Kotlin extensions that optimize the Jetpack and Android platform APIs for Kotlin. Use Kotlin for Android development in a more concise, pleasant and idiomatic way.
  • AppCompat: Provides a series of APIs starting with AppCompat to be compatible with lower versions of Android development.
  • Cars(Auto): Components that help develop Android Auto applications without worrying about vehicle-specific hardware differences (such as screen resolution, software interface, knobs, and touch controls).
  • Benchmark: Quickly benchmark and analyze 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 applications.

Behavior

Behavioral components help developers’ applications to integrate with standard Android services such as notifications, permissions, sharing, and Google Assistant. It contains the following component libraries:

  • 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 after 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): Provide users with the ability to change the application's functions and behavior.
  • Sharing (sharing): Provides a sharing operation suitable for the application operation bar.
  • Slices: Create flexible interface elements that can display application data outside the application.

UI (interface components)

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: Provide various built-in animations, and you can also customize animation effects.
  • Emoji (emoticons): Allow users to use emoticons without updating the system version.
  • Fragment: The basic unit of componentized interface.
  • Layout (layout): interface layout written in xml or completed using Compose.
  • Palette: extract useful information from the palette.

AAC Architecture (Android Archicture Components): Google’s recommended application architecture

Insert picture description here

Jetpack pros and cons

Insert picture description here

Jetpack and AndroidX

Jetpack is the collective name of various component libraries, and AndroidX is the unified package name of these components.

AndroidX has made significant improvements to the original Android Support Library, which is no longer maintained. The androidx package completely replaces the support package, not only providing equivalent functionality, but also providing new libraries. The package name starting with androidx is also completely used in Jetpack components.

Like the Support Library, the libraries in the androidx namespace are provided separately from the Android platform and are backward compatible with various Android versions.

note:

  1. All packages in AndroidX use a consistent namespace, starting with the string androidx. The Support Library package has been mapped to the corresponding androidx.* package.
  2. Unlike the Support Library, the androidx package will be maintained and updated separately. Starting from version 1.0.0, the androidx package uses strict semantic version control. You can update each AndroidX library in the project individually.
  3. Version 28.0.0 is the last version of the Support Library. The android.support library version will no longer be released in the future. All new features will be developed in the androidx namespace.

Use Jetpack library in application

All Jetpack components can be found in the Google Maven repository.

Currently Jetpack components are included in AndroidX, and AndroidX is the specific location of the support library included in Jetpack, so if the project and migration to AndroidX, no additional guide package is needed. If it is not compatible with AndroidX, you need to configure it in gradle:

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:

dependencies {
    
    
    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 and take full advantage of the unique language features of Kotlin.

Guess you like

Origin blog.csdn.net/qq_36699930/article/details/102515750