Anvil for Kotlin: Complete Guide with Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Anvil for Kotlin: Complete Guide with Code Examples

1. What Is Anvil and Why Use It

Anvil is a code generation library for Kotlin that works on top of the Dagger and Hilt dependency injection (DI) frameworks. Its main goal is to eliminate the boilerplate code associated with DI modules, components, factories, and entry points. Instead of manually creating and annotating Dagger modules, you simply mark your classes with Anvil annotations, and the library generates all the necessary code at compile time.

Why is this useful? In large Android or Kotlin Multiplatform projects, the number of classes that need to be registered in the dependency graph can reach hundreds. Adding a new Dagger module each time, writing @Provides or @Binds — it's slow, error-prone, and makes refactoring harder. Anvil solves this by automating module creation based on simple annotations. This is especially valuable in feature-module architectures, where each module needs to be built and tested independently.

Anvil integrates tightly with Gradle and uses Kotlin Symbol Processing (KSP) or KAPT for code generation. It supports both Dagger and Hilt and works in multiplatform projects. The library is developed by Square, the company behind popular Android tools like Retrofit, OkHttp, and Moshi.

2. Installation

To add Anvil to your Kotlin/Gradle project, include the following dependencies in your build.gradle.kts file (project-level):

// In root build.gradle.kts
plugins {
    id("com.google.dagger.hilt.android") version "2.48" apply false
    id("com.squareup.anvil") version "2.4.7" apply false
}

Then, in your app module (e.g., app/build.gradle.kts):

plugins {
    id("com.android.application")
    kotlin("android")
    id("com.google.dagger.hilt.android")
    id("com.squareup.anvil")
}

android {
    // ... your Android configuration
}

dependencies {
    // Hilt
    implementation("com.google.dagger:hilt-android:2.48")
    kapt("com.google.dagger:hilt-compiler:2.48")

    // Anvil
    implementation("com.squareup.anvil:annotations:2.4.7")
    ksp("com.squareup.anvil:compiler:2.4.7") // or kapt if not using KSP
}

If you're using KSP instead of KAPT, make sure the KSP plugin is applied:

plugins {
    id("com.google.devtools.ksp") version "1.9.0-1.0.13"
}

After syncing Gradle, Anvil is ready to use.

3. Quick Start — Minimal Working Example

Let's walk through a simple example: we have a UserRepository class that needs to be injected into MainViewModel.

Step 1. Create the class to be injected:

// UserRepository.kt
class UserRepository @Inject constructor() {
    fun getUser(): String = "John Doe"
}

Step 2. Create the class that uses the dependency:

// MainViewModel.kt
class MainViewModel @Inject constructor(
    private val userRepository: UserRepository
) {
    fun getUserName(): String = userRepository.getUser()
}

Step 3. Create a Hilt (or Dagger) component using Anvil:

// AppComponent.kt
@Singleton
@Component
@MergeComponent(AppScope::class) // Anvil annotation
interface AppComponent {
    fun inject(app: MyApplication)
}

// AppScope.kt (define the scope)
@Scope
@MustBeDocumented
annotation class AppScope

Step 4. Register the classes in the dependency graph with Anvil annotations:

// UserRepository.kt (add                    

Recommendations