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.

Same injects both in subcomponent and parent component with same dependencies type.

See original GitHub issue

Suppose we have next Dagger2 configuration:

@Component(modules = ModuleA.class)
public interface ComponentA {
    void inject(MainActivity activity);
    ComponentB plus();
}

@Module
public class ModuleA {
    @Provides
    public String string() {
        return "A";
    }
}


@Subcomponent(modules = ModuleB.class)
public interface ComponentB {
    void inject(MainActivity activity);
}

@Module
public class ModuleB {
    @Provides
    public String string() {
        return "B";
    }
}


public class MainActivity extends AppCompatActivity {

    @Inject
    String string;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DaggerComponentA.create().plus().inject(this);

        System.out.println(string); //A
    }
}

Wouldn’t it be better if Dagger2 threw an exception instead of injection “A”? If we remove inject from ComponentA the code will not compile, because “String is bound multiple times”. Is it a bug or a feature?

P.S. Full code available here

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

1reaction
ronshapirocommented, Mar 25, 2019

This is now correctly reported as an error.

0reactions
naturalwarrencommented, Dec 13, 2017

At the moment I’m not aware of any updates; fixing this required a large change to how dependencies are resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dagger 2 sub-component injection error - Stack Overflow
When a type is used as a component dependency, each provision method on the dependency is bound as a provider. Note that only...
Read more >
Hierarchical injectors - Angular
Hierarchical dependency injection enables you to share dependencies between different parts of the application only when and if you need to. Types of...
Read more >
Dagger 2. Part II. Custom scopes, Component dependencies ...
Here we're gonna talk custom scopes, components linking via component dependencies and subcomponents. And will touch upon such important ...
Read more >
Subcomponents - Dagger
Dagger is a fully static, compile-time dependency injection framework for both Java and Android. It is developed by the Java Core Libraries Team...
Read more >
Testing Components with children - Testing Angular
How to write unit and integration tests for Components with children. ... The same is done for the other children, app-service-counter and ...
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