Same injects both in subcomponent and parent component with same dependencies type.
See original GitHub issueSuppose 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:
- Created 7 years ago
- Reactions:1
- Comments:8
Top 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 >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
This is now correctly reported as an error.
At the moment I’m not aware of any updates; fixing this required a large change to how dependencies are resolved.