Jetpack Compose Composition Tracing

Ben Trengrove
Android Developers
Published in
4 min readSep 20, 2022

--

Today, we are launching the first alpha of Compose Composition Tracing, a new feature that allows showing Jetpack Compose composable functions in the Android Studio Flamingo system trace profiler. It gives you the low intrusiveness from system tracing, with method tracing levels of detail in composition. This is great for checking your Compose app’s performance and working out why your app may not be performing as you expect.

Previously, the system trace would only show you one block per frame for recomposition, but it would not tell you which composables were actually being recomposed. This information was already available via method tracing, but method tracing can be very intrusive and significantly affect the performance of your app. Now, you can see all of your composables in the system trace without this problem and find any that are taking longer than you expect! Here is a preview of what it looks like:

Composables in the system trace

Just in case it’s not obvious how much extra detail this is providing, here is what it looks like without composition tracing enabled:

System trace without composition tracing

Set up for composition tracing

To try out the recomposition tracing in your project, you will have to update to at least the following versions.

  • Android Studio Flamingo Canary 1
  • Compose UI: 1.3.0-beta01
  • Compose Compiler: 1.3.0

The device or emulator you run your trace on must also be at minimum API level 30.

Additionally, you will need to add a new dependency on Compose Runtime Tracing:

implementation("androidx.compose.runtime:runtime-tracing:1.0.0-alpha01")

With this dependency, when you take a system trace that includes recomposition, you will see the composable functions automatically.

Take a system trace

To take a system trace and see the new recomposition tracing in action, first open the profiler

Click on CPU timeline

Navigate your app to the UI you want to trace and then select System Trace and Record

Use your app in order to cause recomposition and stop recording. Once the trace has been processed and appears, you should now be able to see the composables in the recomposition trace. You can use the keyboard and mouse to zoom and pan around the trace; if you are unfamiliar with navigating a trace see the documentation.

You can also see composables in the Flame Chart along with the file and line number.

Caveats

APK size overhead

While we aimed to minimise the overhead of the feature as much as possible, there is an APK size increase for Compose apps coming from tracing strings embedded in the APK by the Compose compiler. This could be relatively small if your app isn’t using much Compose or larger for fully Compose apps. These are additionally unobfuscated so they can appear in tracing tools, as shown above. The Compose compiler injects them into all apps, starting with version 1.3.0.

These can be removed in your production build by adding the following proguard rule:

These functions may change in the future, but any changes will be mentioned in the Compose release notes.

Note that keeping them in, while incurring some APK size cost, guarantees that the APK being profiled is the same one that the app users run.

Accurate timing

For accurate profiling, like with any performance testing, you need to make the app profileable / non-debuggable as per https://developer.android.com/studio/profile#profileable-apps

Feedback

We would love to hear your feedback on this feature, any bugs you find with it and any requests you have. You can send us feedback via the issue tracker.

--

--