Menu

1 / 4

Static, Finally: Moving Android Code from Companion Objects to Companion Blocks

DEV Community: java·Android Expert·about 7 hours ago
#fvrOhWp8
#devtojava#devto#dev#companion#currency
Reading 0:00
15s threshold

For a decade, every Android codebase written in Kotlin has carried the same quiet tax. You want a factory function, a constant, a TAG , a newInstance() for a Fragment, and the language answers with a nested singleton called Companion . It works, it is idiomatic, and it leaks into places you never planned for: Java call sites, reflection, ProGuard rules, bytecode size.

Kotlin 2.5.0-Beta1 introduces an experimental alternative: companion blocks and companion extensions . JetBrains describes the feature in The Companions to Come , and the full design lives in KEEP-0449 . This article is the Android engineer’s view: what changes, what to try first, and, more importantly, how an adoption goes wrong.

Status note. The feature is experimental. Snippets below follow the syntax shown in the announcement, so pin your toolchain and re-check against the KEEP before copying anything into production. Where I am reasoning from the design rather than quoting it, I say so.

1.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.