Kotlin Dagger-Hilt | @Qualifier Annotation |(Part 5)

  Kotlin Dagger-Hilt | @Qualifier Annotation |

Step 1:
Open build.gradle(Project) and add following dependency and rebuild the project.
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.5'
build.gradle(Project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
Step 2:
Open build.gradle(:app) and add following dependency and rebuild the project.
plugins {
id 'dagger.hilt.android.plugin'
}
dependencies {
//DaggerHilt
implementation 'com.google.dagger:hilt-android:2.40.5'
kapt 'com.google.dagger:hilt-compiler:2.40.5'
}
build.gradle(app)
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
android {
compileSdk 31

defaultConfig {
applicationId "in.kotlinkatta.kotlinkattademo"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}

buildFeatures{
dataBinding true;
viewBinding true
}
}

dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
//DaggerHilt
implementation 'com.google.dagger:hilt-android:2.40.5'
kapt 'com.google.dagger:hilt-compiler:2.40.5'
}
// Allow references to generated code
kapt {
correctErrorTypes = true
}
Step 3:
Open gradle.properties and add following code and rebuild the project.
kapt.use.worker.api=false
Step 4:
Create new class BaseApp.kt and add following code.
BaseApp.kt
package `in`.kotlinkatta.kotlinkattademo

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

@HiltAndroidApp
class BaseApp : Application() {
}
Step 5:
Open AndroidManifest.xml and add following code.
<application
android:name=".BaseApp"
</application>
Step 6:
Create new class Qualifier.kt and add following code.
Qualifier.kt
package `in`.kotlinkatta.kotlinkattademo

import android.util.Log
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Inject
import javax.inject.Qualifier

class Qualifier @Inject constructor(@fName val fName: String, @lName val lName: String) {

fun getNames() {
Log.d("main", "$fName $lName")
}
}

@Module
@InstallIn(SingletonComponent::class)
object Modules {

@Provides
@fName
fun providesfName() = "Vicky"

@Provides
@lName
fun provideslName() = "Sachin"

}

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class fName

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class lName
Step 7:
Open class MainActivity.kt and add following code.
MainActivity.kt
package `in`.kotlinkatta.kotlinkattademo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
public class MainActivity : AppCompatActivity() {

@Inject
lateinit var qualifier: Qualifier

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
qualifier.getNames()
}
}

Comments