Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 951
Krzysztof Kubicki Avatar asked Jun 30 '18 09:06

Krzysztof Kubicki


People also ask

Can [dagger / missingbinding] be provided without an @ provides-annotated method?

The project being centered on the use of the framework Dagger to design this architecture, we found the error message: [Dagger / MissingBinding] cannot be provided without an @ Provides-annotated method. After several unsuccessful searches on Internet, we investigated with brute force. Here is what we found.

Why use dagger 2 for dependency injection?

This is our problem — that the view knew about something it shouldn’t, making the involved components tightly coupled. This is one of the good reasons to use Dagger 2 for dependency injection.

Why does Dagger say @inject for coffeemaker constructor?

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: Because you haven't specified anywhere how CoffeeMaker object should be created.

Why can't I send specific data to my Android presenter?

Your presenter (or ViewModel) depends on a database which requires (Application) context in it’s constructor. Since you shouldn’t send android specific stuff to presenter, your view, the activity will end up instantiating the database.


1 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})
like image 138
KursoR Avatar answered Oct 10 '22 13:10

KursoR