Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger2 component with more than one dependencies

This is what I currently have and it works:

@FragmentScope
@Component(dependencies = {FacebookComponent.class}, 
           modules = {FragmentFacebookLoginModule.class})
public interface FragmentFacebookLoginComponent {

    void inject(FragmentFacebookLogin fragment);
}

Now I want to add another dependency. I changed it to this:

@Component(dependencies = {FacebookComponent.class, AnotherComponent.class}, 
           modules = {FragmentFacebookLoginModule.class})

But now I get this error message:

FragmentFacebookLoginComponent depends on more than one scoped component

How can I solve this? How can I have more than one dependencies?

If I remove the scope from one component I get this error message:

AnotherComponent (unscoped) cannot depend on scoped components

like image 833
Ralph Bergmann Avatar asked May 23 '15 23:05

Ralph Bergmann


People also ask

What are the different types of dependency traits in dagger?

There are two options. Component dependencies. This option migrated from Dagger 1. Let’s denote Component dependencies traits right away: Two dependent components cannot have the same Scope. Parent component must explicitly declare objects which can be used in child components. Component can depend on other components.

What are the differences between component dependencies and subcomponents in dagger?

This one was introduced in Dagger 2. Parent component is obliged to declare Subcomponents getters inside its interface. Subcomponent has access to all parents objects. Subcomponent can only have one parent. Surely Subcomponents have differences from Component Dependencies. Let’s look into scheme and code to get this clear.

What are the parts of the Dagger 2 article cycle?

Dagger 2. Part II. Custom scopes, Component dependencies, Subcomponents Dagger 2 articles cycle: Dagger 2. Part I. Basic principles, graph dependencies, scopes. Dagger 2. Part II. Custom scopes, Component dependencies, Subcomponents. Dagger 2. Part three.

What are the limitations of Dagger 2's dependency injection framework?

While there are other Java dependency injection frameworks, many of them suffered limitations in relying on XML, required validating dependency issues at run-time, or incurred performance penalties during startup. Dagger 2 relies purely on using Java annotation processors and compile-time checks to analyze and verify dependencies.


1 Answers

Now Dagger supports a component that can depends on more than 1 scoped dependencies. Just update your dagger version to 2.27

https://github.com/google/dagger/issues/1414

api 'com.google.dagger:dagger:2.27'
kapt 'com.google.dagger:dagger-compiler:2.27'
like image 54
stephen1706 Avatar answered Sep 21 '22 13:09

stephen1706