Android Core

Hilt Basics in Android

Hilt is a dependency injection library built on top of Dagger that simplifies the setup and management of dependencies in Android applications. It provides a standard way to integrate Dagger into Android components like Activities, Fragments, ViewModels, and Services — without writing complex boilerplate code.

1. What Is Hilt?

Hilt is Google’s recommended solution for dependency injection in Android. It manages the lifecycle of dependencies automatically and connects them with the Android framework.

With Hilt, you simply annotate classes and Android components — and it generates all the necessary Dagger code for you behind the scenes.

2. Why Use Hilt?

  • Reduces the manual setup required for Dagger.
  • Automatically injects dependencies into Android classes (Activity, Fragment, etc.).
  • Provides built-in integration with ViewModel.
  • Encourages modular, testable, and clean architecture.

3. Setting Up Hilt

To start using Hilt, add the dependencies to your build.gradle files:


        // project-level build.gradle
        buildscript {
            dependencies {
                classpath "com.google.dagger:hilt-android-gradle-plugin:2.51"
            }
        }

        // app-level build.gradle
        plugins {
            id 'com.google.dagger.hilt.android'
            id 'kotlin-kapt'
        }

        dependencies {
            implementation "com.google.dagger:hilt-android:2.51"
            kapt "com.google.dagger:hilt-android-compiler:2.51"
        }
          

4. Hilt Setup in Application Class

The @HiltAndroidApp annotation triggers Hilt’s code generation, creating the base dependency container for the entire app.


        // Application class
        @HiltAndroidApp
        class MyApp : Application()
          

Don’t forget to register your Application class in AndroidManifest.xml:


        
        
          

5. Injecting Dependencies into Android Components

Use the @AndroidEntryPoint annotation to enable injection into Activities, Fragments, Services, or ViewModels.


        // Example: Injecting into Activity
        @AndroidEntryPoint
        class MainActivity : AppCompatActivity() {

            @Inject
            lateinit var repository: AuthRepository

            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                repository.loginUser()
            }
        }
          

6. Providing Dependencies with Modules

Hilt uses modules to define how to create and provide dependencies.


        // Example: Hilt Module
        @Module
        @InstallIn(SingletonComponent::class)
        object AppModule {

            @Provides
            @Singleton
            fun provideAuthRepository(): AuthRepository {
                return AuthRepository()
            }
        }
          

The @InstallIn annotation defines the scope of the module — e.g., SingletonComponent means the dependency will live as long as the application does.

7. Injecting into ViewModels

Hilt integrates seamlessly with Jetpack’s ViewModel. Use the @HiltViewModel annotation to inject dependencies into it.


        // Example: Hilt ViewModel
        @HiltViewModel
        class AuthViewModel @Inject constructor(
            private val repository: AuthRepository
        ) : ViewModel() {

            fun authenticate() = repository.loginUser()
        }
          

Then, in your Activity or Fragment:


        @AndroidEntryPoint
        class MainActivity : AppCompatActivity() {

            private val viewModel: AuthViewModel by viewModels()

            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                viewModel.authenticate()
            }
        }
          

8. Hilt Scopes

Hilt provides predefined scopes to manage dependency lifecycles efficiently:

  • @Singleton – Single instance throughout the app.
  • @ActivityScoped – One instance per activity.
  • @FragmentScoped – One instance per fragment.
  • @ViewModelScoped – One instance per ViewModel.

9. Best Practices

  • Use @HiltViewModel for all dependency-aware ViewModels.
  • Keep module functions small and focused.
  • Prefer Constructor Injection wherever possible.
  • Use Field Injection only in Android system classes (e.g., Activity, Fragment).

10. Summary

Hilt removes the complexity of manual dependency management. By leveraging annotations like @HiltAndroidApp, @AndroidEntryPoint, and @HiltViewModel, you can build scalable, testable, and maintainable Android apps.

Next: Learn about Hilt Scopes & Lifecycle Management.