添加AndroidInjector时匕首缺少绑定

时间:2019-07-01 09:48:55

标签: android kotlin dependency-injection dagger-2

我遇到以下问题:我开始研究Dagger 2,并尝试将注入添加到我的App类中。这是我的App类的代码:

class TraktTvApp : Application(), HasActivityInjector {

    @Inject
    lateinit var androidInjector: AndroidInjector<Activity>

    override fun activityInjector(): AndroidInjector<Activity>? = androidInjector

    override fun onCreate() {
        super.onCreate()

        appContext = applicationContext
        instance = this

        initStetho()
        initFabric()

        DaggerAppComponent
                .builder()
                .application(this)
                .build()
                .inject(this)

        FirebaseInstanceId.getInstance().instanceId
                .addOnCompleteListener(OnCompleteListener { task ->

                    if (!task.isSuccessful) {
                        return@OnCompleteListener
                    }

                    val token = task.result?.token
                }
                )
    }

    private fun initFabric() {
        Fabric.with(this, Crashlytics(), CrashlyticsNdk())
    }

    private fun initStetho() {
        if (BuildConfig.DEBUG) {
            Stetho.initializeWithDefaults(this)
        }
    }

    companion object {
        lateinit var instance: TraktTvApp
        lateinit var appContext: Context
    }

}

在此类中,我将appContext作为Property并尝试用注入替换它。为此,我添加了以下行

DaggerAppComponent             .builder()             .application(this)             。建立()             .inject(this)

构建我的应用程序时一切正常,但是添加后

    @Inject
    lateinit var androidInjector: AndroidInjector<Activity>

    override fun activityInjector(): AndroidInjector<Activity>? = androidInjector

并尝试构建时出现此错误:

public abstract interface AppComponent {
                ^
      dagger.android.AndroidInjector<android.app.Activity> is injected at
          com.mikhailovskii.trakttv.TraktTvApp.androidInjector
      com.mikhailovskii.trakttv.TraktTvApp is injected at
          com.mikhailovskii.trakttv.di.AppComponent.inject(com.mikhailovskii.trakttv.TraktTvApp)

我已经看到了可能的解决方案,但是没有一个对我有帮助。这是我的AppModule的代码:

@Module
class AppModule {

    @Singleton
    @Provides
    fun provideContext(application: TraktTvApp): Context {
        return application.applicationContext
    }

}

这是AppComponent

@Singleton
@Component(
        modules = [
            AndroidSupportInjectionModule::class,
            AppModule::class
        ]
)
interface AppComponent {

    @Component.Builder
    interface Builder {

        @BindsInstance

        fun application(application: TraktTvApp): Builder

        fun build():AppComponent

    }

    fun inject(application: TraktTvApp)

}

那么,有什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:2)

您不需要手动添加AndroidInjector。您可以使用Dagger基类,因此Dagger将为您处理

您应该从Application扩展DaggerApplication

 class MyApp: DaggerApplication() {

    /**
     * Returns the injector
     */
    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {

        val appComponent = DaggerAppComponent.builder().application(this).build()
        appComponent.inject(this)

        return appComponent

    }

您的Main Component也扩展了AndroidInjector

/**
 * The main component that holds and services all modules within its builder.
 */
@Singleton
@Component(modules = [AndroidSupportInjectionModule::class])
interface AppComponent : AndroidInjector<MyApp> {

    /**
     * A {@see [Component.Builder]} that initializes necessary implementations
     */
    @Component.Builder
    interface Builder {

        @BindsInstance
        fun application(application : Application) : Builder
        fun build() : AppComponent
    }

}

如果要使用上下文,可以将其添加到您的AppModule

@Module
class AppModule {

    @Singleton
    @Provides
    fun provideContext(application: Application): Context {
        return application.applicationContext
    }

}