Android Hilt

main
rayc 2024-04-25 13:33:42 +08:00
parent eadba9dd60
commit 9a7fe73010
3 changed files with 80 additions and 10 deletions

View File

@ -37,7 +37,19 @@
"state": { "state": {
"type": "markdown", "type": "markdown",
"state": { "state": {
"file": "工具相关文档/VSCode/VSCode 相关.md", "file": "日志检索.md",
"mode": "source",
"source": false
}
}
},
{
"id": "90f44bcb7cbd1134",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "Android Hilt.md",
"mode": "source", "mode": "source",
"source": false "source": false
} }
@ -56,7 +68,7 @@
} }
} }
], ],
"currentTab": 2 "currentTab": 3
} }
], ],
"direction": "vertical" "direction": "vertical"
@ -122,7 +134,7 @@
"state": { "state": {
"type": "backlink", "type": "backlink",
"state": { "state": {
"file": "工具相关文档/VSCode/VSCode 相关.md", "file": "Android Hilt.md",
"collapseAll": false, "collapseAll": false,
"extraContext": false, "extraContext": false,
"sortOrder": "alphabetical", "sortOrder": "alphabetical",
@ -139,7 +151,7 @@
"state": { "state": {
"type": "outgoing-link", "type": "outgoing-link",
"state": { "state": {
"file": "工具相关文档/VSCode/VSCode 相关.md", "file": "Android Hilt.md",
"linksCollapsed": false, "linksCollapsed": false,
"unlinkedCollapsed": true "unlinkedCollapsed": true
} }
@ -162,7 +174,7 @@
"state": { "state": {
"type": "outline", "type": "outline",
"state": { "state": {
"file": "工具相关文档/VSCode/VSCode 相关.md" "file": "Android Hilt.md"
} }
} }
} }
@ -183,8 +195,11 @@
"command-palette:打开命令面板": false "command-palette:打开命令面板": false
} }
}, },
"active": "30e0a2943950d852", "active": "90f44bcb7cbd1134",
"lastOpenFiles": [ "lastOpenFiles": [
"日志检索.md",
"Android Hilt.md",
"工具相关文档/VSCode/VSCode 相关.md",
"工具相关文档/Android Studio", "工具相关文档/Android Studio",
"工具相关文档/git/Git 笔记.md", "工具相关文档/git/Git 笔记.md",
"README.md", "README.md",
@ -192,7 +207,6 @@
"工具相关文档/git", "工具相关文档/git",
"工具相关文档/未命名.md", "工具相关文档/未命名.md",
"开发愿望清单.md", "开发愿望清单.md",
"日志检索.md",
"开发中/本地版本的cos图床.md", "开发中/本地版本的cos图床.md",
"工具相关文档/VSCode/VSCode 连接 云服务器.md", "工具相关文档/VSCode/VSCode 连接 云服务器.md",
"工具相关文档/VSCode/VSCode 文件嵌套.md", "工具相关文档/VSCode/VSCode 文件嵌套.md",
@ -218,8 +232,6 @@
"文档编写/PRD.md", "文档编写/PRD.md",
"文档编写", "文档编写",
"工具相关文档", "工具相关文档",
"UI学习/PS 学习/日志.md", "UI学习/PS 学习/日志.md"
"web/js/`JS` `setTimeout` 和 `clearTimeout`.md",
"web/css"
] ]
} }

55
Android Hilt.md Normal file
View File

@ -0,0 +1,55 @@
暂时不写博客,看[官方教程](https://developer.android.google.cn/training/dependency-injection/hilt-android?hl=zh-cn#generated-components))
不纠结于底层细节就看看在学了Android官方的教程之后的总结
### 引入依赖
- 项目根级 `build.gradle.kts`
```
plugins {
// hilt
id("com.google.dagger.hilt.android") version "2.48.1" apply false
}
```
- app 级 `build.gradle.kts`
```
plugins {
...
kotlin("kapt")
id("com.google.dagger.hilt.android")
}
android {
...
}
dependencies {
...
// hilt
implementation("com.google.dagger:hilt-android:2.48.1")
kapt("com.google.dagger:hilt-android-compiler:2.48.1")
}
kapt {
correctErrorTypes = true
}
```
### 为当前的App启用Hilt (必须)
创建一个 Application 的继承类,在 `AndroidManifest.xml` 中的 `application` 中设置当前的Application为这个 `Application` 添加 `@HiltAndroidApp`
### Android Hilt 使用时的容器及层次结构
Hilt 的顶层容器是 `Application` 容器, 这些容器,存在层级关系
- 层级关系如下
- `SingletonComponent::class` -- `Hilt` 根容器, 就是 Application 容器
- `ActivityComponent::class` -- ``
- `FragmentComponent::class`
- `ViewComponent::class`
- `ServiceComponent::class`
- `BroadcastReceiver::class`
-
上面的层级关系中,`SingletonComponent` 是最高层次,依次类推
### Android Hilt 中的作用
- 标注Hilt容器的注解
- `@HiltAndroidApp`
- `@AndroidEntryPoint`
-

View File

@ -47,3 +47,6 @@
## 2024-04-12 ## 2024-04-12
> Android 中的Service是在app的主线程中进行执行的如果在主线程中执行了耗时操作可能导致ANR > Android 中的Service是在app的主线程中进行执行的如果在主线程中执行了耗时操作可能导致ANR
## 2024-04-24
- [[Android Hilt]]