gpt4 book ai didi

android - Dagger -Android : Missing setters for modules

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:01 28 4
gpt4 key购买 nike

数据模块:

@Module
class DataModule constructor(application: App){

private var db : Database = Room.databaseBuilder(application.applicationContext,
Database::class.java, "database.db")
.build()

@Provides
@PerApplication
fun provideDatabase(): Database {
return db
}

应用:

class App : DaggerApplication() {

@Inject lateinit var activityDispatchingAndroidInjector: DispatchingAndroidInjector<Activity>

override fun onCreate() {
super.onCreate()
setupTimber()
// setupCrashlytics()
RxPaparazzo.register(this)
}

override fun applicationInjector(): AndroidInjector<out App> {
return DaggerApplicationComponent.builder()
.dataModule(DataModule(this)).create(this)
}

应用组件:

@PerApplication
@Component(modules = [AndroidSupportInjectionModule::class, ActivityBindingModule::class, ApplicationModule::class, DataModule::class, ServiceModule::class])
interface ApplicationComponent : AndroidInjector<App> {
@Component.Builder
abstract class Builder : AndroidInjector.Builder<App>()

}

我得到错误:

Error:(21, 2) error: @Component.Builder is missing setters for required modules or components: [com.org.injection.module.DataModule]
public static abstract class Builder extends dagger.android.AndroidInjector.Builder<com.org.App> {
^

最佳答案

发生这种情况是因为您已经声明,DataModule 需要一个应用程序对象的实例才能构建,但您没有指定如何创建该模块。如果一个被 @Module 注释的类在其构造函数中没有任何参数(即有一个默认构造函数),那么 dagger 将负责创建模块。否则,您必须指定如何手动创建模块。

将最顶层(应用程序)组件更改为以下:


@Component(modules = [...])
interface AppComponent {
@Component.Builder
interface Builder {
// @BindsInstance will make `application` to be accessible in the graph
@BindsInstance
fun application(application: Application): Builder
fun build(): AppComponent
}
}

现在更改 DataModule 以请求应用程序对象的实例:


@Module
class DataModule {
@Provides
fun provideDatabase(application: Application): Database {
return ...
}
}

关于android - Dagger -Android : Missing setters for modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48682928/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com