Dagger2 (dependency injection)
Dagger2 is a library that allows us to achieve dependency injection. It has three main concepts.
@Inject
tells dagger which variables to inject.@Module
defines how to create the objects that we want to inject.@Component
links the two together.
When we make any changes to the data code, we need to rebuild the project and the system will generate for us the classes that we need in order to perform the injections.
Implementation
Go into app.build
and add the following
apply plugin: 'kotlin-kapt'
def daggerVersion = '2.13'
dependencies {
implementation "com.google.dagger:dagger:$daggerVersion"
implementation "com.google.dagger:dagger-android-support:$daggerVersion
// kotlin annotation processor
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
}