Почему я реализую Фабрику в этом коде, когда Dagger2 не работает? - PullRequest
0 голосов
/ 27 апреля 2019

Я прошу прощения за простой код о кинжале в kotlin, но он не работает во время выполнения. Что не так и как обновить этот код?

У меня есть класс уведомлений, и я хочу выполнить runNotification.


    interface Notifications {

        fun runNotification()
    }

    class NotificationsImpl @Inject constructor(
        private val context: Application
    ) : Notifications {

        override fun runNotification() {
            Log.e("Notifications", "runNotification")
        }
    }

NotificationModule.kt


        @Module
    class NotificationModule {
            @Provides
            @Singleton
            fun providerNotifications(notificationsImpl: NotificationsImpl): Notifications = notificationsImpl
        }

ApplicationComponent.kt


    @Singleton
    @Component(modules = [NotificationModule::class])
    interface ApplicationComponent {

        @Component.Builder
        interface Builder {
            @BindsInstance
            fun application(application: Application): Builder

            @BindsInstance
            fun notificationModule(notificationModule: NotificationModule): Builder

            fun build(): ApplicationComponent
        }

        fun inject(application: Application)
    }

Код в приложении


    DaggerApplicationComponent.builder()
                .application(this)
                .notificationModule(NotificationModule())
                .build()
                .inject(this)

Проверка кода (возврат этого кода «уведомлений» равен NULL):


class MainActivity : AppCompatActivity() {

    @Inject
    lateinit var notifications: Notifications

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        notifications.updateNotification()
    }
}

Gradle:


    implementation "com.google.dagger:dagger:2.17"
    kapt "com.google.dagger:dagger-compiler:2.17"

    implementation "com.google.dagger:dagger-android:2.17"
    kapt "com.google.dagger:dagger-android-processor:2.17"

    kapt "com.google.dagger:dagger-android-support:2.17"

...