# R8 Optimization: Staticization

DevFeed: [R8 Optimization: Staticization](<https://devfeed.tech/articles/r8-optimization-staticization-20963.md>)

Original publisher: [Read original article](<https://jakewharton.com/r8-optimization-staticization/>)

Published: 2018-12-11T00:00:00Z

Content type: article

Language: en

Sources: [Jake Wharton](<https://devfeed.tech/sources/jake-wharton.md>)

Topics: [R8](<https://devfeed.tech/topics/r8.md>), [Optimization](<https://devfeed.tech/topics/optimization.md>), [Android](<https://devfeed.tech/topics/android.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [code](<https://devfeed.tech/tags/code.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [r8](<https://devfeed.tech/tags/r8.md>)

## AI overview

This article introduces R8 optimization for Android and examines staticization, an optimization that makes eligible functionality static. It explains how R8 extends D8 with optimization passes over an intermediate representation and discusses the runtime and binary-size costs of Kotlin companion objects.

## Source excerpt

Note: This post is part of a series on D8 and R8, Android's new dexer and optimizer, respectively. For an intro to D8 read "Android's Java 8 support". This post introduces R8. The first three posts (1, 2, 3) in this series explored D8. Among its core responsibility of converting Java bytecode to Dalvik bytecode, it desugars new Java language features and works around vendor- and version-specific bugs in Android's VMs. In general, D8 doesn't perform optimization. It may choose to use Dalvik bytecodes which more efficiently represent the intent of Java bytecodes (as seen with the not-int example). Or, in the process of desugaring language features, it may choose to optimize the desugared code it is generating. Aside from these very localized changes, D8 otherwise performs a direct translation. R8 is a version of D8 that also performs optimization. It's not a separate tool or codebase, just the same tool operating in a more advanced mode. Where D8 first parses Java bytecode into its own intermediate representation (IR) and then writes out the Dalvik bytecode, R8 adds optimization passes over the IR before its written out. This post (and a bunch of future posts) are going to explore some of the individual optimizations that R8 performs. We start with an optimization called staticization which means the act of making something static. Companion Objects Kotlin uses companion objects to model the features of Java's static modifier. They're actually a much more powerful language feature allowing things like inheritance and implementing interfaces. That power comes with an associated cost, however, and we pay for that cost regardless of whether we're using the added power or just emulating static. fun main(vararg args: String) { println(Greeter.hello().greet("Olive")) } class Greeter(val greeting: String) { fun greet(name: String) = "$greeting, $name!" companion object { fun hello() = Greeter("Hello") } } In this example, the Greeter class uses a companion object to expose f