1. What are the Android Architecture Components?

The Android Architecture component is a set of libraries that help you design robust, testable, and maintainable applications. Start with classes that manage the LIFE cycle of UI components and handle data persistence.

  • DataBinding is a support library that allows you to bind UI components in your layout to data sources in your application using declarative personalities rather than programmatically.
  • Easily manage the application lifecycle. A new lifecycle awareness component helps you manage the activity and fragment life cycles. Survive configuration changes, avoid memory leaks and easily load data into the UI.
  • LiveData is used to build data objects to notify the view when the underlying database changes.
  • The ViewModel stores UI-related data that is not destroyed in the application rotation.
  • Room is an SQLite object mapping library. Use it to avoid boilerplate code and easily convert SQLite table data into Java objects. Room provides compile-time checking of SQLite statements, returning RxJava, Flowable, and LiveData Observable.
  • Navigation is an interaction that allows users to navigate, import, and exit different content within the app.
  • Paging Library helps you load and display small pieces of data at once. Loading partial data on demand reduces network bandwidth and system resource usage.
  • WorkManager can easily schedule deferred asynchronous tasks that can run even if the application exits or the device is restarted.

The new component architecture was also introduced at Google I/O, and can be found at this link.

2. Add components to the project

After a brief introduction to these components, it is natural to introduce them into our project. These are all official Google components. Of course, you need to first introduce Google () in your project build.gradle file, like this:

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

For LifeCycles and ViewModels and LiveData, just make a choice based on your needs:

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" // For Kotlin use lifecycle-viewmodel-ktx
    // 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" // For Kotlin use kapt instead of annotationProcessor
    // 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" // For Kotlin use lifecycle-reactivestreams-ktx

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

To use dataBinding, add the following configuration to your app’s build.gradle:

android {
    ...
    dataBinding {
        enabled = true}}Copy the code

Navigation requires declaration dependencies:

dependencies {
    def nav_version = "2.1.0 - alpha02"

    implementation "androidx.navigation:navigation-fragment:$nav_version" // For Kotlin use navigation-fragment-ktx
    implementation "androidx.navigation:navigation-ui:$nav_version" // For Kotlin use navigation-ui-ktx
}
Copy the code

Paging requires declarative dependencies:

dependencies {
    def paging_version = "2.1.0."

    implementation "androidx.paging:paging-runtime:$paging_version" // For Kotlin use paging-runtime-ktx

    // alternatively - without Android dependencies for testing
    testImplementation "androidx.paging:paging-common:$paging_version" // For Kotlin use paging-common-ktx

    // optional - RxJava support
    implementation "androidx.paging:paging-rxjava2:$paging_version" // For Kotlin use paging-rxjava2-ktx
}
Copy the code

Room requires declaration dependencies:

dependencies {
    def room_version = "2.1.0 - alpha06"

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor

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

    // 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"
}
Copy the code

The declaration dependencies required by WorkManager:

 dependencies {
    def work_version = "2.0.1"

    // (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 - Test helpers
    androidTestImplementation "androidx.work:work-testing:$work_version"
  }
Copy the code

3. Summary of this section

Architecture Components is a very important part of JetPack. Google has been constantly updating this set of architectures for the past two years. In order to solve many pain points in Android development, Architecture Components will be the inevitable trend of Android development infrastructure in the future, so let’s learn it together now.

  • Android Jetpack Architecture Component family first introduction
  • Android Jetpack Architecture component series 2 DataBinding
  • Android Jetpack architecture component series three life cycle aware components
  • Android Jetpack architecture component series four LiveData