You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多模块Clean架构Android应用Hilt注入报错:MissingBinding Repository

解决Clean架构多模块Android应用中Hilt的MissingBinding错误

问题场景

采用Clean架构分层的多模块Android应用,模块结构如下:

  • root > app(主模块)
  • root > presentation(安卓库模块,依赖domain)
  • root > domain(安卓库模块)
  • root > data(安卓库模块,依赖domain)

Domain层定义了UserCredentialsRepository接口和依赖该接口的UseCase(如SaveUserUseCase),Data层实现了Repository并通过Hilt Module配置依赖注入,但编译时出现以下错误:

/AndroidStudioProjects/CleanArchitectureSample/app/build/generated/hilt/component_sources/debug/com/cleanarchitecturesample/app/CleanArchitectureApp_HiltComponents.java:137:
error: [Dagger/MissingBinding]
com.cleanarchitecturesample.domain.respository.UserCredentialsRepository cannot be provided without an @Provides-annotated method.

错误链显示:Repository被注入到UseCase,UseCase被注入到ViewModel,但Hilt找不到Repository的提供方法。


解决方案

1. 检查模块依赖关系

核心问题:app主模块必须直接依赖data模块
Hilt的根组件(如SingletonComponent)在app模块生成,只有当app模块能访问到data模块的Hilt Module时,Dagger才能生成对应的绑定代码。

在app模块的build.gradle中添加data模块依赖:

dependencies {
    // 其他依赖
    implementation project(":presentation")
    implementation project(":data") // 必须添加这行
}

2. 确保各模块正确配置Hilt

每个涉及Hilt的模块都需要配置对应的插件和依赖:

data模块(build.gradle)

plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'dagger.hilt.android.plugin' // 必须添加Hilt插件
}

dependencies {
    implementation project(":domain")
    implementation "com.google.dagger:hilt-android:2.44" // 替换为你的Hilt版本
    kapt "com.google.dagger:hilt-compiler:2.44"
    // 其他依赖(如本地数据源、网络库等)
}

presentation模块(build.gradle)

plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'dagger.hilt.android.plugin'
}

dependencies {
    implementation project(":domain")
    implementation "com.google.dagger:hilt-android:2.44"
    kapt "com.google.dagger:hilt-compiler:2.44"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03" // ViewModel Hilt支持
    kapt "androidx.hilt:hilt-compiler:1.0.0"
    // 其他依赖(如ViewModel、Coroutines等)
}

app模块(build.gradle)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'dagger.hilt.android.plugin'
}

dependencies {
    implementation project(":presentation")
    implementation project(":data")
    implementation "com.google.dagger:hilt-android:2.44"
    kapt "com.google.dagger:hilt-compiler:2.44"
}

同时,app模块的Application类必须添加@HiltAndroidApp注解:

@HiltAndroidApp
class CleanArchitectureApp : Application()

3. 验证Repository绑定配置

如果使用@Provides方式:
确保Data层的Module正确返回Repository接口,且依赖的UserCredentialsLDS也能被Hilt提供:

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

    @Singleton
    @Provides
    fun provideUserCredentialsRepository(
        userCredentialsLDS: UserCredentialsLDS
    ): UserCredentialsRepository = UserCredentialsRepositoryImpl(userCredentialsLDS)
}

如果使用@Binds方式:
确保Repository实现类的构造函数添加@Inject注解,且Module配置正确:

// Repository实现类
class UserCredentialsRepositoryImpl @Inject constructor(
    private val userCredentialsLDS: UserCredentialsLDS
) : UserCredentialsRepository {
    // ...实现代码
}

// Hilt Module
@Module
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {
    @Binds
    @Singleton
    abstract fun bindUserCredentialsRepository(
        impl: UserCredentialsRepositoryImpl
    ): UserCredentialsRepository
}

4. 清理缓存并重新编译

执行以下操作清理Hilt和Gradle缓存:

  1. Android Studio菜单栏:Build > Clean Project
  2. 接着执行:Build > Rebuild Project
  3. 或使用命令行:
./gradlew clean assembleDebug

内容的提问来源于stack exchange,提问作者mithil1501

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 05:36:00