84

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)
}
Mostasim Billah
  • 4,447
  • 3
  • 19
  • 28

8 Answers8

252

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() {
   . . .
}
Mostasim Billah
  • 4,447
  • 3
  • 19
  • 28
  • 5
    **Note:** For those who are still unable to find `SingletonComponent` while importing, it is in `dagger.hilt.components.SingletonComponent` package. All other `Components` are in `dagger.hilt.android.components` package. – Pranav Karnik Mar 24 '21 at 07:25
  • Oh, It really made me confused, Thanks Dude – Hossein Kurd Jul 25 '23 at 12:00
46

ApplicationComponent is renamed to SingletonComponent

Ercan
  • 2,601
  • 22
  • 23
10

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"
Ali Nawaz
  • 2,016
  • 20
  • 30
1

ApplicationComponent is Deprecated.

@Module @InstallIn(ApplicationComponent::class)

Update hilt dependency and use SingletonComponent @Module @InstallIn(SingletonComponent::class)

//Hilt

implementation 'com.google.dagger:hilt-android:2.43.2'
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
kapt 'com.google.dagger:hilt-android-compiler:2.43.2'
kapt 'androidx.hilt:hilt-compiler:1.0.0'
1

@Module
@InstallIn(ApplicationComponent::class)
abstract class RoomModule
{
@Binds
@Singleton
abstract fun binding(
    obj: UserImpl
):user}




ApplicationComponent has been replaced by SingletonComponent.

@Module
@InstallIn(SingletonComponent::class)
class RoomModule {

}
UJJAWAL KUMAR
  • 31
  • 1
  • 6
0

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'

Atiq
  • 14,435
  • 6
  • 54
  • 69
0

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
0
@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Provides
    @Singleton
    fun mProvidersTaskDatabase(app: Application) : TaskDataBase{
        return Room.databaseBuilder(
            app,
            TaskDataBase::class.java,
            "Task_db"
        ).build()
    }

    @Provides
    @Singleton
    fun mProvidersTaskRepository (dataBase: TaskDataBase) : TaskRepository{
        return TaskRepositoryImpl(dataBase.mTaskDao)
    }
}
Saqib00786
  • 29
  • 5