android 错误:找不到符号@dagger.hilt.InstallIn(值= {应用程序组件.类})

hs1ihplo  于 2023-01-07  发布在  Android
关注(0)|答案(8)|浏览(152)

升级匕首柄后(版本:2.31-alpha)找不到ApplicationComponent.class。对于RoomDatabase之类的Component,有什么替代方案?

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

oalqel3c1#

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

@Module
@InstallIn(SingletonComponent::class)
class RoomModule() {
   . . .
}
fjnneemd

fjnneemd2#

应用程序组件已重命名为单例组件

8dtrkrch

8dtrkrch3#

仅导入:

import dagger.hilt.components.SingletonComponent

并将模块注解为:

@Module
@InstallIn(SingletonComponent::class)

因为在较新版本的daggerHilt中删除了ApplicationComponent,所以我在应用级gradle文件中对daggerHilt使用这些依赖项:

//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"
0aydgbwb

0aydgbwb4#

除了已接受的答案,请务必更新到最新的剑柄版本,否则您将被卡住KaptExecution Error.
当前版本ext.hilt_version = '2.33-beta'

6mw9ycah

6mw9ycah5#

如果您不需要@InstallIn,则可以显式禁用。

Alternatively, the check can be disabled at the individual module level by annotating the module with @DisableInstallInCheck.

https://dagger.dev/hilt/flags.html中所述
喜欢

@DisableInstallInCheck
@Module
class MyAwesomeModule
7eumitmz

7eumitmz6#

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

xj3cbfub7#

应用程序组件已弃用。
@模块@安装(应用程序组件::类)
更新柄依赖项并使用SingletonComponent @Module @InstallIn(SingletonComponent::类)

//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'
gpfsuwkq

gpfsuwkq8#

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

}

相关问题