在我的Android应用程序中,我遇到了Dagger 2的问题。我有一个包含3个依赖项的模块,其中两个是在活动中注入而不是第三个。
<httpRuntime maxRequestLength="whatever value you need in kb max value is 2,147,483,647 kb" relaxedUrlToFileSystemMapping="true" />
这是一个组件
@Module
class MyNumberWriterModule(_type: Boolean) {
private val type = _type
@Provides
@Singleton
fun provideSimpleNumberWriter(): SimpleNumberWriter {
return if (type) BelgianSimpleNumberWriter() else FrenchSimpleNumberWriter()
}
@Provides
@Singleton
fun provideIntegerNumberWriter(simpleNumberWriter: SimpleNumberWriter): IntWriter {
return IntegerNumberWriter(simpleNumberWriter)
}
@Provides
@Singleton
fun provideDecimalNumberWriter(intWriter: IntWriter): NumberWriter {
return DoubleNumberWriter(intWriter)
}
}
我的活动
@Singleton
@Component(modules = [(MyNumberWriterModule::class)])
internal interface MyNumberWriterComponent {
fun inject(mainActivity: MainActivity)
// fun inject(baseActivity: _BaseActivity)
}
为什么不能注入第三个class MainActivity : _BaseActivity() {
@Inject
lateinit var numberWriter: IntWriter
@Inject
lateinit var numberWriter2: NumberWriter
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
DaggerMyNumberWriterComponent.builder()
.myNumberWriterModule(MyNumberWriterModule(true))
.build()
.inject(this)
// Here numberWriter is injected
// Here numberWriter2 is NOT injected
}
}
的任何想法?我没有看到任何错误消息,但在Android Studio模块中显示从未使用@Provides
。运行项目时没有错误
答案 0 :(得分:0)
经过一个小时的实验后,我创建了我的类,我正在注入Not Found
和接口DoubleNumberWriter
,它在包中使用另一个名称实现我的业务逻辑,并且它开始工作。也许dagger不喜欢以前的包名NumberWriter