Enhance Your Android App Efficiency With Baseline Profiles | by way of Jaewoong Eum | Dec, 2023

Jaewoong Eum

ProAndroidDev

Application efficiency holds paramount importance because it at once correlates with the total person enjoy. Inside the developer group, a lot of strategies exist for reinforcing software efficiency. On this publish, you’ll delve into any such tactics by using the Baseline Profile.

Baseline Profiles empower you to succeed in quicker code execution, boasting attainable velocity enhancements of round 20–30% from the primary release. That is completed by way of handing over pre-compiled supply code knowledge, successfully bypassing interpretation and just-in-time (JIT) compilation steps when customers release your software.

While you combine Baseline Profiles into your software, Android Runtime (ART) optimizes actual code pathways by using the equipped supply code profiles. Those profiles consist of knowledge relating to your categories and strategies hired in Forward-of-Time (AOT) compilation.

For library authors, Baseline Profiles provide a very good selection for handing over optimum developer reports. Baseline profiles may also be packaged inside libraries and therefore included into person packages, the place the library’s code pathways are applied to fortify customers’ app efficiency.

In maximum situations, the Baseline Profile complements your app’s efficiency within the method described previous. Alternatively, this development is especially pronounced when your challenge is determined by a lot of third-party libraries, together with common Jetpack libraries. In such instances, your app reports much more considerable optimization and function improvements.

For example, in case your app is determined by a large number of Jetpack Compose libraries, you may come across some minor jankiness or lag all through the preliminary release of your software. That is an expected end result, as Compose operates as a library, so it doesn’t interact in device useful resource sharing and the Android platform. Subsequently, ART must interpret those libraries closely when the app launches and run JIT interpretation when the capability is wanted, reducing your app’s efficiency and inflicting it to be sluggish on startup.

Whilst positive Jetpack libraries already comprise Baseline Profiles, notice that the majority Jetpack libraries wouldn’t have them. Additionally, many libraries from GitHub or developer communities additionally don’t comprise Baseline Profiles.

Taking into account the specifics of your challenge, it will be nice to combine Baseline Profiles into your challenge reasonably than depending on libraries to offer them if you are expecting to optimize your app efficiency.

Now, let’s continue with configuring the Baseline Profile to your challenge. Ranging from AGP 8.0 or upper, you might have the ease of using the Baseline Profile Gradle plugin. This plugin streamlines the introduction of Baseline Profiles, supplies bundle filtering choices, and gives extra handy options, together with taste keep watch over.

If you happen to’re the use of Android Studio Iguana (or a more moderen model) at the side of Android Gradle Plugin 8.2, you’ll be able to without problems create a Baseline Profile module by way of applying a template presented by way of Android Studio.

While you navigate throughout the steps: Record -> New -> New Module inside your Android Studio, you’ll see the Baseline Profile Generator template, illustrated within the symbol underneath:

Make certain that you’ve recognized the precise challenge or module you want to goal for Baseline Profiles technology, and continue to configure the bundle title for the Baseline Profile module accordingly. Upon clicking the End button, you’ll realize the introduction of a module that properties the BaselineProfileGenerator.kt record.

In case your challenge encompasses more than one variants, and also you intention to control behaviors like merging the generated profiles for all variants right into a unified profile or automating Baseline Profile technology all through challenge builds or liberate meeting, seek advice from the Configuring Baseline Profile technology information for complete directions.

For library authors, together with Baseline Profiles inside your library can fortify the developer enjoy. To arrange Baseline Profiles to your library, merely upload the Baseline Profile plugin as you can configure it for the objective module.

Following that, come with the Baseline Profile dependency that you just established within the earlier segment.

After all, you should set bundle filters, as demonstrated underneath, to forestall the inclusion of all unrelated categories and approach knowledge within the generated Baseline Profiles:

You’ll be able to now at once generate Baseline Profiles from the Run conversation in Android Studio, simply as illustrated within the symbol underneath:

Usually, it is possible for you to to generate Baseline profiles the use of the fundamental situations supplied within the BaselineProfileGenerator.kt record. This may also be completed by way of both settling on the menu possibility above or by way of coming into the command line underneath into your terminal:

./gradlew generateBaselineProfile

If the execution is a success, you’ll uncover the generated Baseline Profiles named baseline-prof.txt inside each and every module, dwelling underneath the /src/primary/generated/baselineProfiles listing. While you click on at the textual content record, you’ll be able to in finding the category and approach declarations, introduced as proven underneath:

As glaring within the previous screenshot, the Baseline Profiles surround magnificence and approach knowledge pertaining on your software or library. This knowledge is applied to optimize execution for potency inside the Android runtime atmosphere.

If you happen to come across problems producing Baseline Profiles for any explanation why, believe the next troubleshooting steps:

  1. Double-check that your baselineprofiles module has the right kind targetProjectPath and that your goal challenge module has effectively configured the Baseline Profiles plugin.
  2. In case your challenge is configured with more than one flavors or model title suffixes, be sure that the bundle title utilized in each the baselineprofiles module and the BaselineProfileGenerator.kt record is suitable to your challenge’s particular taste or model.

If you’ve generated Baseline Profiles, let’s read about the APK/AAB information. Now, you’ll be able to check if the Baseline Profiles were as it should be built-in into your APK/AAB information and are in a position to be dropped at customers.

If you create an APK/AAB record of your challenge, you’ll be able to open it by the use of Construct -> Analyze APK menu on Android Studio. If you happen to open your APK/AAB record, you’ll in finding the obfuscated Baseline Profile in underneath property/dexopt/baseline.prof listing for APK or BUNDLE-METADATA/com.android.gear.construct.profiles/baseline.prof for AAB.

