10

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.

  • For the sake of completeness, can you please show us your dagger-related lines in your gradle file (or other build file)? – Jeff Bowman Jul 01 '18 at 19:27

1 Answers1

28

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})
KursoR
  • 1,595
  • 13
  • 17
  • Hi @KursoR, Unfortunately this gives the same error. – Krzysztof Kubicki Jun 30 '18 at 10:07
  • I think it has something to do with the `@ContributesAndroidInjector` ;/ – Krzysztof Kubicki Jun 30 '18 at 10:08
  • 2
    Maybe now this is another error? `Map, AndroidInjector.Factory extends BroadcastReceiver>>` is provided by AndroidInjectionModule ([source](https://github.com/google/dagger/blob/master/java/dagger/android/AndroidInjectionModule.java)), so it should be resolved – KursoR Jun 30 '18 at 10:21
  • You are so right, thank you so much. I would very much appreciate some explanation of these additional modules and why do I have to add them (I don't have them in Kotlin project). Weird stuff. Anyway. Thanks so much. – Krzysztof Kubicki Jul 01 '18 at 19:33