One of Level’s major initiatives post-acquisition is to publish as much open source code as we can. Recently, our Android team released their first project: KBuilders.

KBuilders is a tool for Kotlin that turns your Java builders into beautiful Type-Safe Builders. It is compatible with Protocol Buffers, Wire, and most generic Builders.

So now, instead of this:

// Gross...
Hobbit bilbo = new Hobbit.Builder()
    .carrying(new ArrayList<>(ring, pipe, stinkweed))
    .weapon(new Weapon.Builder()

Hobbit frodo = new Hobbit.Builder(bilbo)

You can have this!

// Beautiful!
val bilbo = buildHobbit {
    firstName = "Bilbo"
    lastName = "Baggins"
    carrying = listOf(ring, pipe)
    weapon = buildWeapon {
        name = "Sting"
        size = Size.SMALL
        detects = Race.GOBLINS

val frodo = bilbo.rebuild {
    firstName = "Frodo" 

How does it work?

KBuilders scans through your Java source looking for internal classes named Builder and generates K combinator extension methods. Because all generated methods are inline, there should theoretically be no added footprint or runtime impact. And because it's Kotlin, you get all the type-safety of Java plus the elegance of JSON or Groovy.

Check out the project website for installation instructions, as well as an example of generated code.