Why @ContributesAndroidInjector doesn't provide Android Framework type

后端 未结 2 713
梦谈多话
梦谈多话 2021-01-28 03:03

I have simplified my application to get the root of the problem and here is the simplified version. I\'m implementing Dagger 2 using following configuration:

App

2条回答
  •  面向向阳花
    2021-01-28 03:35

    What I don't understand is ContributesAndroidInjector is already providing an instace of MainActivity why Dagger still complains about it.

    ContributesAndroidInjector in docs says:

    Generates an {@link AndroidInjector} for the return type of this method. The injector is implemented with a {@link dagger.Subcomponent} and will be a child of the {@link dagger.Module}'s component.

    So it does not provide MainActivity.

    Why do you need it actually at all? I see that you are passing it as parameter to the function:

    fun provideString(mainActivity: MainActivity)

    but do you really need it there? In general you should inject dependencies in MainActivity. MainActivity should use them. If both(MainActivity and the string) they know about each other it is first - not a good design, and second: you are close to create cyclic dependencies which Dagger 2 does not support and throws exception.

提交回复
热议问题