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.
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.
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.
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.
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.
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})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With