I know there is a lot of question about this subject but I have already browse them all and tried all day but I still don't find a solution for my problem. I'm fearly new with dagger but the project I'm working on is using it, and it was working well until we have this case :
We have an abstract fragment that inject a provider (in a MVP architecture) and 3 fragments that extend this fragment (no other injection in those).
I have the following code :
abstract class Fragment1: DaggerFragment(), Fragment1Contract.View {
//region Properties
@Inject
lateinit var presenterFragment1Contract: Fragment1Contract.Presenter<Fragment1Contract.View>
And the 3 fragments that extends this fragment, without much code (I tried to add AndroidSupportInjection.inject(this) in the onAttach method of those fragment but that didn't help).
I have this module :
@Module
open class Fragment1Module {
@Provides
internal fun provideFragment1View(fragment: Fragment1): Fragment1Contract.View {
return fragment
}
@Provides
internal fun provideFragment1Presenter(view: Fragment1Contract.View): Fragment1Contract.Presenter<Fragment1Contract.View> {
return Fragment1Presenter(view)
}
}
And this one :
@Module
abstract class ActivityBindingModule {
@PerActivity
@ContributesAndroidInjector(modules = arrayOf(Fragment1Module::class))
abstract fun bindFragment1(): Fragment1
@PerActivity
@ContributesAndroidInjector(modules = arrayOf(Fragment1Module::class))
abstract fun bindFragment2(): Fragment2
@PerActivity
@ContributesAndroidInjector(modules = arrayOf(Fragment1Module::class))
abstract fun bindFragment3(): Fragment3
@PerActivity
@ContributesAndroidInjector(modules = arrayOf(Fragment1Module::class))
abstract fun bindFragment4(): Fragment4
}
and finally this component :
@PerApplication
@Component(modules = [ActivityBindingModule::class, AndroidSupportInjectionModule::class, Fragment1Module::class])
interface ApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder
fun build(): ApplicationComponent
}
fun inject(app: MyApplication)
}
I tried different things, but I have the following error :
Fragment1 cannot be provided without an @Provides-annotated method
I tried adding different @Provides method, but I end up with either cycling problem, or missing injection for Fragment2, 3 and 4 or for Fragment1Contract.View
I don't know what I'm missing but any help would be useful at this point !
Update :
I tried adding :
@BindsInstance
fun view(view: Fragment1): Builder
but now I have the following bug :
Fragment1 is bound multiple times
@BindsInstance void dagger.android.AndroidInjector.Builder.seedInstance(T)> @org.jetbrains.annotations.NotNull @BindsInstance androidapp.injection.ApplicationComponent.Builder
injection.ApplicationComponent.Builder.view(Fragment1)