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

7

44

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.

Abbess answered 30/3, 2017 at 11:38 Comment(0)
S
37

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

}
Sheryl answered 30/3, 2017 at 12:0 Comment(5)
thanks, I want to know why this,I hope you can give me some idea 。thank youAbbess
grateful for your answer patiently.Do you have a good materials for learning dagger2 ? Can you give me under reference ?Abbess
android.jlelse.eu/practical-guide-to-dagger-76398948a2ea So far the best explanation i have come across for Dagger 2Assume
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 classSebbie
@Sheryl If you have time, maybe you can help me with this question. ThanksBoycott
J
5

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')
}
Jeweller answered 22/5, 2023 at 9:38 Comment(0)
N
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 {
Naumachia answered 30/5, 2022 at 15:1 Comment(0)
J
2

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
Jilt answered 7/9, 2022 at 23:5 Comment(0)
C
1

In Hilt my mistake was wrong @InstallIn component. I just change the component to appropriate one. I changed from:

@InstallIn(ActivityComponent::class)

To:

@InstallIn(SingletonComponent::class)

Cockburn answered 15/2, 2024 at 16:59 Comment(0)
I
0

Had another case. I used lazy and didn't provide the import, so it took Kotlin Lazy instead of dagger.Lazy.

Once I added the dagger import the problem was solved.

Ikey answered 30/10, 2023 at 15:38 Comment(0)
L
-1

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

Lorettalorette answered 14/11, 2022 at 4:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.