Code formatting is one of those debates that drains a sprint for no real gain. Two spaces or four, where the brace sits, how imports get ordered. The answer is to stop arguing and let a tool do it.
This episode of Fragmented, an Android development podcast, focused on ktfmt—a Kotlin formatter originally released by Facebook—and Spotless, which wires it into Gradle. Together they move formatting out of pull-request comments and into the build.
At a glance
- Ktfmt rewrites Kotlin files to a consistent style.
- Spotless runs ktfmt as a Gradle task and can fail builds on unformatted code.
- Ktlint and detekt complement ktfmt by flagging style and correctness issues.
- Formatting automation frees review time and keeps a codebase uniform.
Why formatting eats team time
Most Android teams have a CONTRIBUTING.md nobody reads, a style guide that drifts, and at least one PR review that turns into a tabs-versus-spaces sermon. Every minute spent on that is a minute not spent on the actual feature. The hosts make a blunt case: hand formatting to a deterministic tool and trust the output.
That trust only works if the tool runs the same way locally and in CI—which is where Spotless comes in.
How ktfmt and Spotless fit together
Kfmt is the formatter. It rewrites your Kotlin files to a consistent style. Spotless is the glue: it runs ktfmt as a Gradle task, optionally checks formatting on every build, and can fail the build if a file drifts. The result is that unformatted code never lands on main, and reviewers stop leaving indent comments.
The episode also covered ktlint and detekt. These are not formatters—they flag style and correctness issues that ktfmt alone doesn't catch. A practical stack is ktfmt for layout, ktlint or detekt for rules, all wired through Gradle so developers run one command and the build enforces the rest.
What it means for you
| Role | What you gain |
|---|---|
| Team leads | Reviewers stop nitpicking; CI catches drift before code review |
| Junior devs | Instant local feedback—no waiting for PR comments |
| Solo devs | Code reads like one person wrote it, even after six months away |
| Contributors | A pre-commit hook means every PR looks consistent before review |
The trade-off is accepting that your favorite brace style loses. The initial setup cost is real but finite. Once wired in, the debate simply stops.
Worked example: adding Spotless to a small Android app
Say you maintain a small Kotlin Android app with two contributors. Add the Spotless Gradle plugin, point its Kotlin formatter at ktfmt, and run a one-off apply task to rewrite the codebase. Wire the check task into your CI workflow so any later unformatted file fails the build. Add a pre-commit hook that runs the same check. Within a day, every PR that comes in looks like it came from one hand.
Bottom line
Formatting is a solved problem. Adding ktfmt, wiring it through Spotless in Gradle, and running it in CI removes the churn from reviews and keeps the codebase uniform—so you can move on to the bugs that actually matter.
Key takeaways
- Hand Kotlin formatting to ktfmt and stop debating style in pull requests.
- Use Spotless as the Gradle glue that runs ktfmt locally, in CI, and on pre-commit.
- Pair ktfmt with ktlint or detekt so style and correctness rules run together.
- A one-day setup removes formatting churn from reviews and keeps the codebase uniform.
Frequently asked questions
What does ktfmt actually do?
Ktfmt is a Kotlin formatter that rewrites your Kotlin files to a consistent style, originally released by Facebook.
How does Spotless fit into a Gradle build?
Spotless runs ktfmt as a Gradle task, can check formatting on every build, and can fail the build if a file is not formatted.
Do ktlint and detekt replace ktfmt?
No. Ktlint and detekt flag style and correctness issues but are not formatters—kfmt handles layout while they handle rules.
Who benefits most from switching to automated formatting?
Android teams save review time, junior devs get instant local feedback, CI catches drift, and solo developers avoid re-reading unfamiliar styling later.
What's the minimum setup to get started?
Add the Spotless Gradle plugin, point it to ktfmt, and run SpotlessApply once to rewrite your codebase. Then add SpotlessCheck to your CI pipeline.