依赖注入的三种形式
- 以参数的形式传入构造器。
- 从其他地方抓取。比如Android中的Context.getSystemService()
- 依赖注入。
使用hit的步骤
- 在项目根级的build.gradle中加入依赖
buildscript {
dependencies {
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
}
}
在app/build.gradle文件中添加依赖
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
dependencies {
implementation "com.google.dagger:hilt-android:2.28-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
}
//开启java8支持。
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
-
所有使用 Hilt 的应用都必须包含一个带有
@HiltAndroidApp注释的Application类。@HiltAndroidApp class ExampleApplication : Application() { ... } -
将依赖注入Android中的类。
使用
@AndroidEntryPoint注释的其他 Android 类提供依赖项@AndroidEntryPoint class ExampleActivity : AppCompatActivity() { ... }android 中支持一下这几种类型
Application(通过使用@HiltAndroidApp)ActivityFragmentViewServiceBroadcastReceiver
值得注意的是
- Hilt 仅支持扩展
ComponentActivity的 Activity,如AppCompatActivity。 - Hilt 仅支持扩展
androidx.Fragment的 Fragment。
@AndroidEntryPoint class ExampleActivity : AppCompatActivity() { @Inject lateinit var analytics: AnalyticsAdapter ... } -
构造器注入
class AnalyticsAdapter @Inject constructor( private val service: AnalyticsService ) { ... } -
Hit模块
Hilt 模块是一个带有
@Module注释的类使用@Binds 注入接口实例
interface AnalyticsService { fun analyticsMethods() } class AnalyticsServiceImpl @Inject constructor( ) : AnalyticsService { ... } @Module @InstallIn(ActivityComponent::class) abstract class AnalyticsModule { @Binds abstract fun bindAnalyticsService( analyticsServiceImpl: AnalyticsServiceImpl ): AnalyticsService }使用@Provides注入实例
@Module @InstallIn(ActivityComponent::class) object AnalyticsModule { @Provides fun provideAnalyticsService( // Potential dependencies of this type ): AnalyticsService { return Retrofit.Builder() .baseUrl("https://example.com") .build() .create(AnalyticsService::class.java) } }同一类型提供多个绑定
定义注解
@Qualifier @Retention(AnnotationRetention.BINARY) annotation class AuthInterceptorOkHttpClient @Qualifier @Retention(AnnotationRetention.BINARY) annotation class OtherInterceptorOkHttpClient定义模块
@Module @InstallIn(ApplicationComponent::class) object NetworkModule { @AuthInterceptorOkHttpClient @Provides fun provideAuthInterceptorOkHttpClient( authInterceptor: AuthInterceptor ): OkHttpClient { return OkHttpClient.Builder() .addInterceptor(authInterceptor) .build() } @OtherInterceptorOkHttpClient @Provides fun provideOtherInterceptorOkHttpClient( otherInterceptor: OtherInterceptor ): OkHttpClient { return OkHttpClient.Builder() .addInterceptor(otherInterceptor) .build() } } // As a dependency of a constructor-injected class. class ExampleServiceImpl @Inject constructor( @AuthInterceptorOkHttpClient private val okHttpClient: OkHttpClient ) : ... // At field injection. @AndroidEntryPoint class ExampleActivity: AppCompatActivity() { @AuthInterceptorOkHttpClient @Inject lateinit var okHttpClient: OkHttpClient }android中预定义的限定符
Activity 的 Context 类@ApplicationContext和@ActivityContext限定符。hit有如下组件,并且会管理生命周期。
ApplicationComponentApplicationActivityRetainedComponentViewModelActivityComponentActivityFragmentComponentFragmentViewComponentViewViewWithFragmentComponent带有 @WithFragmentBindings注释的ViewServiceComponentService -
组件作用域
默认情况下,Hilt 中的所有绑定都未限定作用域。这意味着,每当应用请求绑定时,Hilt 都会创建所需类型的一个新实例。
-
组件层次结构
-
组件默认绑定