Friday, 5 November 2021

dagger error binding with matching key exists in component

Dagger 2

/di/AppComponent.java:19: error: [Dagger/MissingBinding] ProductListFragment cannot be provided without an @Inject constructor or an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.
public abstract interface AppComponent {
                ^
  A binding with matching key exists in component: MainFragmentProvider_BindProductListFragment.ProductListFragmentSubcomponent
      ProductListFragment is injected at
          productlist.ProductListModule.provideChildFragmentManager(productListFragment)
      androidx.fragment.app.FragmentManager is injected at
          productlist.adapter.NewProductListPagerAdapter(…, fragmentManager)
      productlist.adapter.NewProductListPagerAdapter is injected at
          productlist.ProductListV2Fragment.mPagerAdapter
      productlist.ProductListV2Fragment is injected at
          dagger.android.AndroidInjector.inject(T) [di.AppComponent → di.BuilderModule_BindMainActivity.MainActivitySubcomponent →  MainFragmentProvider_BindProductListV2Fragment.ProductListV2FragmentSubcomponent]

I have the following module with these 2 fragments here:

@Module
abstract class FragmentProvider {
    @PerFragment
    @ContributesAndroidInjector(modules = [ProductListModule::class])
    abstract fun bindProductListV2Fragment(): ProductListV2Fragment
    
    @PerFragment
    @ContributesAndroidInjector(modules = [ProductListModule::class])
    abstract fun bindProductListFragment(): ProductListFragment
}

In my ProductListModule I have the following:

@Module
class ProductListModule {

    @Provides
    fun provideChildFragmentManager(productListFragment: ProductListFragment) =
        productListFragment.childFragmentManager
}

The fragmentManager will be injected into the following class:

class NewProductListPagerAdapter @Inject constructor(
    @ActivityContext private val context: Context,
    fragmentManager: FragmentManager
) { ..... }

And both fragments will inject this NewProductListPagerAdapter in them.

class ProductListV2Fragment  : BaseProductListFragment() {

    @Inject
    lateinit var mPagerAdapter: NewProductListPagerAdapter
}

class ProductListFragment  : BaseProductListFragment() {

    @Inject
    lateinit var mPagerAdapter: NewProductListPagerAdapter
}

My AppComponent:

@Singleton
@Component(
    modules = [
        AndroidSupportInjectionModule::class,
        AppModule::class,
        ProductModule::class,
        BaseAppModule::class
    ]
)
interface AppComponent {
    @Component.Factory
    interface Factory {
        fun create(
            @BindsInstance application: Application,
        ): AppComponent
    }

    fun inject(tsApplication: TsApplication)
}


from dagger error binding with matching key exists in component

No comments:

Post a Comment