简体   繁体   中英

Android: Kotlin with Dagger activity module issue

I want to inject activity on some class with Dagger 2 . So I was added my project to ActivityModule class. When I was build my project I get an error:

.... AppComponent.java:13: error: android.app.Activity cannot be provided without an @Inject constructor or from an @Provides-annotated method.

All other modules work correctly. Only when I was add activity module on the project then I getting above error :)

class App : Application(), HasActivityInjector {

    @field:Inject lateinit var component: AppComponent
    @field:Inject
    lateinit var injector: DispatchingAndroidInjector<Activity>
    override fun activityInjector() = injector

    override fun onCreate() {
        super.onCreate(); let { instance = this }

        DaggerAppComponent.builder()
                .appModule(AppModule(this))
                .build()
                .apply { inject(this@App); component = this }
                .inject(this)

        ......
    }

    private fun registerCallBack() = registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {

        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            component.inject(activity)
        }

    ...
    ...
    }

}

@AppScope
@Component(modules = arrayOf(
        AndroidInjectionModule::class,
        AndroidSupportInjectionModule::class,
        ActivityBuilderModule::class,
        FragmentBuilderModule::class,
        AppModule::class,
        ActivityModule::class)
)
interface AppComponent {

    interface Builder {
        @BindsInstance
        fun application(app: App): Builder
        fun build(): AppComponent
    }


    @ApplicationContext
    fun getContext(): Context
    fun getActivity(): Activity

    fun inject(application: App)
    fun inject(activity: Activity)

}

@Module()
class ActivityModule(private val activity: Activity) {

    @Provides
    @PerActivity
    @ActivityContext
    fun provideActivity() = activity

    @Provides
    @PerActivity
    @ActivityContext
    fun provideContext() = activity

}

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class AppScope

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class PerActivity

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class PerFragment

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityContext

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ApplicationContext

class HomeFragment : BaseFragment(){

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        ...
        val example = (context as App).component.getActivity()
        ...
    }

}

This line is the problem.

fun getActivity(): Activity

AppComponent provides Activity but non of its modules @Provides one and Activity does not have a constructor that have @Inject .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM