1.1. Overview of Android development status

It’s been 13 years since the first Android smartphone was released in October 2008. For more than ten years, both the system itself and the development technology attached to the system have experienced a long period of development, accumulation and precipitation, and various rules are becoming perfect. This section mainly introduces the history and current situation of Android development technology. Only after understanding the history, can we know the reason for compose’s emergence.

1.1.1 Wild and unrestrained era

The early Android system was wild and unrestrained, and ordinary applications could easily obtain sensitive permissions. For example, the author’s first mobile phone, Redmi Note2, was equipped with Android 5.1 system, on which third-party apps could obtain access to SD cards without the user’s permission. As a result, sd is full of directories created by unknown apps, and private files such as user albums can be easily accessed by any app. Android development technology is also simple and random, all kinds of logic written in the activity, the author has seen more than 6000 lines of activity, it is not strange even if there are more than 1W lines of activity. The most obvious problem with this is that the code becomes unmaintainable. Imagine how painful it is to find the logic you are looking for in a file of thousands of lines, and how minor changes can lead to new and unknown bugs.

1.1.2 Dawn -MVP, MVVM

When conflicts accumulate to a certain stage, when the current development means cannot meet the needs of rapid iteration of APP, new architectures will inevitably emerge to try to solve the problem. MVP and MVVM are the products of this stage. How do they solve the problem? MVP is in Presenter, MVVM is in ViewModel, and some framework tools are used to implement bidirectional data binding. It seems that the logic has become clearer and the responsibilities of the individual classes have become clearer.

1.1.3 Taking a different path? – the React Native, Flutter

React Native and Flutter are two well-known solutions. The author is not familiar with React Native and has been exposed to Flutter for some time. In the following articles, There will also be comparisons between Flutter and Jetpackcompose. Of course, knowing nothing about Flutter will not affect your ability to learn and master Compose. Flutter completely abandonsthe traditional Android app development and uses a new development language, a new idea — responsive development. This is the same for Compose. You can clearly see a Flutter in Compose

1.1.4 Official release – Jetpack

To clarify what Jetpack is, Jetpack is essentially a toolkit from Google that has a number of libraries that Google will find useful and recommend developers to use, including Lifecycle, ViewModel, etc., to unify the Android development ecosystem. It is a good plan to make it easy and fast for developers to develop high-quality apps. Of course, the unknown effect is also very good. More and more projects use ViewModel, LiveData and other useful libraries in Jetpack, and this series of blog will introduce Jetpack compose. Is a new responsive UI development library recommended by Google.


1.2 Preliminary study of Jetpack compose

1.2.1. What is JetpackCompose?

Jetpack Compose (Compose) is a modern toolkit for building native Android UIs. Compose simplifies and speeds up UI development on Android with less code, powerful tools, and an intuitive Kotlin API, according to its official description. Compose is an Android UI toolkit designed to simplify and speed up UI development on Android.

1.2.2. Why does such a thing exist?

The emergence of new things, often in order to solve the existing problems, such as the emergence of Flutter is in order to solve the problem of the same App cross-platform, at the same time, to a certain extent, consistency of the UI and solve the problem, the emergence of plug-in technology, in order to solve the problem of the dynamic and thermal repair, to some extent also relieves the problem of packet size. Hongmeng operating system is to solve the operating system is stuck in the neck and so on. So what problem is Compose trying to solve? As already mentioned in the “Compose” section, AndroidUI development is currently too cumbersome, and Google is trying to simplify it with this tool.

The characteristics of the 1.2.3.Com pose

Now that we know that Compose is meant to simplify Android development, how exactly does that work? Some of you may have heard that Compose is a declarative UI framework, and the previous Android View system is a command UI framework. What is the difference between the two UI frameworks and which one is better? Describe it in one sentence:

  • Imperative UI is about telling the framework what to do and ultimately what to do.
  • Declarative UI tells the framework what to do, we don’t care how.

For example, in the View system of Android, we want to modify the contents of a TextView on the interface. How do we do this? First we need to find the View object by the View ID, and then give the command to the View, and say to it, I command you to change your string to XXX, the string property of TextView will be modified, and it will be displayed as the latest content after the next interface redrawing. The composup UI is composed of a bunch of functions annotated with the @compose annotation. Therefore, it is not possible to find the View object, and it is not possible to set the properties of the View object by command. So how do we update the UI? In Compose, the UI data is passed down one layer from the Compose function at the top level. If you want to update the data, you can simply modify the data and have the UI rebuild again. Now that we know the differences between the two UI frameworks and their respective features, which approach is better? I don’t know. Both Flutter and Compose suggest that Google officially recommends more responsive UI. We won’t go into that here, we’ll just talk about technology, not site.

1.3 JetpackCompose environment construction

The Compose environment is much easier to build than the Flutter environment, or even unnecessary. You just need to download the latest androidstudio for Compose. But so far the only Studio Canara version supports developer. The android, Google. Cn/Studio/prev… . Of course, Compose will be supported by the latest version of Studio when the official version is released, and Studio will update the latest version.

For a new Compose project, simply select the Empty ComposeActivity when selecting the Activity, and the desired Compose dependency will be automatically introduced. For an old project, it would be easy to create an Empty ComposeActivity using Compose. Note that Compose requires a higher Gradle version, miniSdkVersion 21 and above, and Kotlin version 1.4.32 and above. If the introduction fails, it is most likely that these versions of the old project have problems. Now that you know what Compose is and how to set up your environment, the next chapter will show you how to use it