Hilt 在多模块项目中的落地实战:依赖注入的边界与模块化设计

简介: 本文详解Hilt在多模块Android项目中的实战落地,涵盖跨模块依赖注入、接口解耦、Component协调、测试Mock方案及常见问题排查,强调“接口定义在domain、实现分离于data、feature仅依赖抽象”的模块化设计原则

Hilt 在多模块项目中的落地实战:依赖注入的边界与模块化设计

引言

在单模块项目中,Hilt 的依赖注入配置通常比较直接:定义 Module、标注 @Inject、编译通过即可使用。但当项目逐步模块化后,依赖注入的复杂度会显著上升:

  • 如何在 feature 模块中注入来自 data 模块的 Repository?
  • 不同模块的 Hilt Component 如何协调?
  • 测试时如何替换跨模块的依赖?
  • 模块边界应该如何划分,才能让依赖注入保持清晰?

本文将从多模块项目的实际场景出发,梳理 Hilt 在模块化架构中的落地思路、常见问题与解决方案。


多模块 Hilt 的基本配置

Gradle 配置

在多模块项目中,Hilt 的配置需要在多个模块的 build.gradle 中分别声明:

app 模块(应用模块):

plugins {
    id("com.android.application")
    id("kotlin-android")
    id("kotlin-kapt")
    id("dagger.hilt.android.plugin")
}

dependencies {
    implementation("com.google.dagger:hilt-android:2.48")
    kapt("com.google.dagger:hilt-compiler:2.48")
}

feature 模块data 模块等(库模块):

plugins {
    id("com.android.library")
    id("kotlin-android")
    id("kotlin-kapt")
    id("dagger.hilt.android.plugin") // 每个模块都需要
}

dependencies {
    implementation("com.google.dagger:hilt-android:2.48")
    kapt("com.google.dagger:hilt-compiler:2.48")
}

Application 类的配置

Hilt 的入口仍然是 @HiltAndroidApp 标注的 Application 类,它只能存在于 app 模块:

@HiltAndroidApp
class MyApplication : Application()

其他模块不需要再定义 Application,它们会共享 app 模块的 Hilt Component。


跨模块依赖注入的常见问题

问题一:feature 模块无法直接依赖 data 模块的实现类

假设项目结构如下:

:app
:feature:home
:data:repository
:data:network

:feature:home 中,ViewModel 需要注入 UserRepository

// feature/home 模块
@HiltViewModel
class HomeViewModel @Inject constructor(
    private val userRepository: UserRepository // 编译失败:找不到 UserRepository
) : ViewModel()

原因::feature:home 没有依赖 :data:repository 模块,无法访问其中的类。

解决方案:通过接口解耦

  1. :core:domain:data:repository 的公开接口部分定义接口:
// core/domain 模块
interface UserRepository {
    suspend fun getUser(id: String): User
}
  1. :data:repository 中实现接口:
// data/repository 模块
class UserRepositoryImpl @Inject constructor(
    private val api: UserApi
) : UserRepository {
    override suspend fun getUser(id: String): User {
        return api.fetchUser(id)
    }
}
  1. :data:repository 的 Hilt Module 中绑定接口与实现:
@Module
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {

    @Binds
    @Singleton
    abstract fun bindUserRepository(
        impl: UserRepositoryImpl
    ): UserRepository
}
  1. :feature:home 中依赖接口:
@HiltViewModel
class HomeViewModel @Inject constructor(
    private val userRepository: UserRepository // 注入接口
) : ViewModel()

模块依赖关系:

:feature:home -> :core:domain (接口)
:data:repository -> :core:domain (接口)
:app -> :feature:home, :data:repository

这样,:feature:home 只依赖接口,不依赖具体实现,模块边界更清晰。


模块边界的划分与接口设计

推荐的模块结构

