R8, Android's optimizer, includes a method outlining optimization that identifies repeated bytecode sequences and extracts them into a shared method. Using Moshi JSON parsing as a case study, the post demonstrates how duplicated StringBuilder patterns across multiple parser classes get automatically de-duplicated by R8 into a generated `GeneratedOutlineSupport.outline0` method. R8 requires at least 20 duplicate sequences of 3–99 bytes to trigger outlining. The key insight is that deliberately duplicating code (rather than writing a helper method) can yield better APK size results because R8 performs whole-program analysis and can outline across unrelated classes. The post also notes that Kotlin's `inline` modifier can inadvertently create more outlining candidates.

7m read timeFrom jakewharton.com
Post cover image
Table of contents
Representative ExampleOutlining