2.依赖
apply plugin: 'kotlin-kapt'
-----------------------------------------------------------------------------
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
-----------------------------------------------------------------------------
api 'com.google.dagger:dagger:2.27'
kapt 'com.google.dagger:dagger-compiler:2.27'
api 'com.google.dagger:dagger-android:2.27'
kapt 'com.google.dagger:dagger-android-processor:2.27'
2.概念
1.声明者
如activity中声明,我需要...
class MainActivity : AppCompatActivity() {
@Inject
lateinit var cloth: Cloth
@Inject
lateinit var cloth1: Cloth1······
2.注入器Component
如MainComponent,绑定Module。自定义inject方法,传入声明者。
@Component(modules = [MainModule::class])
interface MainComponent {
fun inject(mainActivity: MainActivity)
}
3.提供者Module
@Provides自定义的方法,或@Inject构造函数,提供对象
@Module
class MainModule {
@Provides
fun b(): Cloth1 {
val cloth1 = Cloth1("彩色")
return cloth1
}
}
data class Cloth constructor(var color: String = "") {
@Inject
constructor() : this("构造函数的")
override fun toString(): String {
return "${color}布料"
}
}