:app                      // 应用入口,组装所有模块
:core:domain              // 业务接口与 Model
:core:common              // 通用工具、扩展函数
:data:network             // 网络层实现(Retrofit、OkHttp)
:data:local               // 本地存储(Room、DataStore)
:data:repository          // Repository 实现
:feature:home             // 首页功能模块
:feature:profile          // 个人资料功能模块

依赖原则

  • feature 模块:只依赖 :core:domain:core:common,不依赖其他 feature 或 data 实现
  • data 模块:实现 :core:domain 中的接口,可以相互依赖(如 :data:repository 依赖 :data:network
  • app 模块:依赖所有 feature 和 data 模块,负责组装

接口设计的注意事项

  1. 接口放在 domain 模块,不要放在 data 模块内部,否则 feature 模块无法直接依赖
  2. 返回值使用 domain 模型,不要暴露 DTO 或数据库 Entity
  3. 接口粒度适中,不要为了"解耦"而过度拆分,导致接口爆炸

测试替换与 Mock 注入

问题:测试时如何替换 Repository?

在单元测试中,我们通常需要用 Fake 或 Mock 实现替换真实的 Repository,但 Hilt 默认使用 SingletonComponent 中的绑定,无法轻易替换。

解决方案一:使用 @TestInstallIn

Hilt 提供了 @TestInstallIn 注解,可以在测试中替换 Module:

// test 目录
@Module
@TestInstallIn(
    components = [SingletonComponent::class],
    replaces = [RepositoryModule::class] // 替换生产环境的 Module
)
abstract class FakeRepositoryModule {

    @Binds
    @Singleton
    abstract fun bindUserRepository(
        impl: FakeUserRepository
    ): UserRepository
}

class FakeUserRepository @Inject constructor() : UserRepository {
    override suspend fun getUser(id: String): User {
        return User(id, "Fake User")
    }
}

测试代码:

@HiltAndroidTest
class HomeViewModelTest {

    @get:Rule
    val hiltRule = HiltAndroidRule(this)

    @Inject
    lateinit var repository: UserRepository // 自动注入 FakeUserRepository

    @Test
    fun testGetUser() = runTest {
        val user = repository.getUser("123")
        assertEquals("Fake User", user.name)
    }
}

解决方案二:抽取独立的测试模块

如果多个测试类需要共享 Fake 实现,可以将 Fake 实现和 Module 放在独立的 test-shared 模块中:

:test-shared
  - FakeUserRepository.kt
  - FakeRepositoryModule.kt

在测试模块的 build.gradle 中依赖:

testImplementation(project(":test-shared"))

实战案例:网络层与存储层的模块化注入

案例:构建一个离线优先的用户信息获取流程

模块结构

:core:domain -> UserRepository 接口
:data:network -> UserApi (Retrofit)
:data:local -> UserDao (Room)
:data:repository -> UserRepositoryImpl (组合 network + local)
:feature:profile -> ProfileViewModel (使用 UserRepository)

data/network 模块

interface UserApi {
    @GET("users/{id}")
    suspend fun fetchUser(@Path("id") id: String): UserDto
}

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

    @Provides
    @Singleton
    fun provideRetrofit(): Retrofit {
        return Retrofit.Builder()
            .baseUrl("https://api.example.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    @Provides
    @Singleton
    fun provideUserApi(retrofit: Retrofit): UserApi {
        return retrofit.create(UserApi::class.java)
    }
}

data/local 模块

@Entity(tableName = "users")
data class UserEntity(
    @PrimaryKey val id: String,
    val name: String
)

@Dao
interface UserDao {
    @Query("SELECT * FROM users WHERE id = :id")
    suspend fun getUser(id: String): UserEntity?

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertUser(user: UserEntity)
}

@Database(entities = [UserEntity::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
}

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

    @Provides
    @Singleton
    fun provideDatabase(@ApplicationContext context: Context): AppDatabase {
        return Room.databaseBuilder(
            context,
            AppDatabase::class.java,
            "app_database"
        ).build()
    }

    @Provides
    fun provideUserDao(database: AppDatabase): UserDao {
        return database.userDao()
    }
}

data/repository 模块

class UserRepositoryImpl @Inject constructor(
    private val userApi: UserApi,
    private val userDao: UserDao
) : UserRepository {

    override suspend fun getUser(id: String): User {
        // 先读本地
        val cachedUser = userDao.getUser(id)
        if (cachedUser != null) {
            return cachedUser.toDomain()
        }

        // 再请求网络
        val remoteUser = userApi.fetchUser(id)
        val entity = UserEntity(remoteUser.id, remoteUser.name)
        userDao.insertUser(entity)
        return entity.toDomain()
    }

    private fun UserEntity.toDomain() = User(id, name)
}

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

    @Binds
    @Singleton
    abstract fun bindUserRepository(
        impl: UserRepositoryImpl
    ): UserRepository
}

feature/profile 模块

@HiltViewModel
class ProfileViewModel @Inject constructor(
    private val userRepository: UserRepository
) : ViewModel() {

    private val _userState = MutableStateFlow<User?>(null)
    val userState: StateFlow<User?> = _userState.asStateFlow()

    fun loadUser(id: String) {
        viewModelScope.launch {
            _userState.value = userRepository.getUser(id)
        }
    }
}

依赖关系图

:app
  -> :feature:profile (依赖 :core:domain)
  -> :data:repository (依赖 :core:domain, :data:network, :data:local)
  -> :data:network
  -> :data:local

这样的设计让 :feature:profile 完全不感知网络和数据库的实现细节,只通过接口交互,测试时可以轻松替换 Fake 实现。


常见问题排查

问题:编译时提示 "Hilt component not found"

原因:某个模块没有正确配置 Hilt 插件或依赖。

解决方案

  1. 确认所有需要注入的模块都添加了 dagger.hilt.android.plugin
  2. 确认 kapt("com.google.dagger:hilt-compiler:2.48") 在所有模块中都配置了
  3. 清理构建缓存:./gradlew clean

问题:注入的实例为 null

原因:可能是 Module 的 @InstallIn 注解配置错误,或者 Component 生命周期不匹配。

解决方案

  • 检查 Module 是否正确安装到了 SingletonComponent
  • 检查被注入的类是否标注了 @Inject 构造函数
  • 检查 ViewModel 是否使用了 @HiltViewModel 注解

问题:循环依赖

原因:两个类相互依赖,Hilt 无法确定注入顺序。

解决方案

  1. 重构代码,打破循环依赖(推荐)
  2. 使用 Provider<T>Lazy<T> 延迟注入:
class A @Inject constructor(
    private val bProvider: Provider<B>
) {
    fun doSomething() {
        val b = bProvider.get() // 延迟获取 B 的实例
    }
}

总结

Hilt 在多模块项目中的核心思路是:

  1. 接口与实现分离:接口定义在 domain 模块,实现在 data 模块,feature 模块只依赖接口
  2. 模块边界清晰:feature 不依赖 feature,feature 不依赖 data 实现,依赖关系单向流动
  3. 测试友好:通过 @TestInstallIn 替换 Module,或者抽取独立的测试模块
  4. 统一的 Component:所有模块共享 app 模块的 @HiltAndroidApp,不需要在每个模块中重复定义

当项目规模持续增长时,良好的模块化设计配合 Hilt 的依赖注入能力,可以让代码保持清晰、可测试、可维护。


推荐阅读

相关文章
|
5天前
|
存储 弹性计算 缓存
阿里云服务器租赁费用:新版租赁收费标准及活动报价参考
本文更新了2026年阿里云全系列云服务器租赁活动报价,所有特惠资源均可前往阿里云活动中心选购,整体覆盖从个人入门到企业级高性能场景的全梯度需求。其中轻量应用服务器主打极致性价比,2核2G峰值200M带宽配置每日10点、15点限时抢购价仅38元/年,2核4G配置379元/年起;高性价比的经济型e实例、通用算力型u2i实例覆盖2核4G至4核32G全档位,适配开发测试与中小型企业业务;搭载英特尔至强6处理器的第九代c9i企业级实例算力较上代提升20%,支撑高并发生产环境,不同实例规格价差清晰,用户可根据自身业务负载与预算灵活选型。
1568 111
|
12天前
|
云安全 人工智能 运维
阿里云联动百位企业安全专家,共识Agent防御最佳实践
当Agent成为新员工,你的安全边界在哪里?
1939 8
阿里云联动百位企业安全专家,共识Agent防御最佳实践
|
6天前
|
人工智能 程序员 API
Codex 接入 DeepSeek-V4-Flash:还能补上识图,提供两套方案
Codex 接入 DeepSeek-V4-Flash 怎么配?本文覆盖 CLI 与桌面端,再用 qwen3-vl-flash 补识图,两套方案可直接照做
|
6天前
|
编解码 人工智能 安全
2核4G/4核8G/8核16G阿里云服务器如何选择实例?经济型e、通用算力型u2i与计算型c9i选哪个?
本文介绍了阿里云2核4G、4核8G、8核16G三档主流配置下经济型e、通用算力型u2i和计算型c9i三种实例的最新活动价格与适用场景。同配置下三者价差显著,以2核4G为例,经济型e低至599.93元/年,计算型c9i则高达1742.08元/年。文章详细解析了各实例的性能定位:经济型e适合轻负载入门场景,u2i兼顾稳定算力与性价比,c9i凭借第9代至强处理器与芯片级安全能力支撑高性能业务。同时提示用户可叠加满减优惠券享受折上折,建议根据业务负载与预算综合决策。
526 112
|
18天前
|
人工智能 前端开发 Linux
Codex 桌面版安装 + CC Switch 接入第三方 API 完整教程(2026 最新)
2026最新教程:手把手教你安装Codex桌面版,通过CC Switch v3.17.0一键接入Fenno等国产API(兼容OpenAI Responses格式),跳过账号登录,完整启用代码审查、多步任务与上下文感知功能。零基础友好,全程图文实操。(239字)
2551 4
|
10天前
|
存储 人工智能 关系型数据库
阿里云AI产品与云产品最新组合套餐:Token Plan、AI coding及云服务器和建站等组合优惠价
阿里云推出全新“算力+模型+应用”一站式云与AI组合套餐活动,覆盖从个人开发者到中大型企业的全场景需求。核心亮点为分三档定价的Token Plan订阅服务,支持Qwen3.8-Max-Preview大模型调用,错峰时段最低可享0.2折优惠。活动同步推出AI Coding、智能体部署、云电脑托管、0代码建站等十余类场景化组合,搭配99元/年的普惠云服务器、88元/年的入门数据库等经典特惠产品,还为企业提供1V1定制化AI转型方案,大幅降低了不同用户群体拥抱AI的技术门槛与采购成本。
720 111
|
20天前
|
人工智能 JSON 安全
Fastjson远程代码执行漏洞,阿里云AI安全为您保驾护航
阿里云AI安全产品联动防御Fastjson攻击
2634 13
Fastjson远程代码执行漏洞,阿里云AI安全为您保驾护航
|
6天前
|
人工智能 JSON Shell
2026AI漫剧本地全开源方案(附各个软件模型链接),8G显卡也能流畅运行
这是一套完全本地化部署的AI漫剧生成技术链路:涵盖LLM剧本分镜生成、FLUX文生图(IP-Adapter人脸锁定)、StoryDiffusion时序连贯控制、LTX-2.3唇形同步视频生成,及ComfyUI全流程调度。零云端费用,仅耗硬件算力,单集2–4小时可产出竖屏短视频,适配抖音/B站分发。
|
7天前
Qoder 一周年 × Qwen3.8-Max 正式上线,多重好礼限时领
8月3日,Qwen3.8-Max 正式上线Qoder,迎来Qoder一周年。新老用户可领800次免费调用,下单再赠2000次;夜间(22:00–08:00)调用5折;邀请好友双方得积分与调用额度。
443 1