Mastering Hilt - Android | Kotlin | Hilt (Dagger 2) [Part 7]
Becoming Proficient in Dependency Injection with Hilt for Android (Kotlin)
![Mastering Hilt - Android | Kotlin | Hilt (Dagger 2) [Part 7]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1697042554263%2F24bf9174-5807-4be0-aa01-dbab9ebc2878.jpeg&w=3840&q=75)
Senior Android Engineer from Bangladesh. Love to contribute in Open-Source. Indie Music Producer.
Search for a command to run...
Becoming Proficient in Dependency Injection with Hilt for Android (Kotlin)
![Mastering Hilt - Android | Kotlin | Hilt (Dagger 2) [Part 7]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1697042554263%2F24bf9174-5807-4be0-aa01-dbab9ebc2878.jpeg&w=3840&q=75)
Senior Android Engineer from Bangladesh. Love to contribute in Open-Source. Indie Music Producer.
Hi, thanks for your sample, and explanatio. I want to know where should be defined those new annotations? inside the same module where it can be used?
You can place the code in a Kotlin Class/File under the same package. Just make sure the new annotations is accessible.
This course focuses on mastering Dependency Injection (DI) using Hilt, a widely used library in Android development.
Becoming Proficient in Dependency Injection with Hilt for Android (Kotlin)
A practical guide to choosing the right coroutine primitive for your Android/Kotlin projects

This guide explains what Channels are, when to use them, how to use them correctly, and when NOT to use them β in simple, professional language. 1. What problem do Channels solve? In Kotlin coroutines, you often have multiple coroutines running at t...

Learn how to build preview-safe ViewModels in Jetpack Compose using Hilt.

HandlerInterceptor vs Filter in Spring Boot: A Practical Guide with JNDI Injection Prevention

Simple In-Memory Caching: A Tiny Trick with Massive Impact

Qualifiers in Hilt are used to disambiguate between multiple bindings of the same type. They allow us to differentiate between different implementations of an interface or class, ensuring that the correct dependency is provided when injecting.
To use qualifiers, we define our custom annotations and annotate the corresponding binding methods or fields in Hilt modules.
Example - Using Qualifiers in Hilt:
1. Define custom qualifiers:
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class FirstImplementation
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class SecondImplementation
2. Create Hilt modules with qualified bindings:
@Module
@InstallIn(SingletonComponent::class)
object MyModule {
@Singleton
@FirstImplementation
@Provides
fun provideFirstDependency(): MyDependency {
return FirstImplementationDependency()
}
@Singleton
@SecondImplementation
@Provides
fun provideSecondDependency(): MyDependency {
return SecondImplementationDependency()
}
}
In this example, we define two custom qualifiers, @FirstImplementation and @SecondImplementation. Then, we create a Hilt module, MyModule, with two different @Provides methods, each annotated with a different qualifier.
3. Inject qualified dependencies:
@AndroidEntryPoint
class MyActivity : AppCompatActivity() {
@FirstImplementation
@Inject
lateinit var firstDependency: MyDependency
@SecondImplementation
@Inject
lateinit var secondDependency: MyDependency
// Use firstDependency and secondDependency as needed
}
In the Activity, we use the @FirstImplementation and @SecondImplementation qualifiers to indicate which implementation of MyDependency should be injected for firstDependency and secondDependency, respectively.
By using qualifiers, we can easily switch between different implementations of the same type when injecting dependencies. This is particularly useful when we have multiple implementations of an interface or class and want to specify which one to use in different parts of our application.