1、build.gradle
添加 plugin
buildscript {
...
dependencies {
...
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40'
}
}
2、app/build.gradle
添加配置
plugins {
...
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
android {
...
}
dependencies {
...
implementation "com.google.dagger:hilt-android:2.40"
kapt "com.google.dagger:hilt-compiler:2.40"
}
3、application 添加 @HiltAndroidApp
所有使用 Hilt 的应用程序都必须在 application 加上 @HiltAndroidApp
@HiltAndroidApp
触发 Hilt 的代码生成,包括用作应用程序级依赖项容器的应用程序的基类。
@HiltAndroidApp
class ExampleApplication : Application() { ... }
4、将依赖注入 Android 类
@AndroidEntryPoint
class ExampleActivity : AppCompatActivity() { ... }
5、提供可通过注解注入的类
5.1、@Inject
一般用于简单类注入
class Bar @Inject constructor(){
var str: String? = "hello"
}
class K2Vm @Inject constructor(var bar: Bar? = null) {
...
}
@AndroidEntryPoint
class K2Activity : AppCompatActivity() {
// 直接注入
@Inject lateinit var bar: Bar
// 间接注入
@Inject lateinit var vm: K2Vm
}
@AndroidEntryPoint
是注入入口,可通过此类对对象进行注入
5.2、@module
一般用于第三方或繁琐对象注入
@Module
@InstallIn(ActivityComponent::class)
class AppModule {
@Provides
fun provideBar(): Bar {
return Bar().apply { str = "hello" }
}
}
@AndroidEntryPoint
class K2Activity : AppCompatActivity() {
// 直接注入
@Inject lateinit var bar: Bar
// 间接注入
@Inject lateinit var vm: K2Vm
}
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 允许绑定作用域到特定组件,如上表所示,在指定组件范围内,实例都只会创建一次,并且对该绑定的所有请求都将共享同一实例。
例如:
@Singletion
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
@Module
@InstallIn(SingletonComponent.class)
public class ApiModule {
...
@Singleton
@Provides
public ApiService provideApiService(Retrofit retrofit) {
return retrofit.create(ApiService.class);
}
...
}
2、创建 Interface
@EntryPoint
@InstallIn(SingletonComponent::class)
interface CustomEntryPoint {
fun getApiService(): ApiService
fun getUserInfo(): UserInfo
}
3、注入
val mApiService: ApiService by lazy {
EntryPointAccessors.fromApplication(BaseApplication.getInstance(), CustomEntryPoint::class.java).getApiService()
}