Skip to main content

Command Palette

Search for a command to run...

Mastering Hilt - Android | Kotlin | Hilt (Dagger 2) [Part 7]

Becoming Proficient in Dependency Injection with Hilt for Android (Kotlin)

Updated
2 min read
Mastering Hilt - Android | Kotlin | Hilt (Dagger 2) [Part 7]
R

Senior Android Engineer from Bangladesh. Love to contribute in Open-Source. Indie Music Producer.

Qualifiers in Hilt

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.

A

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?

R

You can place the code in a Kotlin Class/File under the same package. Just make sure the new annotations is accessible.

Dependency Injection w/ Hilt

Part 4 of 12

This course focuses on mastering Dependency Injection (DI) using Hilt, a widely used library in Android development.

Up next

Mastering Hilt - Android | Kotlin | Hilt (Dagger 2) [Part 6]

Becoming Proficient in Dependency Injection with Hilt for Android (Kotlin)