Introduction to Dependency Injection and Hilt/Dagger Libraries
In modern Kotlin application development (especially for Android), one of the key architectural concepts is Dependency Injection (DI). This is a design pattern where an object receives its dependencies from the outside rather than creating them itself. This makes the code more flexible, testable, and maintainable.
Dagger is a powerful DI framework developed by Google that generates code at compile time. It guarantees that all dependencies will be provided correctly and eliminates runtime errors related to missing dependencies. However, Dagger has a steep learning curve due to its complexity.
Hilt is an abstraction layer over Dagger, created to simplify its use in Android applications. Hilt provides standard components, scope annotations, and automatic integration with the Android lifecycle (Activity, Fragment, ViewModel, etc.). If you are writing in Kotlin for Android, Hilt is the recommended choice as it significantly reduces boilerplate code.
In this article, we will cover both libraries in detail, but the main focus will be on Hilt as a more modern and convenient tool. You will learn how to set up Hilt, how to use its main annotations and methods, and we will also analyze real examples and common mistakes.
Setting up Hilt in a Kotlin/Android Project
Hilt is not installed via pip or npm. It is integrated into a Gradle project. For Kotlin and Android, the process looks like this:
1. Add the Hilt plugin to the root build.gradle (Project-level)
// build.gradle (Project)buildscript { repositories { google() mavenCentral() } dependencies { // ... other plugins classpath 'com.google.dagger:hilt-android-gradle-plugin:2.50' // Check for the latest version }}2. Apply the plugin in the app module's build.gradle (Module-level)
// build.gradle (Module: app)plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'kotlin-kapt' // Required for Hilt id 'dagger.hilt.android.plugin'}
android { // ... your configuration}
dependencies { implementation 'com.google.dagger:hilt-android:2.50' kapt 'com.google.dagger:hilt-compiler:2.50'
// For ViewModel (optional but recommended) implementation 'androidx.hilt:hilt-navigation-compose:1.1.0' // For Jetpack Compose // or implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03' // For older versions}3. Configure the Application class
Every application using Hilt must have an Application class annotated with @HiltAndroidApp.
import android.app.Applicationimport dagger.hilt.android.HiltAndroidApp
@HiltAndroidAppclass MyApplication : Application() { override fun onCreate() { super.onCreate() // Application initialization }}Don't forget to specify this class in AndroidManifest.xml:
<application android:name=".MyApplication" ... >Quick Start with Code Example
Let's create a simple example: an application that greets the user, using Hilt to inject the Greeter dependency.
Step 1: Create the class to be injected
import javax.inject.Inject
class Greeter @Inject constructor() { fun greet(name: String): String { return "Hello, $name!" }}The @Inject annotation on the constructor tells Hilt how to create an instance of this class.
Step 2: Injection into Activity
import android.os.Bundleimport android.widge