我正试图将dagger放入我的项目中,但我面临着一个编译问题,因为我已经完成了类似android开发者教程的所有内容。我得到:
错误:没有@Providers注释方法,就无法提供[Dagger/MissingBinding]INotificationService
这是我的应用程序注释:
@HiltAndroidApp
class App : MultiDexApplication() {
活动注释:
@AndroidEntryPoint
class MainActivity: AppCompatActivity() {
片段注释:
@AndroidEntryPoint
class NotificationFragment: Fragment(R.layout.fragment_notification) {
我认为在那之前一切都好。那么我面临的问题是:
Viewmodel类:
@HiltViewModel
class NotificationViewModel @Inject constructor(private val notificationService: INotificationService): ViewModel()
这是INotificationService的接口:
interface INotificationService {
fun refreshNotification(): Single<List<INotification>>
fun markAsRead(notification: INotification)
}
以及实现:
class NotificationServiceImpl @Inject constructor(@ApplicationContext context: Context): INotificationService
带相关模块:
@Module
@InstallIn(ActivityComponent::class)
abstract class NotificationModule {
@Binds
abstract fun bindNotificationService(impl: NotificationServiceImpl): INotificationService
}
模块中的bindNotificationService绑定函数是灰色的,android开发人员教程中的情况并非如此,这个错误让我觉得我错过了一些东西,无法在编译时找到这个函数,但由于有@module和@InstallIn(ActivityComponent::class(,我完全不知道它为什么不编译。
INotificationService是ViewModel依赖项,它应该与ViewModel生命周期绑定,而不是这个
@Module
@InstallIn(ActivityComponent::class)
abstract class NotificationModule {
@Binds
abstract fun bindNotificationService(impl: NotificationServiceImpl): INotificationService
}
使用此:-
@Module
@InstallIn(ViewModelComponent::class)
abstract class NotificationModule {
@Binds
abstract fun bindNotificationService(impl: NotificationServiceImpl): INotificationService
}