40

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.

Rahul
  • 3,293
  • 2
  • 31
  • 43
hanchen ke
  • 455
  • 1
  • 4
  • 7

5 Answers5

33

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

}
azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • thanks, I want to know why this,I hope you can give me some idea 。thank you – hanchen ke Mar 30 '17 at 12:09
  • grateful for your answer patiently.Do you have a good materials for learning dagger2 ? Can you give me under reference ? – hanchen ke Mar 31 '17 at 02:34
  • https://android.jlelse.eu/practical-guide-to-dagger-76398948a2ea So far the best explanation i have come across for Dagger 2 – Ajith M A Mar 12 '19 at 11:37
  • i had similar problem, but i had a different return type of my provider method in my module and expecting a context back in component class – Antroid Apr 28 '19 at 04:26
  • @azizbekian If you have time, maybe you can help me with [this question](https://stackoverflow.com/questions/58747371/how-to-inject-the-creation-of-a-broadcastreceiver-object-in-fragment-using-dagge). Thanks – Jorn Nov 07 '19 at 11:42
4

In my case I missed one module in @Component (in multi-module app). Thanks to @azizbekian.

@Component(
    modules = [
        ApiModule::class,
        SettingsModule::class,
        CoroutinesModule::class // <- I forgot one of these modules
    ],
    dependencies = [Api::class, NetworkProvider::class]
)
interface YourComponent {
CoolMind
  • 26,736
  • 15
  • 188
  • 224
1

Have a closer look at imports

Replace

import com.google.android.datatransport.runtime.dagger.Provides
import com.google.android.datatransport.runtime.dagger.Module

With

import dagger.Module
import dagger.Provides
ItSNeverLate
  • 533
  • 7
  • 8
1

In a multi-module project this error may occur when a new module is added. To fix this you need to add new module to build.gradle(:app) dependencies:

dependencies {
    implementation project(':new_module')
}
-1

If you have this problem when everything seems correct it may be because you did some refactoring, clean the project and compile again.

htafoya
  • 18,261
  • 11
  • 80
  • 104