I recently added a new class to be provided by Hilt as a singleton. But whenever I click Build -> Rebuild Project in Android Studio, I get the following error:
error: [Dagger/MissingBinding] com.example.core.api.service.AlertMainService cannot be provided without an @Provides-annotated method.
For the last day or so I’ve been tearing my hair out trying to fix this error. The app doesn’t have multiple modules, so there isn’t any issue with that. I tried cleaning the project, restarting Android Studio, invalidating caches, deleting the gradle directory in the project, updating dependencies, updating Android Studio itself, and restarting my Mac. No dice.
At some point I just decided to try cleaning the project again and clicking the Run button to run it on my device… and it worked!
But why??? Why would I be able to build it and run on the device, but not rebuild it?
Unfortunately I can’t just ignore the issue, as it won’t properly build in our CI either.
In the module, it looks like this:
@Module
@InstallIn(SingletonComponent::class)
class NetworkModule {
@Singleton
@Provides
fun provideAlertMainService(
alertAuthenticationInterceptor: AlertAuthenticationInterceptor,
hostSelectionInterceptor: HostSelectionInterceptor,
prefs: SharedPrefs
): AlertMainService {
...
}
This is almost identical to another singleton class called AlertService
that is already being provided in the same module by Hilt without any issue:
@Singleton
@Provides
fun provideAlertService(
alertAuthenticationInterceptor: AlertAuthenticationInterceptor,
hostSelectionInterceptorAlert: HostSelectionInterceptorAlert,
prefs: SharedPrefs
): AlertService {
...
}
I thought maybe there was an issue with the fact that they have the same parameters, so I tried changing up their names and even adding/removing parameters. No change in behavior.
The class in which I am injecting them looks like this:
@Singleton
class AlertRepository @Inject constructor(
private val alertService: AlertService,
private val alertMainService: AlertMainService
) {
...
}
If I remove the AlertMainService
from being injected there, it builds just fine.
Is this some sort of bug with Hilt? What’s the deal here?