错误:找不到符号@dagger.hilt.InstallIn(value = {ApplicationComponent.class})

Mos*_*lah 52 android dagger-hilt

升级匕首柄后(版本:2.31-alphaApplicationComponent.class无法找到。Component类似 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)
}
Run Code Online (Sandbox Code Playgroud)

Mos*_*lah 131

ApplicationComponent在 Dagger 版本2.30 中 已弃用,
ApplicationComponent在 Dagger 版本2.31 中 删除
或者SingletonComponent应该使用代替ApplicationComponent

@Module
@InstallIn(SingletonComponent::class)
class RoomModule() {
   . . .
}
Run Code Online (Sandbox Code Playgroud)

  • **注意:** 对于那些在导入时仍然找不到 `SingletonComponent` 的人,它位于 `dagger.hilt.components.SingletonComponent` 包中。所有其他“组件”都在“dagger.hilt.android.components”包中。 (4认同)

Erc*_*can 29

ApplicationComponent更名为SingletonComponent


Ali*_*waz 10

只需导入:

import dagger.hilt.components.SingletonComponent
Run Code Online (Sandbox Code Playgroud)

并将您的模块注释为:

@Module
@InstallIn(SingletonComponent::class)
Run Code Online (Sandbox Code Playgroud)

因为 ApplicationComponent 在较新版本的 daggerHilt 中被删除,并且我在应用程序级 gradle 文件中使用这些依赖项作为 dagger hilt:

//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"
Run Code Online (Sandbox Code Playgroud)