very practically Sooner Jetpack Compose View interop with App Startup and baseline profile | by Sagar Begale | Android Builders | Nov, 2022 will cowl the most recent and most present advice roughly talking the world. admittance slowly correspondingly you perceive skillfully and accurately. will lump your data skillfully and reliably


Jetpack Compose is designed to be interoperable with an present View-based utility. This lets you take an incremental method to migrating your present app’s consumer interface to Compose.

Whereas your utility is in the course of migration, and hasn’t but Composable on the house or touchdown display screen, customers could expertise crashes when navigating to a display screen created with Compose. It’s because compose library is loaded solely when first used. There’s a workaround that has been floating round so as to add a composable pixel on the house/touchdown display screen. This resolution ensures that Compose parts are warmed up earlier than use. We don’t suggest taking this method, as making use of it in your utility leads to code that doesn’t contribute to the implementation of your operate and may result in unexpected errors. Relatively, we suggest a mixture of the applying startup library and customized reference profiles to resolve the crash subject. We are going to examine and visualize how the mix can assist enhance utility efficiency.

Baseline profiles assist enhance utility launch and runtime efficiency of Android purposes. Fundamental profiles are an inventory of lessons and strategies included in an APK. They’re utilized by the Android Runtime (ART) throughout utility set up to precompile essential paths into machine code. It is a type of Profile Guided Optimization (PGO) that enables purposes to optimize startup, cut back crashes, and enhance efficiency for finish customers by decreasing the quantity of code that have to be compiled Simply-In-Time (JIT).

Jetpack Compose is distributed as a library. This enables for frequent updates to Compose and compatibility with older variations of Android. However distributing as a disaggregated library comes at a value; libraries have to be loaded when the app begins and interpreted simply in time when the performance is required (for extra particulars, see Why it’s best to at all times efficiency check Compose at launch). This will result in an extended app startup time or crash each time the app makes use of a library operate for the primary time. To resolve this subject, Compose gives a reference profile to construct Compose forward of time when the app is put in. Most often, the default reference profile offered by Compose is ample to offer nice efficiency. However customizing the profile based mostly on widespread consumer interactions together with your app usually yields higher outcomes.

To research this efficiency enchancment, we wrote some Macrobenchmark exams and measured efficiency on the pattern Sunflower utility. The app has been partially migrated to Compose and doesn’t have Composable on the house or touchdown display screen.

Sunflower app screenshots showing the flow.  One of the screens is built with RecyclerView with each item embedded in Compose.  Another screen is built into Compose and uses AndroidViewBinding to interact with the View.

For this efficiency evaluation, we will probably be interacting with three screens of the applying:

  • the Residence display screen which is constructed with a View-based format and has no Composables.
  • the plant checklist The display screen is constructed with RecyclerView and every aspect inside the view is constructed with Compose. It makes use of the ComposeView to host the Compose content material.
  • the plant particulars A display screen that’s created with Compose and makes use of AndroidViewBinding to create and work together with Android format assets.

The Sunflower utility code has a BaselineProfileGenerator class that creates a baseline profile. We ran the check and created a customized reference profile centered on these Essential Person Journeys (CUJs). This profile is obtainable inside the baseline-prof.txt file beneath the main font set. In comparison with the default baseline profile offered by Compose, the customized baseline profile exhibits efficiency enhancements. Let’s check out the check outcomes for a few situations:

  • [Scenario 1]From the house display screen, the consumer navigates to the Plant Listing display screen that’s partially built-in into Compose. Benchmarking is obtainable within the PlantListBenchmarks class.

Macrobenchmark runs your check a number of occasions and outputs the outcomes as a statistical distribution. The distribution is represented in percentiles. P50P99. The next have been the outcomes after we ran the benchmark exams:

For the information proven within the desk above, P50 for CompilationMode.None implies that 50% of the frames have been rendered sooner than 7.1 milliseconds.

  • [Scenario 2] The consumer navigates to a display screen fully constructed with Compose and makes use of the AndroidViewBinding to work together with the TextView. Benchmarking is obtainable within the PlantDetailBenchmarks class. The next have been the outcomes after we ran the benchmark exams:

