简体   繁体   English

Hilt - 如果没有 @Inject 构造函数或 @Provides 注释方法,则无法提供应用程序

[英]Hilt - app cannot be provided without an @Inject constructor or an @Provides-annotated method

This is my hilt module:这是我的刀柄模块:

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Provides
    @Singleton
    fun provideDatabase(app: App) =
        Room.databaseBuilder(app, AppDatabase::class.java, "app_database").build()

    @Provides
    fun provideUserDao(database: AppDatabase) = database.userDao()

    @Provides
    @Singleton
    fun provideApi(): ContactsService {
        return Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .addConverterFactory(MoshiConverterFactory.create(
                Moshi.Builder().build()))
            .build()
            .create(ContactsService::class.java)
    }

    @Provides
    @Singleton
    fun provideRepository(api:ContactsService,dao: ContactsDao): ContactRepository{
        return ContactRepositoryImpl(api,dao)
    }
}

This is the indicated App class:这是指定的应用程序 class:

@HiltAndroidApp
class App : Application() {
}

This is the error I get:这是我得到的错误:

 ..\assignment\App_HiltComponents.java:128: error: [Dagger/MissingBinding] com.example.assignment.App cannot be provided without an @Inject constructor or an @Provides-annotated method.

I double check every class that I use injection and in each of them I used @Inject annotation.我仔细检查了我使用注入的每一个 class,并且在每一个中我都使用了 @Inject 注释。 I read every stack question but none of them solved my problem.我阅读了每个堆栈问题,但没有一个解决了我的问题。

Hilt is built on top of the DI library Dagger. Hilt 构建在 DI 库 Dagger 之上。 Dagger works only with a specific type of class. Dagger 仅适用于特定类型 class。

Hilt provides @ApplicationContext as Context type. Hilt 提供 @ApplicationContext 作为上下文类型。

You should provide your App explicitly.您应该明确提供您的App

@Provides
fun provideApp(@ApplicationContext context: Context): App = context as App

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有@Inject 构造函数或@Provides-annotated 方法无法提供活动| 从 Dagger2 迁移到 Hilt - Activity cannot be provided without an @Inject constructor or an @Provides-annotated method | Migrating from Dagger2 to Hilt 不能在没有 @Inject 构造函数或从 @Provides-annotated 方法的情况下提供 - cannot be provided without an @Inject constructor or from an @Provides-annotated method 如果没有@Inject构造函数或@Provides注释的方法,则无法提供片段 - Fragment cannot be provided without an @Inject constructor or an @Provides-annotated method Dagger:不能在没有 @Inject 构造函数或 @Provides 注释的方法的情况下提供 - Dagger: cannot be provided without an @Inject constructor or an @Provides-annotated method Dagger-Hilt:[Dagger/MissingBinding] Object 如果没有 @Inject 构造函数或 @Provides 注释方法,则无法提供来自外部模块 - Dagger-Hilt: [Dagger/MissingBinding] Object from external Module cannot be provided without an @Inject constructor or an @Provides-annotated method 匕首柄:如果没有@Provides-annotated 方法,则无法提供 - Dagger Hilt: cannot be provided without an @Provides-annotated method 没有 @Provides-annotated 方法就无法提供。 刀柄 - cannot be provided without an @Provides-annotated method. hilt Dagger Hilt:如果没有 @Provides-annotated 方法,则无法提供存储库 - Dagger Hilt: Repository cannot be provided without an @Provides-annotated method 如果没有@Provides 注释的方法,则无法提供刀柄 - Hilt cannot be provided without an @Provides-annotated method 如果没有 @provides-annotated 方法就无法提供 - Dagger/Hilt - Cannot be provided without an @provides-annotated method - Dagger/Hilt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM