Interviewer: As an Android developer, don’t even know jetpack? You should change your career!

JetPack came out in 2018, ushering in its third year. However, the popularity in China is far lower than that in foreign countries. When domestic developers hear others say JetPack, the words that pop up in their minds are " immature", "not easy to use", and "difficult to debug" , but the truth is Is it really so?

Let's first take a look at JetPack's ambitions when it is released:

The above is from Google's quotations. In summary:

  1. JetPack is a complete set of component libraries

  2. It can help us write quality applications

  3. It saves us from writing unhealthy boilerplate code

4. It can regulate our development behavior

Next, let's take a look at what components are included in the JetPack component library:

From the figure above, we can see that the JetPack component library is mainly divided into four parts, namely architecture (Architecture) , interface (UI) , behavior (behavior) and foundation (foundation) . Each component can be used alone or in combination. Among them are DataBinding, LiveData, Paging, Room, ViewModel, Lifecycles.

But why is it difficult for domestic developers to accept it? In fact, several reasons can be analyzed:

  1. The smell of new technology is relatively slow

  2. I don’t know how to use a single component of JetPack, where to use it

  3. No full copy source

  4. The old project did not use JetPack before, now the change will affect the whole body, lazy change, love whoever

  5. Don't know what JetPack is

The above analysis may be a bit of a direct hit, but it is true. In fact, summing up the above reasons is that you don’t know JetPack very well. Even if you have used it but are not familiar with the source code and underlying principles, it is like finding a partner who doesn’t know much about it, and you will easily hand over yourself. Is he or she?

It is for these reasons that everyone completely ignores the power of JetPack and the changes it can bring to us. Next, we will analyze what it can bring us.

1. Solved the Android architecture problem

Since Google has not introduced standards for the architecture design of Android applications before, developers of various companies need to polish a set of development plans that meet their own projects. But this will cause two obvious problems. The first is that these plans are born from wild roads and have no official status. The second is that they cannot find a set of plans that meets everyone's expectations. To put it bluntly, there is no uniform standard and serious fragmentation. So Google is aware of this problem and launched JetPack, allowing developers to use standard architecture components without having to entangle the design of the architecture, and can focus more on their own business code.

2. Improved code quality

Jetpack has the ability based on life cycle perception, which can reduce memory leaks and NPE crashes, and provide a strong guarantee for us to develop robust and smooth programs. At the same time, the automatic generation of boilerplate code can reduce the number of remaining BUGs.

3. Improved development efficiency

Jetpack can reduce boilerplate code and help improve the efficiency of Android development. These components can be used alone or in combination, and run the same in different Android versions.

Seeing the changes brought about by these JetPacks, can you just let it go and be tempted? Next, we proceed from the actual job requirements:

Everyone should have understood here that JetPack is now clear in many job requirements, which also means that in addition to the previous knowledge points, JetPack will appear in the future interview process.

Having said that, let’s talk about how to learn JetPack. Many people usually copy and paste in development and don’t bother to understand the principle, but if you don’t know the principle of JetPack, you will find that human errors are prone to occur. This is back. At the very beginning, many people think that JetPack has a lot of problems. In fact, it is not, but you don't know enough about it. Therefore, if you want to learn, you must learn thoroughly and understand all their principles, not just staying at the level of application.

How to learn?

If you are lack of learning materials, and I happened to find this Alibaba internal Jetpack collection, from entry to proficiency, the tutorial is easy to understand, rich in examples, both basic knowledge and advanced skills, which can help readers get started quickly. It is you to learn the Sunflower Book of Jetpack.

If you need a full set of "Jetpack architecture components from entry to proficiency", please go directly to the end of the article to receive it for free~

Android Jetpack - Navigation

Navigation is directly translated as navigation. It is one of the Android Jetpack components, making single Activity applications the preferred architecture. The jump of the Fragment page in the application is handled by Navigation, and the developer does not need to deal with the complexity of FragmentTransaction and related transition animations.


Android Jetpack - Data Binding

Data Binding is a support library that uses a declarative method instead of a coding method to bind UI controls and data sources.
Usually we call the method of the UI framework layer to declare the view in the activity. For example, the following code calls findViewById() to declare a TextView control and bind it to the userName property of viewModel:

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

