R8, Android's optimizer built on top of D8, performs a transformation called staticization on Kotlin companion objects. When a companion object's methods don't actually require access to an instance, R8 converts them to true static methods, moves them directly onto the enclosing class, and eliminates the generated Companion class and its singleton field entirely. This reduces binary size, lowers memory pressure, and replaces slower virtual method calls with faster static calls. The post walks through the before/after Dalvik bytecode using dexdump, shows the equivalent Java source-level transformation, and notes that @JvmStatic alone doesn't achieve the same result since Kotlin callers still go through the Companion instance. The optimization also applies to regular Kotlin objects and Java singleton patterns.

9m read timeFrom jakewharton.com
Post cover image
Table of contents
Companion ObjectsMore Than Companions