Hilt-plus 简介

Hilt Plus

基于 Hilt 的轻量级扩展库,提供 Room 多模块聚合动态代理接口自动化注入能力,用 20% 的代码解决 80% 的样板代码问题。


安装

kotlin 复制代码
// build.gradle.kts
plugins {
    id("com.google.devtools.ksp") version "2.1.10-1.0.31" // 需启用 KSP
}

dependencies {
    implementation("com.google.dagger:hilt-android:2.56.2")
    ksp("com.google.dagger:hilt-compiler:2.56.2")
    implementation("top.cyclops:hilt-plus:version")
    ksp("top.cyclops:hilt-plus-compiler:version")
}

功能 1:动态代理接口自动化注入

核心原理

通过 @HiltApi 标记接口,自动生成 Hilt 的 Provider 模块,开发者只需提供 ApiCreator 实现类(如 Retrofit 实例)。

三步接入

1. 定义接口(自动生成 Provider)
kotlin 复制代码
@HiltApi
@ServerA  // 可选:配合限定符区分不同服务
interface GitHubApi {
    @GET("/users/{name}")
    suspend fun getUser(@Path("name") name: String): User
}
2. 提供 ApiCreator 实现
kotlin 复制代码
@dagger.Module
@InstallIn(SingletonComponent::class)
object RetrofitModule {
    @Provides
    @ServerA  // 匹配接口的限定符
    fun provideApiCreator(retrofit: Retrofit): ApiCreator = retrofit.create(clazz) 
}
3. 直接注入使用
kotlin 复制代码
@HiltViewModel
class UserViewModel @Inject constructor(
    @ServerA private val gitHubApi: GitHubApi // 自动注入
) : ViewModel() {
    fun loadUser(name: String) = viewModelScope.context {
        val user = gitHubApi.getUser(name) // 直接调用
    }
}

功能 2:Room 多模块聚合

痛点解决

  • 🚫 传统问题 :多模块需在 @Database 中手动聚合所有 Entity 和 Dao
  • Hilt Plus 方案 :通过 @HiltDao + 模块节点 自动聚合

单模块配置

1. 定义 Dao(自动注册到 Database)
kotlin 复制代码
@HiltDao(entities = [User::class]) // 声明关联的 Entity
@Dao
interface UserDao {
    @Query("SELECT * FROM User WHERE id = :id")
    fun getById(id: Long): Flow<User?>
}
2. 定义 Database(自动聚合)
kotlin 复制代码
@HiltRoom(name = "app", version = 1)
@TypeConverters(LocalDateConverter::class)
interface AppDatabase : DatabaseTransaction // 必须继承事务接口
🔑 可注入类型
kotlin 复制代码
@AndroidEntryPoint
class UserRepository @Inject constructor(
    private val userDao: UserDao,           // 直接注入 Dao
    private val dbTransaction: DatabaseTransaction // 注入事务管理
) {
    fun safeUpdate(user: User) = dbTransaction.runInTransaction {
        userDao.update(user)
    }
}

多模块配置(大型项目)

1. 定义模块节点
kotlin 复制代码
// module_user 模块
@Qualifier
annotation class UserModuleNode  // 用户模块节点

// module_music 模块
@Qualifier
annotation class MusicModuleNode // 音乐模块节点
2. 分模块声明 Dao
kotlin 复制代码
// module_user 中
@HiltDao(
    entities = [User::class], 
    node = UserModuleNode::class // 绑定到用户模块节点
)
@Dao
interface UserDao { /* ... */ }

// module_music 中
@HiltDao(
    entities = [Song::class], 
    node = MusicModuleNode::class // 绑定到音乐模块节点
)
@Dao
interface SongDao { /* ... */ }
3. 主模块聚合 Database
kotlin 复制代码
@HiltRoom(
    name = "main",
    version = 1,
    nodes = [UserModuleNode::class, MusicModuleNode::class] // 聚合多模块
)
interface MainDatabase : DatabaseTransaction
🔑 跨模块注入
kotlin 复制代码
@AndroidEntryPoint
class MusicService @Inject constructor(
    @MusicModuleNode  // 限定符指定模块节点
    private val transaction: DatabaseTransaction
) {
    fun importSongs(songs: List<Song>) = transaction.withTransction {
        // 使用音乐模块的事务上下文
    }
}

技术优势

功能 传统方案 Hilt Plus
Retrofit 接口注入 手动编写 @Provides 模块 通过 @HiltApi 自动生成
Room 多模块 需在主模块手动注册所有 Dao @HiltDao 分模块声明自动聚合
事务管理 需通过 @Database 实例获取 直接注入 DatabaseTransaction

注意事项

  1. KSP 版本对齐

    需保证 ksp 插件版本与 Kotlin 版本匹配。

  2. 模块可见性

    多模块场景中,@HiltDao 接口需对主模块可见(避免使用 internal)。

  3. ProGuard 规则

    若启用混淆,需添加规则保留生成的 _Database_Impl 类。


完整示例

访问 GitHub 仓库 获取:

  • 🛠️ 单模块完整 Demo
  • 🧩 多模块聚合示例
相关推荐
介一安全37 分钟前
【Frida Android】实战篇7:SSL Pinning 证书绑定绕过 Hook 教程阶段总结
android·网络安全·逆向·安全性测试·frida
用户2018792831671 小时前
Android 混淆引发的反序列化问题浅析
android
00后程序员张2 小时前
iOS 性能优化的体系化方法论 从启动速度到渲染链路的多工具协同优化
android·ios·性能优化·小程序·uni-app·iphone·webview
游戏开发爱好者82 小时前
iPhone重启日志深度解析与故障代码诊断
android·ios·小程序·https·uni-app·iphone·webview
TDengine (老段)5 小时前
TDengine 字符串函数 TO_BASE64 用户手册
android·大数据·服务器·物联网·时序数据库·tdengine·涛思数据
spencer_tseng6 小时前
Eclipse Oxygen 4.7.2 ADT(android developer tools) Plugin
android·java·eclipse
来来走走7 小时前
Android开发(Kotlin) 协程
android·java·kotlin
河铃旅鹿8 小时前
Android开发-java版:Framgent
android·java·笔记·学习
2501_9160088912 小时前
手机抓包app大全:无需root的安卓抓包软件列表
android·ios·智能手机·小程序·uni-app·iphone·webview