Skip to content Skip to sidebar Skip to footer

Dagger2 Error At Inject In A Class Who Injects

I'm trying to inject in my presenter an interactor but gives me an error, at seems I can't inject in a class who injects to another: error: [Dagger/DuplicateBindings] com.example.c

Solution 1:

You should separate one-other with the @Named("someName") annotation, or you could just do what @Derek says. My approach:

@Provides@Named("someName1")
    publicOperacionprovideSuma() {
        returnnewInteractorSuma();
    }

    @Provides@Named("someName2")
    publicOperacionprovideResta() {
        returnnewInteractorResta();
    }

    @Provides@Named("someName3")
    publicOperacionprovideDiv() {
        returnnewInteractorDivision();
    }

    @Provides@Named("someName4")
    publicOperacionprovideMult() {
        returnnewInteractorMultiplicacion();
    }

Otherwise dagger doesn't know which one to return where.

Call the @Named when injecting also.

Solution 2:

For this kind of situation is where Dagger2 Qualifiers are build for.

1.- Create your qualifiers:

@Qualifier
public @interface OperacionSuma {}
@Qualifier
public @interface OperacionResta {}
@Qualifier
public @interface OperacionDiv {}
@Qualifier
public @interface OperacionMult {}

2.- Set qualifiers in your providers methods:

@ModulepublicclassInteractorModule {
    @Provides@OperacionSumapublicOperacionprovideSuma() {
        returnnewInteractorSuma();
    }

    @Provides@OperacionRestapublicOperacionprovideResta() {
        returnnewInteractorResta();
    }

    @Provides@OperacionDivpublicOperacionprovideDiv() {
        returnnewInteractorDivision();
    }

    @Provides@OperacionMultpublicOperacionprovideMult() {
        returnnewInteractorMultiplicacion();
    }
} 

3.- Specify what kind of "operation" do you want to inject in your presenter:

classPresenter {

    @Inject
    Presenter(@OperacionSuma Operacion operacion) { }

    @Inject
    Presenter(@OperacionResta Operacion operacion) { }

    @Inject
    Presenter(@OperacionDiv Operacion operacion) { }

    @Inject
    Presenter(@OperacionMult Operacion operacion) { }
}

Solution 3:

Since dagger looks for the return type, not a name that given to the function you should take care of it. However, Dagger2 provides a solution to such problems. Using @Named annotation.

Sometimes the type alone is insufficient to identify a dependency. For example, if you need a Refrofit instance with GsonConverterFactory and another one ScalarConverterFactory you will end up with 2 provide methods that have the same return type: Retrofit. In this case, you can use @Named annotation to differentiate two Retrofit instances

enter image description here

Now you can use it like following

enter image description here

Coming to your case

@ModulepublicclassInteractorModule {
    @Provides@Named("InteractorSuma")
    publicOperacionprovideSuma() {
        returnnewInteractorSuma();
    }

    @Provides@Named("InteractorResta")
    publicOperacionprovideResta() {
        returnnewInteractorResta();
    }

    @Provides@Named("InteractorDivision")
    publicOperacionprovideDiv() {
        returnnewInteractorDivision();
    }

    @Provides@Named("InteractorMultiplicacion")
    publicOperacionprovideMult() {
        returnnewInteractorMultiplicacion();
    }
}

Here is the full example of how to use @Named annotation

Let me know if you have still a problem

Solution 4:

Return the instance of Child class, not parent class.

@ModulepublicclassInteractorModule {
    @ProvidespublicInteractorSumaprovideSuma() {
        returnnewInteractorSuma();
    }

    @ProvidespublicInteractorRestaprovideResta() {
        returnnewInteractorResta();
    }

    @ProvidespublicInteractorDivisionprovideDiv() {
        returnnewInteractorDivision();
    }

    @ProvidespublicInteractorMultiplicacionprovideMult() {
        returnnewInteractorMultiplicacion();
    }
} 

Post a Comment for "Dagger2 Error At Inject In A Class Who Injects"