Jetpack is a suite of libraries (not a single component library)

Why use Android Jetpack?

  • Follow best Practices

The Android Jetpack component is built with the latest design approach and is backward compatible to reduce crashes and memory leaks

  • Eliminate boilerplate code

Android Jetpack manages tedious activities such as background tasks, navigation, and lifecycle management so that you can focus on building distinctive applications.

  • Reduce inconsistency

These libraries work in a consistent manner across Android versions and devices, helping you reduce complexity.

Jetpack classification

As you can see from the figure above, the JetPack component library is divided into four partsArchitecture, UI, Behavior, and Foundation. Each component can be used individually or in combination. Among them, DataBinding, LiveData, Paging, Room, ViewModel and Lifecycles are widely used, which are also the focus of the following article.

  • Architecture component: provides an easy-to-use framework with robust, testable, and maintainable applications.

DataBinding, ViewModel, LiveData, Lifecycle…

  • Basic components: provides compatibility, testing, security, Kotlin language support, and more.

Android KTX, Test…

  • Behavior component: Provides Android services, notifications, permissions, sharing, etc.

Permissions, Notifications, CameraX…

  • Interface components: provide pages, layouts, animations, etc.

Fragment, Emoji, Palette…

According to the introduction on the official website, Jetpack currently has a total of 85 component libraries. Some of them look very familiar, such as viewPager, Fragment, recyclerView, etc. But some of them seem to have never seen or used before.

Architecture(Architecture component)

  • Data Binding: A Data Binding library is a support library that allows you to declaratively bind interface components in a layout to Data sources in an application.

  • Lifecycles: Easy to manage Activity and Fragment Lifecycles, helping developers write lighter, easier to maintain code.

  • LiveData: is an observable data holder class. Unlike regular Observables, LiveData is lifecycle aware.

  • Navigation: Handles everything needed for in-app Navigation.

  • Paging: Helps developers load and display small pieces of data at once. Loading partial data on demand reduces network bandwidth and system resource usage.

  • Room: The Room persistence library provides an abstraction layer on TOP of SQLite to help developers access SQLite databases in a friendly and smooth way.

  • ViewModel: Stores and manages UI-related data in a life-cycle-aware manner.

  • WorkManager: It is easy to schedule deferred asynchronous tasks that are expected to run even if the application exits or the device is restarted.

Use the Jetpack

All Jetpack components are available in the Google Maven code base.

Check out the latest version of the Jetpack library

Open your project’s build.gradle file and add the Google () codebase, as shown below

allprojects {
    repositories {
        google()
        jcenter()
    }
}
Copy the code

You can then add the Jetpack component to build.gradle under the APP directory, as shown below:

Databinding

The data binding library comes bundled with the Android Gradle plugin. You do not need to declare a dependency on this library, but you must enable it.

To enable dataBinding, set the dataBinding build option to true in the build.gradle file of the module, as shown below:

android {
    ...
    buildFeatures {
        dataBinding true}}Copy the code

Note: Even if a module does not use data binding directly, data binding must be enabled for all modules that depend on libraries that use data binding.

Lifecycles+ViewModel+LiveData

The API in lifecycle- Extensions is deprecated. You can add the required dependencies for a particular Lifecycle artifact.

Kotlin

    dependencies {
        val lifecycle_version = "2.4.0 - alpha02"
        val arch_version = "2.1.0."

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

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

        // Annotation processor
        kapt("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-ktx:$lifecycle_version")

        // optional - Test helpers for LiveData
        testImplementation("androidx.arch.core:core-testing:$arch_version")}Copy the code

Java

    dependencies {
        val lifecycle_version = "2.4.0 - alpha02"
        val 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")}Copy the code

Navigation

dependencies {
  val nav_version = "2.3.5"

  // Java language implementation
  implementation("androidx.navigation:navigation-fragment:$nav_version")
  implementation("androidx.navigation:navigation-ui:$nav_version")

  // Kotlin
  implementation("androidx.navigation:navigation-fragment-ktx:$nav_version")
  implementation("androidx.navigation:navigation-ui-ktx:$nav_version")

  // Feature module Support
  implementation("androidx.navigation:navigation-dynamic-features-fragment:$nav_version")

  // Testing Navigation
  androidTestImplementation("androidx.navigation:navigation-testing:$nav_version")

  // Jetpack Compose Integration
  implementation("Androidx. Navigation: navigation - compose: 2.4.0 - alpha03")}Copy the code

Paging

dependencies {
  val paging_version = "3.0.0"

  implementation("androidx.paging:paging-runtime:$paging_version")

  // alternatively - without Android dependencies for tests
  testImplementation("androidx.paging:paging-common:$paging_version")

  // optional - RxJava2 support
  implementation("androidx.paging:paging-rxjava2:$paging_version")

  // optional - RxJava3 support
  implementation("androidx.paging:paging-rxjava3:$paging_version")

  // optional - Guava ListenableFuture support
  implementation("androidx.paging:paging-guava:$paging_version")

  // optional - Jetpack Compose integration
  implementation("Androidx. The paging: the paging - compose: 1.0.0 - alpha11")}Copy the code

Room

dependencies {
    def room_version = "2.3.0"

    implementation("androidx.room:room-runtime:$room_version")
    annotationProcessor "androidx.room:room-compiler:$room_version"

    // To use Kotlin annotation processing tool (kapt)
    kapt("androidx.room:room-compiler:$room_version")
    // To use Kotlin Symbolic Processing (KSP)
    ksp("androidx.room:room-compiler:$room_version")

    // optional - Kotlin Extensions and Coroutines support for Room
    implementation("androidx.room:room-ktx:$room_version")

    // optional - RxJava2 support for Room
    implementation "androidx.room:room-rxjava2:$room_version"

    // optional - RxJava3 support for Room
    implementation "androidx.room:room-rxjava3:$room_version"

    // optional - Guava support for Room, including Optional and ListenableFuture
    implementation "androidx.room:room-guava:$room_version"

    // optional - Test helpers
    testImplementation("androidx.room:room-testing:$room_version")}Copy the code

Configure compiler options

Room has the following annotation processor options:

  • Room.schemalocation: Configures and enables the ability to export database schemas to JSON files in a given directory. For more details, see Room Migration.
  • Room. incremental: Enable Gradle incremental annotation processors.
  • Room.expandprojection: Configure Room to rewrite the query so that its top star projection, when expanded, contains only columns defined in the DAO method return type.

The following code snippet illustrates how to configure these options:

android {
    ...
    defaultConfig {
        ...
        javaCompileOptions {
            annotationProcessorOptions {
                arguments += mapOf(
                    "room.schemaLocation" to "$projectDir/schemas"."room.incremental" to "true"."room.expandProjection" to "true")}}}}Copy the code

WorkManager

dependencies {
    val work_version = "2.5.0"

    // (Java only)
    implementation("androidx.work:work-runtime:$work_version")

    // Kotlin + coroutines
    implementation("androidx.work:work-runtime-ktx:$work_version")

    // optional - RxJava2 support
    implementation("androidx.work:work-rxjava2:$work_version")

    // optional - GCMNetworkManager support
    implementation("androidx.work:work-gcm:$work_version")

    // optional - Test helpers
    androidTestImplementation("androidx.work:work-testing:$work_version")

    // optional - Multiprocess support
    implementation "androidx.work:work-multiprocess:$work_version"
}
Copy the code

To learn how to use the Kotlin extension, refer to the KTX documentation.

For more information about dependencies, see Adding build Dependencies.