Jetpack Compose entry to proficient

What is Compose



Jetpack Compose is a new Android toolkit for building native interfaces. It simplifies and speeds up interface development on Android. Quickly bring your apps to life with less code, powerful tools, and an intuitive Kotlin API.

This sounds a bit abstract, I don't know what to say.
Let me translate:

Jetpack Compose is a framework that redefines Android layout based on Kotlin API. It can implement Android native applications more quickly. Save development time, reduce package size, and improve application performance.

Save development time, reduce package size, and improve application performance. This one sounds tempting, let's see how it works.

1.1 Android Studio support for Compose


powerful preview
This feature is based on Compose support in new versions of Android Studio.
The new version of Android Studio Arctic Fox (now in Canary version) has added many new tools to support new features of Jetpack Compose, such as: real-time text, animation preview, layout checking and so on.
1.1.1 Powerful Preview
The new Android Studio adds the effect of real-time preview of text changes, which can be previewed in real-time on Preview, emulators, or real machines.

1.1.2 Animation Preview
Animations can be viewed, inspected or played within AndroidStudio, and can also be played stitch by stitch.

1.1.3 Layout Checker
Android Studio Arctic Fox adds support for Compose in the layout monitor, which can analyze the hierarchy of Compose components. As follows:

1.1.4 Interactive Preview
In this mode, you can interact with UI components, click on them, and see how the state changes. This way, you can get quick feedback on how the interface reacts and preview animations quickly. To enable this mode, simply click the Interact icon , and the preview mode will be toggled.

To stop this mode, click Stop Interactive Preview in the top toolbar.
The above is AndroidStudio's support for Compose, which can be said to be a big deal.

1.2 Jetpack Compose before and after comparison


You thought Compose just added a preview feature? That's not it.
Switching from a normal app to a Compose app can greatly improve your app's speed and performance.
Let's take a look at an example of Google's official transformation of the application.
1.2.1 APK size reduction
The metric that users care about most is the APK size.
Here is the result as measured by APK Analyzer for a minified release APK (using R8) with resource reduction turned on:


A note on the numbers above:
1. The "APK file size" reported by APK Analyzer (instead of the download size) was used. APK size analysis
2. After using Compose, we found that the APK size was reduced by 41% and the number of methods was reduced by 17%
1.2.2 Number of lines of code
Although the number of lines of source code cannot be used as a standard to measure the quality of software, it can compare how much effort an experiment has made in "slimming", providing a statistical perspective for observing changes in experiments.

As you can see from the graph, the number of XML lines has dropped by a massive 76%. Goodbye, layout files, and other XML files like styles, themes, etc.
At the same time, the total number of lines of Kotlin code has also dropped.
That's a big part of why APKs can slim down.
1.2.3 Build Speed
Build speed is a metric that developers care about a lot.

A few clarifications are needed here:
"Full access to Compose" uses the latest version of Dagger/Hilt, which uses the new ASM API in Android Gradle Plugin 7.0. Other versions use an older Hilt version, which uses a different mechanism and can seriously slow down the generation of dex files.
In addition, the Kotlin compiler and the Compose compiler plugin do for us, such as location memoization, fine-grained reorganization, etc., the build time can be reduced by 29%, which can be said to be amazing.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00