OKLayoutInflater
Article Summary
OkCredit's Android team was hitting a wall: complex XML layouts were causing frozen frames and ANRs. AndroidX's AsyncLayoutInflater wasn't cutting it.
The OkCredit engineering team built OkLayoutInflater, an open-source library that supercharges Android layout inflation using Kotlin coroutines. They tackled the limitations of AndroidX's single-threaded approach and achieved dramatic performance gains.
Key Takeaways
- Replaced single-threaded inflation with coroutines for parallel processing across CPU cores
- Added lifecycle awareness to prevent crashes from inflation callbacks after screen closes
- Reduced frozen frames by 23% overall and 30% on critical screens
- Cut RecyclerView load times significantly by inflating items asynchronously
- Works seamlessly with MVI architecture by caching render calls until inflation completes
OkCredit reduced frozen frames by 23% and improved RecyclerView performance by switching from AndroidX AsyncLayoutInflater to their coroutine-based solution.
About This Article
AndroidX AsyncLayoutInflater uses a single background thread, which created a bottleneck. When one RecyclerView item took a long time to inflate, it blocked everything else waiting in the queue.
OkLayoutInflater switched from the single-threaded InflateThread to Kotlin coroutines with the default dispatcher. This allows layout inflation to happen in parallel across all available CPU cores.
RecyclerView load times improved significantly from the 90th percentile. Fragment opening times also got faster, dropping from the 99th percentile after switching to the coroutine-based approach.