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.

Issue with subcomponent repeated modules: "Is bound multiple times"

See original GitHub issue

I’m developing an android app and I’m facing an issue when using subcomponents.

  1. There’s a Fragment A, which has its own Component, which installs Module A and injects a class ClassFoo.
  2. There’s nested fragment (Fragment B), which has a Subcomponent, which also installs Module A and injects the same class as Fragment A: ClassFoo.
  3. 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:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
douglasiacovellicommented, Aug 19, 2020

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 😃

1reaction
Chang-Ericcommented, Aug 17, 2020

I’m assuming ModuleA has a binding for ClassFoo? 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 a ClassFoo 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.

Read more comments on GitHub >

github_iconTop 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 >

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