Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 311
hanchen ke Avatar asked Mar 30 '17 11:03

hanchen ke


1 Answers

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);     }  } 
like image 77
azizbekian Avatar answered Oct 02 '22 03:10

azizbekian