Dagger/MissingBinding 但模块存在

Dagger/MissingBinding but module exists

我正在尝试将 dagger 放入我的项目中,但我遇到了一个编译问题,我没有遇到过,因为我已经完成了 android 开发人员教程中的所有内容。我得到:

error: [Dagger/MissingBinding] INotificationService cannot be provided without an @Provides-annotated method

这是我的应用注释:

@HiltAndroidApp
class App : MultiDexApplication() {

Activity注解:

@AndroidEntryPoint
class MainActivity: AppCompatActivity() {

片段注释:

@AndroidEntryPoint
class NotificationFragment: Fragment(R.layout.fragment_notification) {

我认为在那之前一切都很好。那么我面临的问题就在这里:

视图模型class:

@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
}