Dagger 2: @Component.Builder is missing setters for required modules or components: [appi.example.com.dagger.AppModule]`

£可爱£侵袭症+ 提交于 2019-11-28 16:26:25

问题


I'm configuring the new Dagger Android module but I got this error Here's my Component:

@AppScope
@Component(modules = {AppModule.class, NetModule.class})
public interface AppComponent {

  @Component.Builder
  interface Builder {
    @BindsInstance
    Builder application(ExampleApplication application);

    @BindsInstance
    Builder appModule(AppModule appModule);

    @BindsInstance
    Builder netModule(NetModule netModule);

    AppComponent build();
  }

  void inject(ExampleApplication __); 
...

Which I build like this in my Application

appComponent = DaggerAppComponent
      .builder()
      .application(this)
      .appModule(new AppModule(this))
      .netModule(new NetModule())
      .build()
      .inject(this);

But I still receive the error

Error:(20, 3) error: @Component.Builder is missing setters for required modules or components: [app.example.com.dagger.AppModule]

According to the documentation that should be right, What am I missing?

For example, this could be a valid Component with a Builder:

@Component(modules = {BackendModule.class, FrontendModule.class})
interface MyComponent {
  MyWidget myWidget();

  @Component.Builder
  interface Builder {
    MyComponent build();
    Builder backendModule(BackendModule bm);
    Builder frontendModule(FrontendModule fm);
  }
}

回答1:


Remove the below code from the AppModule.class and rebuild the project

    @Provides
    @Singleton
    Application provideContext(SomeApplication application) {
        return application;
    }



回答2:


I think this provides a somewhat clearer explanation on the use of @BindsInstance and removal of @Provides Application, Dagger 2 Component Builder:

@BindsInstance What?

Here’s the definition :

Marks a method on a component builder or subcomponent builder that allows an instance to be bound to some type within the component. — source

WHAAT? I don’t understand it either 😛

Here’s a simple hint of when to use it :

@BindsInstance methods should be preferred to writing a @Module with constructor arguments and immediately providing those values. — source

I come from Spring Boot and Dagger 2 is OMG so much more complicated. :(

So based on my extremely limited experience with Dagger 2, this happens because there a *Module with a constructor argument which is improperly configured. I still don't know how to properly configure the Module with a constructor argument, but I rather follow recommended approach given by Dagger 2 documentation, and that is to remove the constructor argument(s) and use @BindsInstance and @Inject instead.

e.g.

@Module
class NetModule { // no constructor argument here!

    @Inject @Named("mqttServer") // replaced by @Inject
    internal lateinit var mqttServer: String

}

and in AppComponent :

@Singleton
@Component(modules = [AndroidSupportInjectionModule::class, AppModule::class, NetModule::class, ActivityBuilder::class])
interface AppComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: Application): Builder

        @BindsInstance // you'll call this when setting up Dagger
        fun mqttServer(@Named("mqttServer") mqttServer: String): Builder

        fun build(): AppComponent
    }

    fun inject(app: GeoAssistantApp)
}

Then you provide the dependencies of the modules when constructing the DaggerAppComponent from the Application subclass (make sure you specify the subclass name in AndroidManifest.xml):

class GeoAssistantApp : Application(), HasActivityInjector, HasSupportFragmentInjector {

    @Inject
    internal lateinit var activityDispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
    @Inject
    internal lateinit var fragmentDispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>

    override fun onCreate() {
        super.onCreate()
        Log.i(GeoAssistantApp::class.java.simpleName, "Initializing DaggerAppComponent...")
        DaggerAppComponent.builder()
                // list of modules/dependencies of modules that are part of this component need to be created here too
                .application(this)
                .mqttServer(getString(R.string.mqtt_server))
                .build()
                .inject(this)
    }

    override fun activityInjector(): AndroidInjector<Activity> {
        return activityDispatchingAndroidInjector
    }

    override fun supportFragmentInjector(): AndroidInjector<Fragment> {
        return fragmentDispatchingAndroidInjector
    }
}

Note that the support-v4 Fragment vs native Fragment usage can be a source of problems. e.g. for support-v4, you need to use AndroidSupportInjectionModule, HasSupportFragmentInjector, while with native, you need to use AndroidInjectionModule, HasFragmentInjector.




回答3:


In my case I was using an object Module, so I had to annotate with @JvmStatic



来源:https://stackoverflow.com/questions/44083243/dagger-2-component-builder-is-missing-setters-for-required-modules-or-componen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!