Be aware: Benchmark exams have been run on Samsung Galaxy Fold, your benchmark outcomes could present completely different numbers, however the total efficiency affect ought to be related.

if you look intently P99 numbers, you’ll discover vital enhancements. These outliers are sometimes what trigger crashes.

To additional enhance utility efficiency, we are able to use the applying startup library to heat up Compose parts earlier than they’re really used. Let’s strive it out and see what we discover out.

The applying startup library gives a structured approach to initialize parts at utility startup. With the app startup library, you possibly can heat up the Compose library throughout app startup.

Observe these steps so as to add the Compose initializer utilizing the applying startup library:

  • To make use of Jetpack startup in your utility, add the dependency on the startup runtime in your utility’s configuration. construct.gradle proceedings.

To heat up the Compose part, create a brand new ComposeInitializer class as proven within the following code snippet.

ProcessLifecycleInitializer ensures that the Compose library heats up solely when a minimum of one exercise goes to be seen.

Initializing the ComposeView on this means won’t add the content material to the view hierarchy, but it surely nonetheless helps to initialize the Compose part.

  • To make ComposeInitializer discoverable by App Startup, go to AndroidManifest.xml file in your mission. add a <meta-data> low entry InitializationProvider manifest entry as proven within the following code:

InitializationProvider is a particular content material supplier that helps uncover and name the part initializer you simply outlined.

  • Rebuild and replace the reference profile to account for the adjustments at startup. The benchmark profiler is a part of the macrobenchmark module. You simply must run the startPlantListPlantDetail check BaselineProfileGenerator class.
  • Rerun the macrobenchmark exams from the PlantListBenchmarks Y PlantDetailBenchmarks class.

Here is what we found for every of the above situations:

  • [Scenario 1]From the house display screen, the consumer navigates to the plant checklist display screen.

If you look intently, for the display screen that’s partially constructed with Compose, the outliers appear to have an enhancement of ~21%. Typically it’s the outlier frames that trigger the crash.

  • [Scenario 2] The consumer navigates to the Plant Particulars display screen.

There are ~27% share enchancment in outliers.

In each circumstances, including the applying startup initializer improved efficiency, particularly for outliers in P99.

Right here is the hyperlink to entry the code on GitHub.

Now the query is, do you have to at all times add the applying startup library for such situations? Is it relevant to all of your initiatives? Effectively, earlier than making such a choice, we strongly suggest that you just:

  1. Write benchmark exams and measure utility efficiency.
  2. implement the adjustments
  3. Rerun your exams to see if efficiency has really improved.

This implementation method would aid you in the long term to evaluate whether or not the ideas are actually relevant to your utility. Let statistical knowledge drive your deployment choices to enhance efficiency.

  • All the time add a baseline profile, no matter whether or not you want the applying launcher library.
  • The mix of the customized baseline profile and the app launcher library yields higher efficiency outcomes when your app has no composable components on the house or touchdown display screen, and the consumer navigates to a display screen that’s partially or absolutely constructed with Compose.
  • As a substitute of including Composable to the splash/touchdown display screen for efficiency causes (and never design selection), you should use the applying launcher library to heat up Compose parts.
  • Simply because somebody suggests it does not imply it’s important to observe that recommendation.. Write Macrobenchmark exams to confirm if the applying launcher library really improves the efficiency of your utility. Let statistical knowledge drive your deployment decisions to make sure they end in improved efficiency.

Thanks Ben Trengrove, Florina Mutenescu, Ben Weiss, Ryan Mentley, and Rahul Ravikumar for serving to with revisions.

I want the article kind of Sooner Jetpack Compose View interop with App Startup and baseline profile | by Sagar Begale | Android Builders | Nov, 2022 provides notion to you and is beneficial for add-on to your data

Faster Jetpack Compose View interop with App Startup and baseline profile | by Sagar Begale | Android Developers | Nov, 2022

By admin

x