gpt4 book ai didi

android - Dagger 2 @Component.Builder 依赖项缺少 setter 错误

转载 作者:行者123 更新时间:2023-12-02 13:31:47 28 4
gpt4 key购买 nike

我正在尝试使用一些外部参数来实例化 Dagger(我正在编写一个库),但编译器不断给我以下错误:

error: @Component.Builder is missing setters for required modules or components: [com.example.domain.LibraryClient.Dependency]



依赖是一个简单的接口(interface),我用它来获取应用程序以创建空间。
   interface Dependency {
fun getApplication(): Application
}

这是我的库组件:
@Singleton
@Component(
modules = [AndroidInjectionModule::class, AndroidSupportInjectionModule::class, LibraryModule::class],
dependencies = [LibraryClient.Dependency::class])
interface LibraryComponent {

@Component.Builder
interface Builder {
@BindsInstance
fun client(client: LibraryClientImpl): Builder

fun build(): LibraryComponent
}

fun inject(target: LibraryClientImpl)
}

这是注入(inject)器:
object LibraryInjector {

private var deps: LibraryClient.Dependency? = null

fun setDependency(dependency: LibraryClient.Dependency) {
this.deps = dependency
}

fun init(client: LibraryClientImpl): LibraryComponent {

val appComponent = DaggerLibraryComponent.builder()
.dependency(deps)
.client(client)
.build()

appComponent.inject(client)

return appComponent
}
}

我见过一些例子,他们使用 @Component 中的依赖项选项。这似乎是正确的。如果我删除 dependencies = [LibraryClient.Dependency::class]它通过了构建。我错过了什么?

最佳答案

既然你有 dependencies = [LibraryClient.Dependency::class]@Component依赖你必须添加一个相应的@Component.Builder方法(二传手)也是。

interface Builder {
...
fun libraryClient(libraryClient: LibraryClient.Dependency): Builder
...
}

关于android - Dagger 2 @Component.Builder 依赖项缺少 setter 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60775974/

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