0
0
Android Kotlinmobile~5 mins

Dependency injection with Hilt in Android Kotlin

Choose your learning style9 modes available
Introduction

Dependency injection helps your app get the things it needs without creating them itself. Hilt makes this easy and organized in Android apps.

When you want to share the same object across different parts of your app without creating it multiple times.
When you want to make your code easier to test by providing fake or mock objects.
When your app has many classes that depend on each other and you want to manage them cleanly.
When you want to reduce boilerplate code for creating and passing dependencies manually.
Syntax
Android Kotlin
1. Add @HiltAndroidApp to your Application class.
2. Use @AndroidEntryPoint on Activities or Fragments.
3. Create modules with @Module and @InstallIn annotations.
4. Provide dependencies with @Provides or @Inject constructors.
5. Use @Inject to get dependencies in your classes.

@HiltAndroidApp triggers code generation for dependency injection setup.

@AndroidEntryPoint tells Hilt where to inject dependencies in Android components.

Examples
This marks your Application class to start Hilt.
Android Kotlin
@HiltAndroidApp
class MyApp : Application()
This injects UserRepository into MainActivity automatically.
Android Kotlin
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
  @Inject lateinit var repository: UserRepository
}
This module tells Hilt how to create a Retrofit instance.
Android Kotlin
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
  @Provides
  fun provideRetrofit(): Retrofit {
    return Retrofit.Builder().baseUrl("https://api.example.com").build()
  }
}
This shows constructor injection where Hilt provides Retrofit automatically.
Android Kotlin
class UserRepository @Inject constructor(private val retrofit: Retrofit) {
  // Use retrofit here
}
Sample App

This example shows a simple Android app using Hilt. The GreetingService is provided by a module and injected into MainActivity. When the app runs, it prints a greeting message.

Android Kotlin
import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class MyApp : Application()

import androidx.activity.ComponentActivity
import android.os.Bundle
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @Inject lateinit var greetingService: GreetingService

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        println(greetingService.greet())
    }
}

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object GreetingModule {
    @Provides
    @Singleton
    fun provideGreetingService(): GreetingService = GreetingService()
}

class GreetingService {
    fun greet() = "Hello from Hilt!"
}
OutputSuccess
Important Notes

Always add @HiltAndroidApp to your Application class once per app.

Use @AndroidEntryPoint on every Activity or Fragment where you want injection.

Modules define how to create objects that Hilt cannot create by itself.

Summary

Hilt helps manage dependencies automatically in Android apps.

Use annotations like @HiltAndroidApp, @AndroidEntryPoint, @Module, and @Inject.

This makes your code cleaner, easier to test, and reduces manual setup.