Kotlin Intrinsics on Android
Article Summary
Rahul Ravikumar reveals how Android's tooling has been quietly carrying the weight of Kotlin's type safety. Every null check, every parameter validation adds overhead that pure Kotlin apps don't actually need.
When Kotlin compiles to JVM bytecode, it sprinkles Intrinsics checks everywhere to provide helpful error messages for Java callers. But if your entire app is Kotlin, these checks are pure overhead, bloating your dex file with unnecessary strings and method calls.
Key Takeaways
- AGP 9.0 automatically replaces Intrinsics.checkNotNull() with efficient getClass() calls
- Compose PokedexScrollBenchmark shows measurable frame duration improvements after optimization
- ART has native intrinsic for getClass(), compiling to extremely efficient assembly
- Eliminates string constants from dex pool while preserving null safety semantics
Android Gradle Plugin 9.0 eliminates Kotlin's null-safety overhead automatically, giving you type safety at compile time and better runtime performance without any developer intervention.
About This Article
Kotlin's null-safety checks add JVM bytecode that includes Intrinsics.checkNotNullParameter() calls with string constants. This creates extra overhead in the dex file when both the library and application use Kotlin.
R8 starting with Android Gradle Plugin 9.0 automatically replaces Intrinsics.checkNotNullParameter() invocations with invoke-virtual getClass() calls. ART can then compile these to efficient assembly code.
The PokedexScrollBenchmark benchmark showed better frame durations after this optimization. Compose can now produce frames with lower latency, and string constants no longer take up space in the dex pool.