Anvil: Reducing Dagger 2 Boilerplate in Kotlin

Anvil from Square is a Kotlin compiler plugin that builds on top of Dagger 2. It solves Dagger's biggest ergonomic problem.

The problem with Dagger 2: Component wiring is manual. Every module has to be registered on a component by hand. There is no equivalent of Spring's auto-discovery — no way to just add a class and have it wired in automatically.

What Anvil adds: Automatic contribution and merging. Annotate a class with @ContributesTo and it's merged into the right Dagger component at compile time — no manual registration. Similar to Spring Boot starters: add the dependency, get automatic configuration.

DI evolution in the JVM: Spring DI → Guice → Dagger 2 → Anvil (on top of Dagger 2).

Dagger 2 stays for compile-time safety. Anvil adds the ergonomics. I integrated it into a pet project and deleted a significant chunk of wiring code.

Anvil on GitHub