hanchen ke
hanchen ke

Reputation: 515

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.

Upvotes: 46

Views: 111173

Answers (7)

Timur
Timur

Reputation: 770

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)

Upvotes: 2

Ilya Gazman
Ilya Gazman

Reputation: 32281

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.

Upvotes: 0

Ruslan Zhuravlov
Ruslan Zhuravlov

Reputation: 226

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

Upvotes: 7

htafoya
htafoya

Reputation: 19293

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

Upvotes: -1

ItSNeverLate
ItSNeverLate

Reputation: 769

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

Upvotes: 3

CoolMind
CoolMind

Reputation: 28875

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 {

Upvotes: 5

azizbekian
azizbekian

Reputation: 62209

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

}

Upvotes: 37

Related Questions