As glaring within the screenshot above, you’ll practice that the baseline-prof.txt record is securely embedded inside your AAR record. At this level, you may marvel, “Why does it elevate a .txt extension, not like the APK/AAB information?”

The solution to this query may also be discovered within the IssueTracker: Baseline Profile isn’t obfuscated for AAR library. Consistent with the Baseline Profile group’s reaction, “Libraries aren’t normally obfuscated, and AGP these days does now not disambiguate between debug vs. liberate variations of a library.” So, for those who’ve spotted a .txt record as a substitute of a .prof record, it is totally as anticipated.

The authentic Android documentation states that “Compiled Baseline Profiles should be smaller than 1.5MB.” Let’s discover the reason at the back of this limitation.

Navigate to the listing the place the generated baseline-prof.txt record is saved, and check out the record’s houses. You can most probably realize that the record dimension exceeds the 1.5 MB prohibit, steadily starting from 4 to ten MB or upper.

Then can’t we comprise our Baseline Profiles within our APK/AAB? The solution is nope. The authentic Android documentation mentioned, “The compiled binary Baseline Profiles,” now not the textual content record. Then let’s see the dimensions of the compiled binary Baseline Profiles.

To analyze this, open your APK/AAB record the use of Android Studio’s Analyze APK menu, and get admission to the property/dexopt/ listing. Within, you’ll be able to come across the baseline.prof record. Upon checking its houses, you’ll be able to realize that the record dimension normally falls inside the vary of 10 to 30 KB and even upper.

Certainly, this dimension is considerably smaller than 1.5 MB, proper? While you bundle your Baseline Profiles into an APK/AAB record, a considerable portion of the profile knowledge is optimized, significantly lowering the compiled dimension. Within the majority of instances, you want now not be overly involved concerning the specified barriers.

Now that you just’ve delved into producing Baseline Profiles and comprehended their integration into your app, there’s yet another a very powerful facet to supercharge your startup and runtime efficiency — crafting efficient benchmark situations.

If you happen to’ve relied at the default situation supplied within the BaselineProfileGenerator.kt record, you want to notice that this situation handiest captures your app’s preliminary launcher job with out accounting for added person reports and behaviors. As a result, your Baseline Profiles may lack the excellent knowledge had to surround the wider vary of situations that customers may come across.

To tailor your Baseline Profiles to surround a much broader array of person interactions, you must craft your individual app situations the use of Macrobenchmark and UIAutomator libraries. You’ll be able to create automatic UI assessments, which let you simulate quite a lot of person movements, together with navigating to other displays, clicking buttons, scrolling, interacting with UI elements, or even emulating bodily instrument button presses.

According to your automatic UI check, Macrobenchmark extends its profiling and recording to provide Baseline Profiles with a broader scope of your software, taking pictures a extra intensive vary of profile knowledge. If you happen to’re serious about studying the way to create environment friendly benchmark situations, seek advice from the GitHub repositories underneath:

Now, let’s gauge the real-world affect of Baseline Profiles to your app’s startup and runtime efficiency. You’ll be able to simply measure your app’s startup efficiency by way of crafting benchmarking code the use of Macrobenchmark, as demonstrated within the following instance:

Make certain that you’ve hired the right kind bundle title and that your app situations align with the ones applied within the BaselineProfileGenerator.kt record. Within the supplied code snippet, you’ll be able to practice that you are benchmarking two situations to your app’s efficiency: one with out Baseline Profiles and the opposite with Baseline Profiles.

After executing your efficiency dimension check, you’ll in finding the document introduced in Android Studio as follows:

StartupBenchmarks_startupCompilationBaselineProfiles
timeToInitialDisplayMs min 341.7, median 419.6, max 765.2
Lines: Iteration 0 1 2 3 4 5 6 7 8 9

StartupBenchmarks_startupCompilationNone
timeToInitialDisplayMs min 371.4, median 434.7, max 815.9
Lines: Iteration 0 1 2 3 4 5 6 7 8 9

The document signifies that Baseline Profiles give a contribution to an build up in app efficiency, starting from 7% to ten% within the min/max instances.

If the measurements point out awesome efficiency within the non-Baseline Profiles situation, it’s very important to scrutinize whether or not your app situations closely depend on various options, contingent upon quite a lot of scenarios, equivalent to a lot of community or database requests.

Generally, you’ll be able to generate Baseline Profiles on every occasion you create a liberate construct by using the plugin possibility underneath:

You even have the versatility to generate Baseline Profiles adapted to extra particular situations in line with your personal tastes via GitHub Movements as you’ll be able to see within the script underneath:

Because the workflow runs, it is going to routinely begin a Pull Request, updating Baseline Profiles as depicted within the symbol underneath:

During this newsletter, you’ve delved right into a complete working out of Baseline Profiles. You’ve explored how Baseline Profiles paintings underneath the skin with the Dex optimizer, realized to investigate your APK/AAB/AAR information, found out the way to create benchmark situations, and the way to measure your app’s efficiency.

If you wish to be informed extra about Baseline Profiles and Benchmark, take a look at Baseline Profiles Review and What’s new in Jetpack Benchmark 1.2.0.

You’ll be able to in finding the creator of this newsletter on Twitter @github_skydoves or GitHub when you have any questions or comments. If you happen to’d like to stick up to the moment with Circulation, apply us on Twitter @getstream_io for extra nice technical content material.

As at all times, glad coding!

Jaewoong

Leave a Comment

Your email address will not be published. Required fields are marked *