Dagger Hilt With?Android
Anand Gaur
Mobile Tech Lead @ TCS | Android Kotlin & Jetpack Compose | Ex-Samsung & Tech Mahindra | 22K+ Connections | Technical Blogger on Medium
What is Dagger?Hilt?
Dagger Hilt is a dependency injection (DI) library for Android that simplifies Dagger setup by providing a standardized way to implement DI in Android applications. Hilt is built on top of Dagger and is specifically designed to address the common patterns in Android, making dependency injection easier and more intuitive.
Key Concepts in Dagger?Hilt
Scopes in?Hilt
Hilt allows you to define the scope of dependencies, which determines how long the dependency should live. Some common scopes are:
Implementing Dagger Hilt in an Android?Project
Here’s a step-by-step guide to implementing Dagger Hilt in an Android project:
Step 1: Add Hilt Dependencies
Add the Hilt dependencies to your build.gradle file:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
android {
...
}
dependencies {
implementation 'com.google.dagger:hilt-android:2.44'
kapt 'com.google.dagger:hilt-android-compiler:2.44'
// ViewModel support
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
}
Make sure to apply the Hilt plugin at the top of your build.gradle:
apply plugin: 'dagger.hilt.android.plugin'
Step 2: Create the Application Class
Create an Application class and annotate it with @HiltAndroidApp. This is required to initialize Hilt in your project.
@HiltAndroidApp
class MyApplication : Application() {
}
Step 3: Inject Dependencies in an?Activity
You can now inject dependencies into your Android components (e.g., Activity, Fragment) by annotating them with @AndroidEntryPoint:
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var userRepository: UserRepository
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Now you can use the injected userRepository
userRepository.doSomething()
}
}
Step 4: Create a Module to Provide Dependencies
Modules are used to provide dependencies that Hilt cannot create automatically (e.g., instances of interfaces). Create a module with the @Module and @InstallIn annotations:
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
@Singleton
fun provideUserRepository(): UserRepository {
return UserRepositoryImpl()
}
}
In this example, SingletonComponent ensures that the UserRepository is a singleton throughout the entire application lifecycle.
Step 5: Use Hilt with ViewModel
To inject dependencies into a ViewModel, use the @HiltViewModel annotation and inject the required dependencies into the constructor:
@HiltViewModel
class MyViewModel @Inject constructor(
private val userRepository: UserRepository
) : ViewModel() {
fun loadData() {
// Use userRepository to load data
}
}
In your Activity or Fragment, retrieve the ViewModel using the Hilt-aware ViewModelProvider:
领英推荐
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private val viewModel: MyViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel.loadData()
}
}
Advantages of Using Dagger?Hilt
Interview Questions Answer about Dagger?Hilt
1. What is Dagger Hilt, and how does it differ from?Dagger?
?Dagger Hilt is a dependency injection library built on top of Dagger that simplifies Dagger’s setup in Android projects. It provides a more streamlined and Android-specific integration of Dagger, with predefined components for Android classes like Activity, Fragment, ViewModel, and Service. Hilt reduces boilerplate code, manages the dependency injection lifecycle, and provides built-in support for common Android patterns, making it easier to integrate dependency injection into Android applications.
2. What are the key annotations provided by Dagger?Hilt?
3. How does Dagger Hilt manage the lifecycle of dependencies?
Dagger Hilt provides predefined components that are tied to the lifecycle of Android components like Activity, Fragment, and ViewModel. Each Hilt component corresponds to a specific scope, such as @Singleton for the application-level scope, @ActivityRetainedScoped for the activity-level scope, and @ViewModelScoped for the ViewModel-level scope. These scopes ensure that dependencies are created and destroyed according to the lifecycle of the associated component.
4. What is the purpose of the @AndroidEntryPoint annotation in?Hilt?
The @AndroidEntryPoint annotation is used in Dagger Hilt to specify that a particular Android component (like Activity, Fragment, Service, or BroadcastReceiver) will be a target for dependency injection. It allows Hilt to automatically inject dependencies into the annotated class, eliminating the need for manual injection setups and boilerplate code.
5. Can you explain the @HiltViewModel annotation and how it is?used?
The @HiltViewModel annotation is used in Dagger Hilt to inject dependencies into an Android ViewModel. It allows Hilt to manage the lifecycle of the ViewModel and inject the required dependencies into it. The @HiltViewModel annotation is used in conjunction with @Inject in the ViewModel's constructor, and the ViewModel is then provided by Hilt in the Activity or Fragment using the ViewModelProvider.
@HiltViewModel
class MyViewModel @Inject constructor(
private val repository: UserRepository
) : ViewModel() {
// ViewModel logic
}
6. How does Hilt handle dependency injection in a multi-module project?
In a multi-module project, Hilt allows you to define modules and components specific to each module. You can use the @InstallIn annotation to specify the component (e.g., SingletonComponent, ActivityComponent) in which the module should be installed. This modular approach allows each feature module to have its dependencies managed independently while still sharing common dependencies from the app module.
7. How do you migrate from Dagger to Dagger?Hilt?
To migrate from Dagger to Dagger Hilt, you generally follow these steps:
8. What are the limitations of Dagger?Hilt?
Some limitations of Dagger Hilt include:
9. How does Dagger Hilt work with Jetpack components like ViewModel, WorkManager, and Navigation?
Dagger Hilt provides direct support for Jetpack components:
Summary
Dagger Hilt simplifies dependency injection in Android applications by providing an easy-to-use and lifecycle-aware DI framework. With Hilt, you can manage dependencies efficiently across different Android components, reduce boilerplate, and improve the modularity and testability of your code.
Follow me on Github for more updates.
If you have any Doubt you can connect On WhatsApp.