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
  • 🧩 多模块聚合示例
相关推荐
ljt27249606618 小时前
Compose笔记(二十四)--Canvas
笔记·android jetpack
韩仔搭建9 小时前
第二章:安卓端启动流程详解与疑难杂症调试手册
android·ui·娱乐
A-花开堪折9 小时前
Android7 Input(七)App与input系统服务建立连接
android
冰糖葫芦三剑客9 小时前
Android 自定义悬浮拖动吸附按钮
android
吃汉堡吃到饱9 小时前
【Android】从Choreographer到UI渲染(二)
android·ui
微信公众号:AI创造财富10 小时前
显示的图标跟UI界面对应不上。
android·ui
aningxiaoxixi10 小时前
安卓 Audio Stream 类型
android
奔跑吧 android10 小时前
【android bluetooth 协议分析 01】【HCI 层介绍 3】【NUMBER_OF_COMPLETED_PACKETS 事件介绍】
android·bluetooth·hci·bt·gd·aosp13
_龙小鱼_13 小时前
Kotlin扩展简化Android动画开发
android·开发语言·kotlin
奔跑吧 android13 小时前
【android bluetooth 协议分析 01】【HCI 层介绍 6】【WriteLeHostSupport命令介绍】
android·bluetooth·bt·gd·aosp13·writelehostsup·hcicmd