error: cannot find symbol @dagger.hilt.InstallIn(value = {ApplicationComponent.class})
AndroidDagger HiltAndroid Problem Overview
After upgrading dagger hilt(version: 2.31-alpha
) ApplicationComponent.class
can not find.
What is the alternative for a Component
like RoomDatabase?
@Module
@InstallIn(ApplicationComponent::class)
class RoomModule() {
private val DATABASE_NAME = "salat_time"
@Singleton
@Provides
fun provideRoomDatabase(@ApplicationContext appContext: Context) = Room.databaseBuilder(
appContext,
AppDatabase::class.java,
DATABASE_NAME
).createFromAsset("db/$DATABASE_NAME.sqlite").build()
@Singleton
@Provides
fun provideLocalSalatRepository(database: AppDatabase) = LocalSalatRepository(database)
@Singleton
@Provides
fun provideDistrictRepository(database: AppDatabase) = DistrictRepository(database)
}
Android Solutions
Solution 1 - Android
ApplicationComponent
is Deprecated in Dagger Version 2.30
ApplicationComponent
removed in Dagger Version 2.31
Alternatively SingletonComponent
should be used instead of ApplicationComponent
@Module
@InstallIn(SingletonComponent::class)
class RoomModule() {
. . .
}
Solution 2 - Android
ApplicationComponent is renamed to SingletonComponent
Solution 3 - Android
Just import:
import dagger.hilt.components.SingletonComponent
and annotate your module as:
@Module
@InstallIn(SingletonComponent::class)
Because ApplicationComponent is removed in the newer version of daggerHilt and I'm using these dependencies for dagger hilt within app level gradle file:
//Dagger - Hilt
implementation "com.google.dagger:hilt-android:2.31-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
Solution 4 - Android
In addition to the accepted answer, please be sure to update to the latest hilt version, or else you will be stuck with KaptExecution Error.
Current version ext.hilt_version = '2.33-beta'
Solution 5 - Android
If you do not need @InstallIn, you can always disable explicitly.
Alternatively, the check can be disabled at the individual module level by annotating the module with @DisableInstallInCheck.
As mentioned in https://dagger.dev/hilt/flags.html
Like
@DisableInstallInCheck
@Module
class MyAwesomeModule