Home » Android » java – Dagger2 issue with "cannot be provided without an @Provides-annotated method."

java – Dagger2 issue with "cannot be provided without an @Provides-annotated method."

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to setup a new project with Dagger2, I’ve used Dagger2 before, but now I’m trying to set it up from scratch by myself. I’m getting the example from a Kotlin project that I’m part of, but can’t set it up for Java the same way as it works in Kotlin right now (or maybe I’m missing something).

It’s just a single Component, single Module and Application.

Component

@Singleton
@Component(modules = {MainAppModule.class})
public interface AppComponent extends AndroidInjector<App> {
@Component.Builder
abstract class Builder implements AndroidInjector.Factory<App> {

    public AppComponent create(App application) {
        seedApplication(application);
        return build();
    }

    @BindsInstance
    abstract void seedApplication(App application);

    abstract AppComponent build();
}
}

Module

@Module
abstract class MainAppModule {

@Binds
abstract public Application bindApplication(App application);

@ContributesAndroidInjector
abstract public MainActivity contributeActivityInjector();
}

*Application *

public class App extends DaggerApplication {

@Override
public AndroidInjector<? extends DaggerApplication> applicationInjector() {
    return DaggerAppComponent.builder().create(this);
}
}

At this point I don’t have any classes that I call with @Inject
I’m just getting error at build time:

 error: [dagger.android.AndroidInjector.inject(T)] java.util.Map<java.lang.Class<? extends android.content.BroadcastReceiver>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.content.BroadcastReceiver>>> cannot be provided without an @Provides-annotated method.
public interface AppComponent extends AndroidInjector<App> {
        ^ 

Of course cannot be provided without an @Provides-annotated method. seems to be the problem, but I just don’t know how to solve it. It works fine on my kotlin project, that somebody else set up.

How to&Answers:

It looks like you are missing AndroidInjectionModule (or AndroidSupportInjectionModule if you use support fragments) installed on you AppComponent.

It should be like:

@Component(modules = {AndroidInjectionModule.class, MainAppModule.class})