Extending Anvil for Fun and Profit

Note: This article assumes some familiarity with Dagger, Anvil, and Kotlin. We use Dagger heavily in the Slack Android app for compile-time dependency injection. It’s powerful, flexible, supports basic Kotlin idioms, and allows for advanced dependency injection patterns with less boilerplate. It’s not without its sharp edges though. It slows down our builds with kapt,…

Zac Sweers
9 min readintermediate
--
View Original

Overview

This article discusses the extension of Anvil to enhance Dagger dependency injection patterns within the Slack Android app. It highlights the challenges faced with Dagger, the benefits of using Anvil, and provides a detailed implementation example of Activity injection patterns.

What You'll Learn

1

How to extend Anvil's code generation for custom dependency injection patterns

2

Why to use Anvil for reducing boilerplate in Dagger dependency injection

3

When to implement custom Anvil injectors for specific use cases

Prerequisites & Requirements

  • Familiarity with Dagger, Anvil, and Kotlin

Key Questions Answered

What are the advantages of using Anvil with Dagger?
Anvil augments Dagger by reducing boilerplate code, enabling factory generation, and allowing automatic wiring of dependencies without modules. This leads to improved build times and a more streamlined dependency injection process, which can reduce incremental build times by as much as 25%.
How does the custom Anvil injector work for Activity injection?
The custom Anvil injector uses the @InjectWith annotation to automatically generate the necessary boilerplate code for Activity injection. This simplifies the process for developers, allowing them to focus on business logic rather than wiring dependencies manually.
What challenges does Dagger present in dependency injection?
Dagger can slow down builds due to kapt, has a steep learning curve, and requires tedious module and component plumbing. These challenges necessitate the need for tools like Anvil to streamline the process and reduce boilerplate.
When should you consider using Anvil in your codebase?
Anvil should be considered when you want to reduce boilerplate in Dagger, improve build performance, and simplify dependency injection patterns in complex applications. It is especially useful in codebases with specific patterns that require customization.

Key Statistics & Figures

Reduction in incremental build times
25%
This improvement was achieved through factory generation enabled by Anvil.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Dependency Injection
Dagger
Used for compile-time dependency injection in the Slack Android app.
Code Generation
Anvil
Augments Dagger to reduce boilerplate and improve dependency injection patterns.
Programming Language
Kotlin
The primary language used for implementing the dependency injection patterns discussed in the article.

Key Actionable Insights

1
Implementing Anvil in your project can significantly reduce the boilerplate code associated with Dagger, making your codebase cleaner and easier to maintain.
This is particularly beneficial in large projects where dependency injection can become cumbersome, allowing developers to focus on writing functional code rather than managing dependencies.
2
Utilizing the compiler API in Anvil enables you to customize code generation to fit your specific needs, which can lead to better performance and reduced build times.
This is crucial for teams looking to optimize their build processes and improve developer productivity.

Common Pitfalls

1
Failing to properly configure Anvil can lead to increased boilerplate rather than reducing it.
This often happens when developers do not fully leverage Anvil's capabilities, resulting in unnecessary complexity in their dependency injection setup.

Related Concepts

Dependency Injection
Code Generation
Kotlin Programming