0

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)
Romain Huber
  • 205
  • 4
  • 14

1 Answers1

0

So, you are using this Fragment1 here and your module doesn't know about this Fragment1 so you need to provide this dependency through your Component from your Fragment.

@Provides
internal fun provideFragment1View(fragment: Fragment1): Fragment1Contract.View {
       return fragment
   }

In you component you have to do something like this way:

  @BindsInstance
  fun view(view: Fragment1): Builder
0xAliHn
  • 18,390
  • 23
  • 91
  • 111
  • Thank you for your answer ! I tried adding @ BindsInstance fun view(view: Fragment1): Builder But now I have the following error : Fragment1 is bound multiple times org.jetbrains.annotations.NotNull BindsInstance injection.ApplicationComponent.Builder injection.ApplicationComponent.Builder.view(Fragment1) and BindsInstance void dagger.android.AndroidInjector.Builder.seedInstance(T) – Romain Huber Oct 14 '20 at 13:01
  • I have update my answer to be more clear than a comment – Romain Huber Oct 14 '20 at 13:55
  • I think you can remove `ActivityBindingModule ` completely and then try again – 0xAliHn Oct 14 '20 at 14:03
  • I got the following crash : java.lang.IllegalStateException: OperationFragment must be set – Romain Huber Oct 14 '20 at 14:18
  • I think not related to dagger somethig with your project. – 0xAliHn Oct 14 '20 at 14:19
  • It crashes when we start injecting : DaggerApplicationComponent .builder() .application(this) .build() .inject(this) so it seems it's related to dagger – Romain Huber Oct 14 '20 at 14:34