Dependency Injection with Hilt
What is Dependency Injection?
Instead of a class creating its own helpers, it receives them. Hilt is Google's library that creates and delivers these objects for you.
kotlin
// Without DI: the ViewModel builds its own dependency (hard to test or replace)
class TodoViewModel : ViewModel() { private val repository = RealTodoRepository() }
// With DI: it receives what it needs
class TodoViewModel(private val repository: TodoRepository) : ViewModel()
Step 1: Setup
Add the Hilt plugin, the KSP plugin, and the dependencies (use the latest stable versions from the Hilt documentation).
kotlin
// app/build.gradle.kts
plugins {
alias(libs.plugins.hilt)
alias(libs.plugins.ksp)
}
dependencies {
implementation(libs.hilt.android)
ksp(libs.hilt.compiler)
implementation(libs.androidx.hilt.navigation.compose)
}
Step 2: Application and Activity
kotlin
@HiltAndroidApp
class TodoApp : Application()
// AndroidManifest.xml: <application android:name=".TodoApp" ...>
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent { AppTheme { TodoScreen() } }
}
}
Step 3: Inject
kotlin
class DefaultTodoRepository @Inject constructor(
private val dao: TodoDao
) : TodoRepository { /* ... */ }
@HiltViewModel
class TodoViewModel @Inject constructor(
private val repository: TodoRepository
) : ViewModel() { /* ... */ }
@Composable
fun TodoScreen(viewModel: TodoViewModel = hiltViewModel()) { /* ... */ }
Step 4: Modules Tell Hilt How to Build Things
kotlin
@Module
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {
@Binds
abstract fun bindTodoRepository(impl: DefaultTodoRepository): TodoRepository // interface -> implementation
}
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Provides @Singleton
fun provideOkHttp(): OkHttpClient = OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.build()
@Provides @Singleton
fun provideRetrofit(client: OkHttpClient): Retrofit = Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.client(client)
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.build()
}
| Annotation | Meaning |
|---|---|
@Inject constructor | Hilt knows how to create this class |
@Binds | Use this implementation for that interface |
@Provides | Hilt calls this function to build a type you do not own (Retrofit, Room) |
@Singleton | One instance for the whole app |
@HiltViewModel | ViewModel created by Hilt |
Testing: Replace a Module
kotlin
@HiltAndroidTest
@UninstallModules(RepositoryModule::class)
class TodoScreenTest {
@BindValue val repository: TodoRepository = FakeTodoRepository()
// the test now uses the fake
}
Alternatives: Koin is a popular, simpler library, and manual DI works for small apps. Hilt is Google's recommended option for most apps.
Common errors: Forgetting
@AndroidEntryPoint on the Activity, or android:name in the manifest, causes startup crashes. Read the first error line from Hilt, it is usually clear.
Conclusion
Hilt creates your objects, swaps implementations for tests and removes boilerplate. Annotate the Application, Activity, ViewModel and tell Hilt about interfaces in modules.
Quick Summary: @HiltAndroidApp, @AndroidEntryPoint, @HiltViewModel, @Inject constructor, and modules with @Binds and @Provides.

