【问题标题】:Dagger-Hilt: [Dagger/MissingBinding] Object from external Module cannot be provided without an @Inject constructor or an @Provides-annotated methodDagger-Hilt:如果没有 @Inject 构造函数或 @Provides-annotated 方法,则无法提供来自外部模块的 [Dagger/MissingBinding] 对象
【发布时间】:2022-01-04 21:30:30
【问题描述】:

我在我的应用模块中使用来自外部模块的AuthenticationRequest 对象。我将此对象作为对我的 AccountRepository 的依赖项提供。这就是我在 app 模块中定义依赖项的方式。

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

@Provides
@Singleton
fun provideRepository(
    authenticationRequest: AuthenticationRequest,
    accountDao: AccountDao
) = AccountRepository(authenticationRequestFactory, accountDao)

}

外部模块中AuthenticationRequest的构造函数:

class AuthenticationRequest(
   api: AuthenticationApi,
   apiError : ApiError =  ApiErrorImpl()
) : Request<AuthenticationApi>(api, apiError)

还有 AccountRepository:

class AccountRepository @Inject constructor(
   private val authenticationRequestFactory: AuthenticationRequestFactory,
   private val accountDao: AccountDao
) {....}

但我收到以下错误消息: Dagger/MissingBinding] com.external.auth.AuthenticationRequest 不能在没有@Inject 构造函数或@Provides-annotated 方法的情况下提供。

如何正确注入AuthenticationRequest 对象?请注意,定义此对象的模块不使用 Dagger-Hilt。它虽然使用 Dagger-2。任何指针都会很棒。

注意:项目结构是

|-- :app
 -- :external module

【问题讨论】:

    标签: android dagger-2 dagger-hilt


    【解决方案1】:

    Dagger/Hilt 只能创建具有@Inject 构造函数的类。正如你所说,AuthenticationRequest 类不是这样设置的。

    你需要添加一个@Provides方法让Dagger知道如何创建类:

    
    @InstallIn(SingletonComponent::class)
    @Module
    object ApplicationModule {
        @Provides
        @Singleton
        fun provideAuthenticationRequest(
            someDependency: SomeDependency,
            anotherOne: AnotherOne,
        ): AuthenticationRequest {
            return AuthenticationRequest(
                someDependency,
                anotherOne
            )
        }
    
        @Provides
        @Singleton
        fun provideSomeDependency(): SomeDependency {
            return SomeDependency()
        }
    
        @Provides
        @Singleton
        fun provideAnotherOne(
            evenMoreDependency: EvenMoreDependency
        ): AnotherOne {
            return AnotherOne(evenMoreDependency)
        }
    
        @Provides
        @Singleton
        fun provideEvenMoreDependency(): EvenMoreDependency {
            return EvenMoreDependency()
        }
    
    }
    

    正如@bapness 所说,这应该是一个对象。您只需要在创建@Binds 方法时创建一个(抽象)类,因为 Hilt 将进行实现。但在这种情况下不需要这样做。

    至于是否应该全部在一个组件中 - 这完全取决于您。我会说,如果您不确定然后将它们全部放在一个对象中,那么以后将它们迁移到自己的文件中并不是什么大问题。

    更多信息:https://dagger.dev/dev-guide/ 在“满足依赖关系”下

    【讨论】:

    • 这行不通。 AuthenticationRequest 对象有其自身的更多依赖项,我发现的所有使用 Hilt 和第三方依赖项的示例都没有显示具有任何依赖项的示例。
    • 我认为@gilles 是对的。您可以将AuthentifcationRequest 提供函数拆分为:external_module 中的专用文件。此外,如以下链接中所述,您应该为您的 Hilt @Module 声明 object 而不是 classdeveloper.android.com/training/dependency-injection/…
    • @GraSim 您必须为链中所有必需的依赖项创建更多提供方法,直到没有依赖项为止。
    • @GillesBraun 我是否在同一个类中提供所有依赖项?它是否需要声明为对象或类?如果你能提供一个完整的有效答案,我会投票,以便其他遇到这个问题的人可以看到解决方案。
    • @GraSim 我用更完整的示例和其他信息更新了答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 2019-11-04
    • 2022-10-24
    相关资源
    最近更新 更多