android Dagger 2:@Component.Builder缺少所需模块或组件的设置器:[应用程序示例网站匕首应用程序模块]“

i1icjdpr  于 2023-01-03  发布在  Android
关注(0)|答案(5)|浏览(120)

我正在配置新的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缺少所需模块或组件的设置器:[应用程序示例]

根据文件,应该是正确的,我错过了什么?
例如,这可能是具有生成器的有效组件:

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

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

yjghlzjz1#

从AppModule.class中移除以下代码并重新生成项目

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

mqkwyuun2#

我认为这对@BindsInstance的使用和@Provides ApplicationDagger 2 Component Builder的删除提供了更清晰的解释:

    • @BindsInstance什么?**

定义如下:
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
我来自春靴和匕首2是OMG这么多复杂
因此,根据我对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-v4Fragment与本机Fragment的使用可能是问题的根源。例如,对于support-v4,您需要使用AndroidSupportInjectionModuleHasSupportFragmentInjector,而对于本机,您需要使用AndroidInjectionModuleHasFragmentInjector

zmeyuzjn

zmeyuzjn3#

在我的例子中,我使用的是对象模块,所以我必须用@JvmStatic注解provider方法

@Module
object GsonModule {

    @JvmStatic
    @Singleton
    @Provides
    fun provideGson() = Gson()

}
raogr8fs

raogr8fs4#

“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.
}
eni9jsuy

eni9jsuy5#

在我的例子中,我的模块类有一个构造函数,删除@BindInstance是解决方案。
该单元:

@Module
class SomeModule(private val count:Int)

组件构建器:

@Component.Builder
interface Builder {
    fun engineModule(someModule: SomeModule): Builder
}

相关问题