Dagger 2: Cannot be provided without an @Provides-annotated method

Issue

I just started learning dagger2 and faced a strange issue that looks like a bug to me. Here’s the module:

@Module
public class SimpleModule {

    @Provides
    Cooker providerCooker() {

        return new Cooker("tom", "natie");
    }
}

Component:

@Component(modules = SimpleModule.class)
public interface SimpleComponent {

    void inject(DaggerTestActivity activity);

}

Interface:

public interface CoffeeMaker {

    String makeCoffee();
}

Implementation:

  public class SimpleMaker implements CoffeeMaker {
    
        Cooker mCooker;
      
        @Inject
        public SimpleMaker(Cooker cooker) {
    
            this.mCooker = cooker;
    
        }
    
        @Override
        public String makeCoffee() {
    
            return mCooker.makeCoffee();
        }
    }

Cooker :

public class Cooker {

    String name; 
    String coffeeKind;


    public Cooker(String name, String coffeeKind) {
        this.name = name;
        this.coffeeKind = coffeeKind;
    }
    
   public  String  makeCoffee() {
  
        return name + "make" + coffeeKind; 
    }

}

Coffee machine:

public class CoffeeMachine {

    CoffeeMaker mMaker;

    @Inject
    public CoffeeMachine(CoffeeMaker coffeeMaker) {

        this.mMaker = coffeeMaker;
    }

    public String makeCoffee() {

        return mMaker.makeCoffee();
    }
}

Just it. I use it in the activity. Faced strange issue here:

    @Inject
    CoffeeMachine mCoffeeMachine;

The error I’m getting from the Dagger 2 compiler is the following:

Error:(14, 10) com.wyyc.daggertest.CoffeeMaker cannot be provided without an @Provides-annotated method.
com.wyyc.zqqworkproject.DaggerTestActivity.mCoffeeMachine
[injected field of type: com.wyyc.daggertest.CoffeeMachine mCoffeeMachine]
com.wyyc.daggertest.CoffeeMachine.<init>(com.wyyc.daggertest.CoffeeMaker coffeeMaker)

All this situation looks very strange, and I’d like to hear some input from more experienced Dagger 2 users.

Solution

Your CoffeeMachine needs CoffeeMaker. And you have declared that Dagger will take care of providing that dependency to the CoffeeMachine by annotating the constructor with @Inject. But Dagger says:

CoffeeMaker cannot be provided without an @Provides-annotated method

Because you haven’t specified anywhere how CoffeeMaker object should be created. @Injecting SimpleMaker is not enough, because SimpleMaker != CoffeeMaker. So, you have to specify explicitly, that when Dagger wants CoffeeMaker then provide him SimpleMaker.

Change your module to this:

@Module
public class SimpleModule {

    @Provides
    Cooker providerCooker() {
        return new Cooker("tom", "natie");
    }

    @Provides
    CoffeeMaker provideCoffeeMaker(Cooker cooker) {
        return new SimpleMaker(cooker);
    }

}

Answered By – azizbekian

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published