Issue with subcomponent repeated modules: "Is bound multiple times"
See original GitHub issueI’m developing an android app and I’m facing an issue when using subcomponents.
- There’s a Fragment A, which has its own Component, which installs Module A and injects a class
ClassFoo
. - There’s nested fragment (Fragment B), which has a Subcomponent, which also installs Module A and injects the same class as Fragment A:
ClassFoo
. - The Fragment A instantiates Fragment B inside of it and one of its modules installs the Subcomponent of Fragment B.
When in the nested fragment (Fragment B) I want to inject the ClassFoo
I’m having issues. The repeated modules seem to be instantiated as new ones and Dagger complains saying:
[Dagger/DuplicateBindings] com.mypackage.ClassFoo is bound multiple times
My Subcomponent builder method:
@Subcomponent.Builder
interface Builder {
fun build(): AppBannerDialogComponent
}
The moduleA has a constructor with no args.
Ps: I know if I remove ModuleA
from my subcomponent modules list that it will work, but I’d like to be explicit so I can use in other contexts that may or may not have ModuleA
installed. I also know that if I remove ClassFoo
from FragmentA that it works.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Duplicate binding reported with the same type from a ... - GitHub
Consider the following Dagger graph: public class SampleDaggerGraph { @Singleton @Component(modules = DatabaseModule.class) interface ...
Read more >error: [Dagger/DuplicateBindings] com.example.StartRouter is ...
1 Answer 1 ... Your setup is fine but for the fact that you also add both modules (both binding StartRouter ) to...
Read more >Subcomponents - Dagger
An object bound in a subcomponent can depend on any object that is bound in its parent component or any ancestor component, in...
Read more >Using Dagger in multi-module apps - Android Developers
Dagger has a mechanism called component dependencies that you can use to solve this issue. Instead of the child component being a subcomponent...
Read more >Dagger 2: Android Modules - ProAndroidDev
In this article I'd like to go into detail about how to use Dagger 2 with Android, the current version of Dagger at...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Oh wow! I’m sorry I didn’t mention that earlier. My thought was that it wouldn’t make such a difference.
So I believe the first suggestion you gave still applies. I’ll take that into consideration.
Thank you for helping me figuring that out, @Chang-Eric! I’m closing the issue for now 😃
I’m assuming
ModuleA
has a binding forClassFoo
? If so, the issue here is that bindings in subcomponents are inherited from the parent. Even though both bindings come from the same module, Dagger treats them differently because some things are based on the context of the component they binding is installed in like which scope annotations are allowed. You’ll want to just install modules at the widest scope they are needed.From your PS, I think the way to think about it is figuring out if FragmentB’s API is that it comes with a definition for
ClassFoo
or if it requires aClassFoo
from its user. It kind of needs to be one or the other. If you want to set up a default that can be overridden, you would have to do that with some extra machinery with optionals.