Kotlin Dagger-Hilt | Interface | @Module @Binds
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=falseStep 4:Create new class BaseApp.kt and add following code.BaseApp.ktpackage `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 interface Demo.kt and add following code.Demo.ktpackage `in`.kotlinkatta.kotlinkattademo
import android.util.Log
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Inject
import javax.inject.Singleton
interface Demo {
fun demo()
}
class DemoImplementation @Inject constructor() : Demo {
override fun demo() {
Log.d("main","demo is running.")
}
}
class MainDemo @Inject constructor(val demo: Demo) {
fun mainDemo() {
demo.demo()
}
}
@Module
@InstallIn(SingletonComponent::class)
abstract class Module {
@Binds
@Singleton
abstract fun provides(
demoImplementation: DemoImplementation
): Demo
}Step 7:Open class MainActivity.kt and add following code.MainActivity.ktpackage `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 mainDemo: MainDemo
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mainDemo.mainDemo()
}
}
Comments
Post a Comment