question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multibinding doesn't work with Component Dependencies

See original GitHub issue

Dagger docs states:

A binding in a subcomponent can depend on a multibound set or map from its parent, just as it can depend on any other binding from its parent. But a subcomponent can add elements to multibound sets or maps that are bound in its parent as well, by simply including the appropriate @Provides methods in its modules.

However, the same is not true when using component dependencies. If one has many small components that shares dependencies using “component dependencies” - a “child” component that has a dependency to another component might not be able to contribute to its Graph.

For example, consider the following classes:

@Component
interface CommonComponent {
    fun factoryBindings(): Map<String, Factory>
}

@Component(
    modules = [FeatureModule::class]
    dependencies = [CommonComponent::class]
)
interface FeatureComponent

@Module
interface FeatureModule {
    // Contributes to factories map.
    @[Binds IntoMap FactoryKey("featureFactory")]
    fun featureFactory(impl: FeatureFactory): CustomFactory
}

class CompositeCustomFactory @Inject constructor(
   val factoriesMap: Map<String, Factory>
)

In this if I use FeatureComponent to inject the CompositeCustomFactory, I would only receive the ones contributed inside CommonComponent - ignoring the ones inside FeatureComponent (e.g., FeatureFactory).

Note that using a subcomponent setup, it’d work as expected.

Is this a design decision and/or limitation of Dagger, a bug when dealing with component dependencies or am I doing something wrong?

Obs: #1186 is related but the use cases are slightly different so I opened this PR.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
bcorsocommented, Aug 7, 2020

Hi Marcello, no need to provide an example. I think @Chang-Eric is right that the reason you don’t get a conflict is because the FactoryComponent is installing into the map with type Map<String, CustomFactory>.

However, I think the suggestion in https://github.com/google/dagger/issues/2027#issuecomment-670589644 still applies, just make sure your map types are the same.

0reactions
marcellogalhardocommented, Aug 7, 2020

Hey @bcorso,

The workaround from https://github.com/google/dagger/issues/1112#issuecomment-474985867 is working as expected and it is enough for my use case and therefore, I will close this issue.

Thanks a lot for yours and @Chang-Eric help! ✌️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expose Multibinding through a component dependency
Is there a way to expose some sort of map in the BaseComponent to add AndroidInjectors to the ApplicationComponent ? (I'll try to...
Read more >
Dagger by Tutorials, Chapter 14: Multibinding With Maps
In this chapter, you'll learn how to use multibinding with Map. ... and WeatherEndpoint and notice there's a problem — they don't share...
Read more >
Hilt in multi-module apps - Android Developers
You must use component dependencies to solve this problem with feature modules. Follow these steps: Declare an @EntryPoint interface in the ...
Read more >
Testing with Dagger
One of the benefits of using dependency injection frameworks like Dagger is that it makes testing your code ... But there are problems...
Read more >
Dagger 2 Multibindings Illustrated (Kotlin) | by Elye - Medium
Dagger 2 Multibindings is simply a feature that packages dependencies from ... In the event that the parent component doesn't have any elements...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found