1、build.gradle 添加 plugin

  1. buildscript {
  2. ...
  3. dependencies {
  4. ...
  5. classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40'
  6. }
  7. }

2、app/build.gradle 添加配置

  1. plugins {
  2. ...
  3. id 'kotlin-kapt'
  4. id 'dagger.hilt.android.plugin'
  5. }
  6. android {
  7. ...
  8. }
  9. dependencies {
  10. ...
  11. implementation "com.google.dagger:hilt-android:2.40"
  12. kapt "com.google.dagger:hilt-compiler:2.40"
  13. }

3、application 添加 @HiltAndroidApp

所有使用 Hilt 的应用程序都必须在 application 加上 @HiltAndroidApp
@HiltAndroidApp 触发 Hilt 的代码生成,包括用作应用程序级依赖项容器的应用程序的基类。

  1. @HiltAndroidApp
  2. class ExampleApplication : Application() { ... }

4、将依赖注入 Android 类

  1. @AndroidEntryPoint
  2. class ExampleActivity : AppCompatActivity() { ... }

5、提供可通过注解注入的类

5.1、@Inject 一般用于简单类注入

  1. class Bar @Inject constructor(){
  2. var str: String? = "hello"
  3. }
  4. class K2Vm @Inject constructor(var bar: Bar? = null) {
  5. ...
  6. }
  7. @AndroidEntryPoint
  8. class K2Activity : AppCompatActivity() {
  9. // 直接注入
  10. @Inject lateinit var bar: Bar
  11. // 间接注入
  12. @Inject lateinit var vm: K2Vm
  13. }

@AndroidEntryPoint 是注入入口,可通过此类对对象进行注入

5.2、@module 一般用于第三方或繁琐对象注入

  1. @Module
  2. @InstallIn(ActivityComponent::class)
  3. class AppModule {
  4. @Provides
  5. fun provideBar(): Bar {
  6. return Bar().apply { str = "hello" }
  7. }
  8. }
  9. @AndroidEntryPoint
  10. class K2Activity : AppCompatActivity() {
  11. // 直接注入
  12. @Inject lateinit var bar: Bar
  13. // 间接注入
  14. @Inject lateinit var vm: K2Vm
  15. }

6、相应的一些注解如下

@HiltAndroidApp

触发Hilt的代码生成,包括适用于应用程序的基类,可以使用依赖注入,应用程序容器是应用程序的父容器,这意味着其他容器可以访问其提供的依赖项。

@AndroidEntryPoint

其会创建一个依赖容器,该容器遵循Android类的生命周期

@Inject

用来注入的字段,其类型不能为Private
如果要告诉 Hilt 如何提供相应类型的实例,需要将 @Inject 添加到要注入的类的构造函数中。
Hilt有关如何提供不同类型的实例的信息也称为绑定

@Install(xx)

Install 用来告诉 Hilt 这个模块会被安装到哪个组件上.

组件(Compenent)

Hitl默认有以下标准组件,只需要在类上增加 @AndroidEntryPoint 即可支持以下类的注入

Compenent Injector for
ApplicationComponent Application
ActivityRetainedComponent ViewModel(请参阅JetPack-ViewModel扩展)
ActivityComponent Activity
FragmentComponent Fragment
ViewComponent View
ViewWithFragmentComponent View 与 @WithFragmentBindings
ServiceComponent Service

需要注意的是,Hilt仅支持扩展FragmentActivity(如AppCompatActivity)的活动和扩展Jetpack库的片段Fragment,而不支持FragmentAndroid平台(现已弃用)的 片段 。

组件(Compenent)的生命周期

  • 它限制了在创建组件和生成组件范围绑定的生命周期
  • 它指示合适可以使用成员注入的值。(例如:当@Inject 字段不为null时) | Component | 作用范围 | Created at | Destroyed at | | —- | —- | —- | —- | | ApplicationComponent | @Singleton | Application#onCreate() | Application#onDestroy() | | ActivityRetainedComponent | @ActivityRetainedScope | Activity#onCreate() | Activity#onDestroy() | | ActivityComponent | @ActivityScoped | Activity#onCreate() | Activity#onDestroy() | | FragmentComponent | @FragmentScoped | Fragment#onAttach() | Fragment#onDestroy() | | ViewComponent | @ViewScoped | View#super() | View#destroyed() | | ViewWithFragmentComponent | @ViewScoped | View#super() | View#destroyed() |

默认情况下,所有的绑定都是无作用域,也就是说,每次绑定时,都会创建一个新的绑定实例;
但是,Dagger 允许绑定作用域到特定组件,如上表所示,在指定组件范围内,实例都只会创建一次,并且对该绑定的所有请求都将共享同一实例。

例如:

  1. @Singletion
  2. class TestCompenent @Inject constructor()

其中@Singleton 就代表 TestComponent 实例在整个app中是唯一的,当后续某个类想要注入其时,将共享这个实例。
————————————————
版权声明:本文为CSDN博主「petterp」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/petterp/article/details/106771203

非 Android 组件自动注入方式

1、创建 module

  1. @Module
  2. @InstallIn(SingletonComponent.class)
  3. public class ApiModule {
  4. ...
  5. @Singleton
  6. @Provides
  7. public ApiService provideApiService(Retrofit retrofit) {
  8. return retrofit.create(ApiService.class);
  9. }
  10. ...
  11. }

2、创建 Interface

  1. @EntryPoint
  2. @InstallIn(SingletonComponent::class)
  3. interface CustomEntryPoint {
  4. fun getApiService(): ApiService
  5. fun getUserInfo(): UserInfo
  6. }

3、注入

  1. val mApiService: ApiService by lazy {
  2. EntryPointAccessors.fromApplication(BaseApplication.getInstance(), CustomEntryPoint::class.java).getApiService()
  3. }