The following code shows how to use Data Binding to directly assign a value to the test attribute of TextView in the layout. The advantage of this is that there is no need to call Java code like the example above. Note that the syntax used in the assignment expression is @{}:

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

Binding UI controls directly in the layout can reduce the call of UI framework methods in the activity, which makes the code more concise and easier to maintain. It can also improve App performance and avoid memory leaks and null pointer exceptions.


Android Jetpack - ViewModel & LiveData

ViewModelThe view and logic are separated. ActivityOr Fragmentonly responsible for the UI display part. Specific network requests or database operations are ViewModelresponsible. Similar MVPto the Presenterlayers in the pattern . ViewModelThe class is designed to store and manage interface-related data in a life cycle-oriented way. Allow data to be retained after configuration changes such as screen rotation . We know that changes in configuration items such as rotating screens will cause our Activity to be destroyed and rebuilt. At this time, the data held by the Activity will be lost ViewModeland will not be destroyed, which can help us save data in the process . And ViewModeldo not hold Viewthe instance of the layer, through the communication LiveDatawith Activityor without worrying about potential memory leaks .Fragment

LiveDataIt is an observable data storage class. Unlike regular observable classes, LiveData has life cycle awareness , which means that it follows the life cycle of other application components (such as Activity, Fragmentor Service). This perception ability ensures that LiveDatawhen the data source changes, its observers are notified to update the UI interface. At the same time, it will only notify the observer in the Active state to update the interface. If the state of an observer is in the Pausedor state Destroyed, it will not receive the notification. So don't worry about memory leaks.


Android Jetpack - Room

Room is a member of the Jetpack component library and belongs to the ORM library. It mainly abstracts Sqlite to simplify the operation of the database by developers. Room supports syntax checking at compile time and supports returning LiveData.

Add dependency

Add the following dependencies in the app's build.gradle:

def room_version = "2.2.0-rc01"

implementation "androidx.room:room-runtime:$room_version"
// For Kotlin use kapt instead of annotationProcessor (注意这个注释)
kapt "androidx.room:room-compiler:$room_version"

If the project is developed using Kotlin language, use the kapt keyword when adding room-compiler, and use the annotationProcessor key for java language development. Otherwise it will cause access errors.

Android Jetpack - Paging

Many applications obtain data from data sources that contain a large number of items, but only display a small portion of the data at a time. Loading the data displayed in an application can be large and costly, so avoid downloading, creating or presenting too much data at once. In order to make it easier to gradually load data in our application Google method provides this component, which can be easily loaded and now a large data set with our RecyclerView fast, infinite scrolling. It can load paging data from local storage, network or both, and allows us to customize how to load content. It can be used with Room, LiveData and RxJava.

Paging Libray is divided into three parts: DataSource, PagedList, PagedAdapter

Android Jetpack - WorkManger

WorkManager is a component for managing background tasks in Android Jetpack.

Common usage scenarios: 1. Send logs or analysis data to back-end services 2. Periodically synchronize application data with the server

Use the WorkManager API to easily schedule background tasks. Tasks that can be run delayed (that is, do not need to run immediately) and can run reliably when the application exits (the process is not closed) or the application restarts.

Lifecycle of Android Jetpack architecture components

For application security, there is often a need for security confirmation processing such as software confirmation when switching from the background to the foreground. If it was in the past, this requirement is actually quite difficult to achieve. But since Google launched the Lifecycle component, this requirement has become much simpler. In addition to the perception of Lifecycle switching from the background back to the foreground, this component can be used to more simply implement complex life cycle operations


The most comprehensive guide to Android Jetpack Compose

Jetpack Compose is a modern toolkit for building native Android UI. It is based on a declarative programming model, so you can simply describe the appearance of the UI, while Compose takes care of the rest-when the state changes, your UI Will be updated automatically. Since Compose is built on Kotlin, it is fully interoperable with the Java programming language and can directly access all AndroidJetpack APIs. It is also fully compatible with the existing UI toolkit, so you can mix the original View and the new View, and use Material and animation to design from the beginning.

Conclusion

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

If you happen to have friends who need this information, you can directly click here to get it for free .

More information about Android architecture and interview topics can be found here.

Guess you like

Origin blog.csdn.net/Android578/article/details/113725933