Recently, I heard a lot of friends ask “Is Android development still promising?” . It’s true that Android development is no longer as hot as it was a few years ago, and the market for entry-level developers is saturated, but according to several HR friends of mine, senior engineers are being wooed by the big companies, often with high salaries but no talent. This also put forward higher requirements for us developers, so, strive to become a senior engineer is the most important, technology is our core competitiveness!
Jetpack is a collection of libraries that make it easier for developers to write great Android apps. Avoid frequent bugs and do repetitive work. Jetpack also has several advantages for developers: Faster development: Jetpack offers a rich set of components that can be used individually to take the most out of the abstraction, but can be deeply customized to eliminate boilerplate code: Jetpack manages cumbersome activities such as background tasks, navigation, and lifecycle management. Build high quality powerful applications: Jetpack components are built around modern design practices with backward compatibility to reduce crashes and memory leaks.
Introduction to the
Jetpack is a set of libraries, tools, and guides that make it easier for developers to write great apps. These components help developers follow best practices, get developers out of boilerplate coding, and simplify complex tasks so they can focus on the code they need.
What the Android Jetpack component does
-
Navigation: a tool class for managing Fragment switching, visual, binding controls, animation support, etc.
-
Data Binding: Needless to say, speeds up MVVM creation.
-
Lifecycle: this is an important reason why we can handle the Lifecycle of activities and fragments. Fragments and activities on AndroidX already provide support for Lifecycle by default.
-
ViewModel: Acts as the ViewModel layer of MVVM and has the declared cycle awareness to handle uI-related data.
-
LiveData: Just like RxJava, it listens on data, with the advantage of no lifecycle handling, no memory leaks, etc.
-
Room: Powerful ORM database framework.
-
Paging: An easy-to-use data Paging library that supports RecyclerView. WorkManager: a flexible, simple, delayed, and guaranteed execution background task processing library.
Why you might choose Android Jetpack
-
Easier to use together: Because many libraries in the Android Jetpack component support other libraries, such as Room and Paging support LiveData.
-
Backward compatibility: Almost every component has support for lower versions.
-
Support for RxJava: Due to RxJava’s powerful ecosystem, almost all data-related components provide support for RxJava.
-
Reduce code: MVVM mode built with Data Binding + ViewModel + LiveData or RxJava can significantly reduce code and be more convenient than MVP mode, without actively updating the UI.
-
No bundling: The Android Jetpack family of components can be used without bundling, so you can rely on just one library if you only want to use a single one.
Android Jetpack?
Android Jetpack, for Google, is a manifestation of their determination to reorganize and unify the Android ecosystem. What Android Jetpack shows is also the direction that Google wants to expand and maintain. For Android developers who have long agonized over third-party library choices, this is a beacon for us.
Some of the Libraries in the Android Jetpack family may not be ready to be your first choice for your job, but digging into the Android Jetpack component will give you the opportunity to talk to the Big guns at Google and bring some ideas to your day-to-day development.
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.
directory
Actual combat screenshot
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.
directory
Actual combat screenshot
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.
directory
Actual combat screenshot
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.
directory
The project of actual combat
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
directory
Actual combat screenshot
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.
directory
Actual combat screenshot
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.
directory
Part of the screenshot
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.
directory
Actual combat screenshot
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.