I'm following this article in order to make Dagger 2 to directly create a module for me. However I keep getting this error:
Error:(10, 1) error: @Component.Builder is missing setters for required modules or components: [stalker.commons.di.app.AppModule]
I've search around but, although there are similar questions, none of the answers seemed to apply for me. Below is my implementation:
AppComponent.kt
@Singleton
@Component(
modules = [
(AppModule::class),
(ActivityBinding::class),
(AndroidInjectionModule::class)
]
)
interface AppComponent : AndroidInjector<App> {
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder
fun build(): AppComponent
}
}
AppModule.kt
@Module(
subcomponents = [
(LoginSubComponent::class),
(RegisterSubComponent::class),
(ForgotPasswordSubComponent::class),
(HomepageSubComponent::class)
]
)
class AppModule (private val application: Application){
@Provides
@Singleton
fun providesDefaultFieldValidator(): DefaultFieldValidator = DefaultFieldValidator()
}
App.kt
class App : DaggerApplication() {
override fun onCreate() {
super.onCreate()
(applicationInjector() as AppComponent).inject(this)
plant(Timber.DebugTree())
}
override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
return DaggerAppComponent.builder()
.application(this)
.build()
}
}
Any idea what I've might be doing wrong?
Dagger can't create your module because it does not have an empty default constructor. Instead you declare it as
class AppModule (private val application: Application)
, so you need to manually create the module and add it to the component—as hinted by the error.While this binds the application, you do not add the module—and as mentioned Dagger can't create it because you declared constructor arguments.
It seems like you're not using the constructor argument, so just delete it and it will work.
I guess it is because of the fact that you have your custom constructor that takes
Application
as parametertry adding
to your
and initialize it when you create the component in your Application subclass