Dagger 2 does not generate the component class (An

2019-07-25 17:01发布

Basically, I wanted to test if TheCoffee was automatically "injected". But I could not find DaggerMyComponent. I think I must have done something wrong, but what?

Under the 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'

0条回答
登录 后发表回答