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

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);
    }

}

Leave a Comment