Dagger 2:@Component.Builder 缺少所需模块或组件的设置器:[appi.example.com.dagger.AppModule]`
Dagger 2: @Component.Builder is missing setters for required modules or components: [appi.example.com.dagger.AppModule]`
我正在配置新的 Dagger Android 模块,但出现此错误
这是我的组件:
@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 __);
...
我在我的应用程序中这样构建的
appComponent = DaggerAppComponent
.builder()
.application(this)
.appModule(new AppModule(this))
.netModule(new NetModule())
.build()
.inject(this);
但我仍然收到错误
错误:(20, 3) 错误:@Component.Builder 缺少所需模块或组件的设置器:[app.example.com.dagger.AppModule]
根据文档应该是对的,我错过了什么?
例如,这可能是具有生成器的有效组件:
@Component(modules = {BackendModule.class, FrontendModule.class})
interface MyComponent {
MyWidget myWidget();
@Component.Builder
interface Builder {
MyComponent build();
Builder backendModule(BackendModule bm);
Builder frontendModule(FrontendModule fm);
}
}
从 AppModule.class 中删除以下代码并重建项目
@Provides
@Singleton
Application provideContext(SomeApplication application) {
return application;
}
我认为这对使用 @BindsInstance
和删除 @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
什么?我也不明白
以下是何时使用它的简单提示:
@BindsInstance methods should be preferred to writing a @Module with
constructor arguments and immediately providing those values. — source
我来自 Spring Boot and Dagger 2 真是天哪,复杂多了。 :(
因此,根据我对 Dagger 2 极其有限的经验,发生这种情况是因为 *Module
的构造函数参数配置不正确。我仍然不知道如何使用构造函数参数正确配置模块,但我宁愿遵循 Dagger 2 文档给出的推荐方法,也就是删除构造函数参数并使用 @BindsInstance
和 @Inject
代替。
例如
@Module
class NetModule { // no constructor argument here!
@Inject @Named("mqttServer") // replaced by @Inject
internal lateinit var mqttServer: String
}
并在 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)
}
然后在从 Application
子类构造 DaggerAppComponent
时提供模块的依赖项(确保 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
}
}
请注意,support-v4
Fragment
与本机 Fragment
用法可能是问题的根源。例如对于 support-v4,您需要使用 AndroidSupportInjectionModule
、HasSupportFragmentInjector
,而对于 native,您需要使用 AndroidInjectionModule
、HasFragmentInjector
.
在我的例子中,我使用的是一个对象模块,所以我不得不用@JvmStatic
注释提供者方法
@Module
object GsonModule {
@JvmStatic
@Singleton
@Provides
fun provideGson() = Gson()
}
Kotlin 答案
检查 Module 的构造函数。
如果您有构造函数和应用程序参数,请将其删除。
真实用法:
@Module
class MyModule {
// This is example module for true usage.
}
••• 错误用法:
@Module
class MyModule constructor(application: Application) {
// This is example module for wrong usage.
}
我正在配置新的 Dagger Android 模块,但出现此错误 这是我的组件:
@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 __);
...
我在我的应用程序中这样构建的
appComponent = DaggerAppComponent
.builder()
.application(this)
.appModule(new AppModule(this))
.netModule(new NetModule())
.build()
.inject(this);
但我仍然收到错误
错误:(20, 3) 错误:@Component.Builder 缺少所需模块或组件的设置器:[app.example.com.dagger.AppModule]
根据文档应该是对的,我错过了什么?
例如,这可能是具有生成器的有效组件:
@Component(modules = {BackendModule.class, FrontendModule.class})
interface MyComponent {
MyWidget myWidget();
@Component.Builder
interface Builder {
MyComponent build();
Builder backendModule(BackendModule bm);
Builder frontendModule(FrontendModule fm);
}
}
从 AppModule.class 中删除以下代码并重建项目
@Provides
@Singleton
Application provideContext(SomeApplication application) {
return application;
}
我认为这对使用 @BindsInstance
和删除 @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
什么?我也不明白
以下是何时使用它的简单提示:
@BindsInstance methods should be preferred to writing a @Module with constructor arguments and immediately providing those values. — source
我来自 Spring Boot and Dagger 2 真是天哪,复杂多了。 :(
因此,根据我对 Dagger 2 极其有限的经验,发生这种情况是因为 *Module
的构造函数参数配置不正确。我仍然不知道如何使用构造函数参数正确配置模块,但我宁愿遵循 Dagger 2 文档给出的推荐方法,也就是删除构造函数参数并使用 @BindsInstance
和 @Inject
代替。
例如
@Module
class NetModule { // no constructor argument here!
@Inject @Named("mqttServer") // replaced by @Inject
internal lateinit var mqttServer: String
}
并在 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)
}
然后在从 Application
子类构造 DaggerAppComponent
时提供模块的依赖项(确保 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
}
}
请注意,support-v4
Fragment
与本机 Fragment
用法可能是问题的根源。例如对于 support-v4,您需要使用 AndroidSupportInjectionModule
、HasSupportFragmentInjector
,而对于 native,您需要使用 AndroidInjectionModule
、HasFragmentInjector
.
在我的例子中,我使用的是一个对象模块,所以我不得不用@JvmStatic
注释提供者方法@Module
object GsonModule {
@JvmStatic
@Singleton
@Provides
fun provideGson() = Gson()
}
Kotlin 答案
检查 Module 的构造函数。
如果您有构造函数和应用程序参数,请将其删除。
真实用法:
@Module
class MyModule {
// This is example module for true usage.
}
••• 错误用法:
@Module
class MyModule constructor(application: Application) {
// This is example module for wrong usage.
}