基本上,我想测试TheCoffee
是否自动"注入"。但我找不到DaggerMyComponent
。我想我一定做错了什么,但是什么?
在MainActivity下,
@Component(modules=arrayOf(MyModule::class))
@Singleton
interface MyComponent
{
fun inject(coffee: CoffeeShop)
}
@Module
class MyModule
{
@Provides
@Singleton
@ForApplication
fun provideCoffee():Coffee
{
return Coffee("Bad coffee");
}
}
data class Coffee(var Name:String)
class CoffeeShop
{
@Inject
var TheCoffee:Coffee? = null;
}
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ForApplication
的build.gradle
apply plugin: 'kotlin-kapt'
...
kapt {
generateStubs = true
}
....
dependencies {
compile 'com.google.dagger:dagger-android:2.13'
kapt 'com.google.dagger:dagger-android-processor:2.13'
compileOnly 'com.google.dagger:dagger:2.13'
kapt 'com.google.dagger:dagger-compiler:2.13'