Android Performance — Part 2

Wajid Ali
3 min readJul 30, 2023

JankStats AndroidX Library

Introduction

JankStats is a groundbreaking AndroidX library designed to detect and report performance issues within your Android applications on real user devices. The term “jank” refers to poor application performance, leading to missed frames, discontinuous UI motion, and ultimately, an unsatisfactory user experience. As developers, identifying and resolving performance-related problems can be challenging, especially when dealing with real-world devices and diverse user environments. JankStats comes to the rescue, offering a unified API to capture per-frame performance data, enabling instrumentation and reporting of performance problems, and providing crucial insights into user interactions.

Understanding Jank

Jank refers to instances when an application’s user interface experiences stuttering or frame rate drops, hindering smooth interactions and causing frustration for users. This can happen due to excessive workload on the UI thread, inefficient layout calculations, or unoptimized code. Detecting and resolving jank is crucial for delivering exceptional user experiences and maintaining user retention.

The Need for Performance Monitoring

  • Identifying Jank Incidents: JankStats aids in detecting and quantifying instances of jank, allowing developers to pinpoint when and where performance issues occur.
  • Analyzing Performance Metrics: Performance monitoring tools offer valuable metrics such as frame rates, UI response times, and memory usage, helping developers assess overall app performance.
  • Code Optimization: Armed with performance data, developers can optimize critical code sections, reduce resource consumption, and improve app responsiveness.
  • Enhancing User Experience: Eliminating jank and improving performance leads to smoother and more enjoyable user experiences, fostering user satisfaction and app success.

Key Features of JankStats

  • Per-Frame Performance Monitoring: JankStats simplifies the process of capturing per-frame duration data. It provides a single API to report per-frame durations, internally utilizing appropriate mechanisms such as FrameMetrics on API 24+ devices. This unified API eliminates the need to implement different mechanisms across API versions, making performance monitoring more accessible.
  • Real-World Data Collection: Unlike some benchmarking libraries, JankStats is specifically designed for obtaining performance data from user devices in real-world scenarios. This ensures that you can assess app performance under actual usage conditions, accommodating a diverse range of devices and constraints.
  • PerformanceMetricsState API: One of JankStats’ unique contributions is the PerformanceMetricsState API. This simple set of methods enables developers to report important UI state information during app runtime. It provides context on what was happening in the app when performance issues occurred, aiding in understanding and resolving problems effectively.

Using JankStats: An Example

To demonstrate how JankStats works, let’s consider a sample RecyclerView in an Android app. We’ll track the UI state as the RecyclerView is scrolled using JankStats:

val scrollListener = object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
val metricsState = metricsStateHolder?.state ?: return
when (newState) {
RecyclerView.SCROLL_STATE_DRAGGING -> {
metricsState.addState("RecyclerView", "Dragging")
}
RecyclerView.SCROLL_STATE_SETTLING -> {
metricsState.addState("RecyclerView", "Settling")
}
else -> {
metricsState.removeState("RecyclerView")
}
}
}
}

By injecting this state into your application, JankStats can provide detailed information on per-frame duration and user interactions, facilitating accurate diagnosis and resolution of performance problems.

Conclusion

JankStats is a revolutionary AndroidX library that empowers developers to take performance monitoring to the next level. By capturing per-frame performance data, enabling real-world data collection, and providing valuable insights through the PerformanceMetricsState API, JankStats equips developers with the tools needed to tackle jank and enhance their apps’ user experiences. As JankStats reaches its first alpha release, the Android community eagerly awaits its impact on building high-performing Android applications. Don’t miss the opportunity to optimize your app’s performance and deliver exceptional user experiences — get started with JankStats today!

--

--

Wajid Ali

Android, iOS, Flutter, Augmented Reality and Technology enthusiast.