Jetpack is no stranger to you. Since Google launched Jetpack in 2018, it has become an indicator of the future of Android.

As an Android engineer, we used to joke about how fragmented Android is, how the project library is made up of Square, Glide, Bus, MVP, MVVM, etc. Jetpack solves this problem perfectly.

Jetpack is a strategic plan put forward by Google to solve the fragmentation of Android development and create a mature and healthy ecosystem. It is also the future development direction of Android proposed by Google. Jetpack has been embraced by many Github libraries and major manufacturers, and is now a must-have for android interviews, so if you want to build android at a major manufacturer, Jetpack will be a must-have.

How to learn?

If you are lack of learning materials, and I just collected this Alibaba internal Jetpack treasure book, from the beginning to master, the course is easy to understand, rich examples, both basic knowledge, but also advanced skills, can help readers quickly start, is the sunflower treasure book for you to learn Jetpack.

Android Jetpack – Navigation

Navigation is one of the Android Jetpack components that makes single-activity apps the preferred architecture. In-app Fragment page jumps are handled by Navigation, eliminating the need to deal with the complexity of FragmentTransaction and associated transitions.

Android Jetpack – Data Binding

Data Binding is a support library that uses a declarative rather than coded approach to bind UI controls to Data sources. Normally we call the UI framework layer method declaration View inside an activity. For example, the following code calls findViewById() to declare a TextView control and bind it to the viewModel userName property:

findViewById<TextView>(R.id.sample_text).apply {
    text = viewModel.userName
}
Copy the code

The following code shows how to use a Data Binding to assign the test property of a TextView directly within the layout. The advantage of this is that you do not have to invoke Java code as in the above example. Note that the syntax used in assignment expressions is @{} :

<TextView
    android:text="@{viewmodel.userName}" />
Copy the code

Binding UI controls directly to the layout reduces the need to call UI framework methods in the activity, which makes the code cleaner and easier to maintain. It can also improve App performance, avoiding memory leaks and null pointer exceptions.

Android Jetpack – ViewModel & LiveData

The ViewModel separates the view and logic. The Activity or Fragment is only responsible for the UI display. The ViewModel is responsible for specific network requests or database operations. This is similar to the Presenter layer in MVP mode. The ViewModel class is designed to store and manage interface-related data in a life-cycle oriented manner. Allow data to persist after configuration changes such as screen rotation. We know that configuration changes such as rotating the screen cause our Activity to be destroyed and rebuilt, and the data held by the Activity is lost along with it, while the ViewModel is not destroyed to help us save data in the process. And the ViewModel does not hold instances of the View layer and communicates with activities or fragments through LiveData without worrying about potential memory leaks.

LiveData is an observable data store class. Unlike regular observable classes, LiveData has lifecycle awareness, meaning that it follows the lifecycle of other application components such as activities, fragments, or services. This awareness ensures that LiveData notifies its observers to update the UI interface when the data source changes. It also notifies only observers in the Active state to update the screen, and does not notify an observer if Paused or Destroyed is in the state. So you don’t have to worry about memory leaks.

Android Jetpack – Room

Room is a member of the Jetpack component library, an ORM library that abstracts Sqlite to make it easier for developers to manipulate the database. Room supports compile-time syntax checking and returns LiveData.

Add the dependent

Add the following dependencies to your app’s build.gradle:

Def room_version = "2.0-rc01" implementation "androidx.room: room-Runtime :$room_version Kapt "Androidx. room:room-compiler:$room_version"Copy the code

If the project is developed in Kotlin, use the kapt keyword when adding room-compiler, and Java uses the annotationProcessor key. Otherwise, an access error may occur.

Android Jetpack – Paging

Many applications get data from a data source that contains a large number of items, but only display a small amount of data at a time. Loading the data displayed in your application can be large and expensive, so avoid creating or rendering too much data in one download. In order to make it easier to gradually load data in our application The Google method provides this component that can easily load and now large data sets with our RecyclerView for fast, infinite scrolling. It loads paging data from local storage, network, or both, and lets you customize how the content is loaded. It can be used with Room, LiveData and RxJava.

Paging Libray consists of three parts: DataSource, PagedList, and PagedAdapter

Android Jetpack – WorkManger

WorkManager is the component that manages background tasks in Android Jetpack.

The common application scenarios are as follows: 1. Sending logs or analyzing data to the back-end service 2. Periodically synchronizing application data with the server

Scheduling background tasks is easy using the WorkManager API. Tasks that are delayable (that is, do not need to run immediately) and can run reliably when the application exits (the process is not shut down) or when the application restarts.

Lifecycle for Android Jetpack architecture components

In order to ensure the security of applications, there are often requirements for security verification such as software verification when switching back from the background to the foreground. In the past, this requirement was actually quite difficult to achieve. But since Google released the Lifecycle component, this requirement has become much simpler. Lifecycle in addition to perceiving the transition from the background back to the foreground, this component makes it easier to implement complex operations that process the Lifecycle

Jetpack Compose for Android

Jetpack Compose is a modern toolkit for building native Android UIs based on a declarative programming model, so you can simply describe what the UI looks like, while Compose does the rest — your UI updates automatically when the state changes. Because Compose is built on Kotlin, it is fully interoperable with the Java programming language and has direct access to all AndroidJetpack apis. It’s also fully compatible with existing UI toolkits, so you can mix old views with new ones, and design with Material and animation from the start.

conclusion

Due to the limited space of the article, only part of the content is shown. This note also includes the Android Jetpack architecture components –App Startup, the introduction of the latest components of Android Jetpack, the actual Android Jetpack project (building the WanAndroid client version of Jetpack from 0), the actual project, and more.

Friends if you need, you can [click on me] to read and download. Free to share.

In addition, there is a Jetpack MVVM field project with source code + video.

Program source code【 Click on me 】To obtain.