Meet AutoBuilder: The KSP Plugin to Automate Your Builders

Matthew Shoemaker
2 min readJul 7, 2024

--

Do you ever find yourself avoiding writing builders for your data classes because they are difficult to maintain and take a lot of time to write for large classes? Maybe you find yourself just dreading the tedium of the times you do have to write builders. Or maybe you find yourself stubbing out classes in your tests that require 20–30 constructor params and thought “There’s gotta be a better way”…

I’d like to introduce you to AutoBuilder!

What is AutoBuilder?

AutoBuilder is a lightweight KSP plugin available in Maven Central that automatically generates builder classes for your Kotlin data classes. No more tedious hand-written builders — just annotate your data class, and AutoBuilder does the rest. It’s like having a personal assistant who handles all the boring stuff, so you can focus on the fun parts of coding.

Why Use AutoBuilder?

  1. Saves Time: Imagine the hours you can save by not writing repetitive builder code. Or maybe you just avoid writing builders altogether. Well, AutoBuilder does the heavy lifting for you.
  2. Reduces Errors: Manual code is prone to mistakes. By automating the generation, you ensure consistent and error-free builders.
  3. Enhances Readability: Generated builders follow a clean and consistent pattern, making your codebase easier to read and maintain.
  4. Customizable Defaults: AutoBuilder supports default values for all primitive and non-primitive types, ensuring that your builders are robust and reliable.
  5. Simpler Tests: we’ve all found ourselves writing stubbed objects with 30 parameters in our tests and thought to ourselves that a builder would save us a ton of time.

How Does It Work?

Using AutoBuilder is as simple as annotating your data class. Here’s a quick example to show you how:

package com.example

import io.github.mattshoe.shoebox.autobuilder.annotations.AutoBuilder

@AutoBuilder
data class MyDataClass(
val name: String,
val age: Int
)

With this annotation, AutoBuilder will generate a MyDataClassBuilder class, allowing you to build instances of MyDataClass like this:

val myData = MyDataClassBuilder().name("John Doe").age(30).build()

Key Features

  • Support for Primitive Properties: Handles all Kotlin primitive types, including nullable versions.
  • Support for Complex Object Properties: Supports non-primitive properties via either a no-arg constructor or the @DefaultConstructor annotation to specify constructor arguments.
  • Custom Default Values: Supports custom default values specified through annotations.

Installation

To get started with AutoBuilder, add it to your build.gradle.kts:

plugins {
kotlin("jvm")
id("com.google.devtools.ksp") version "2.0.0-1.0.21" // Use your judgment on version here
}

repositories {
mavenCentral()
}

dependencies {
ksp("io.github.mattshoe.shoebox.autobuilder:AutoBuilder.Processor:1.1.0")
compileOnly("io.github.mattshoe.shoebox.autobuilder:AutoBuilder.Annotations:1.1.0")
}

Conclusion

AutoBuilder is here to make your Kotlin development faster, cleaner, and more enjoyable. Say goodbye to boilerplate builder code and hello to a more productive coding experience. Give AutoBuilder a try and see how it can transform your workflow!

Stay tuned for more updates and features. Happy coding!

Feel free to share your feedback and contribute to the project on GitHub.

--

--

Matthew Shoemaker

Senior Android Developer driven by a passion for learning and sharing knowledge. Engaging with the developer community through blogs and open-source projects.