Three forms of dependency injection
- Pass it to the constructor as a parameter.
- Grab it from somewhere else. For example, context.getSystemService () in Android
- Dependency injection.
Steps to use hit
- Add dependencies to build.gradle at the root of your project
buildscript {
dependencies {
classpath 'com. Google. Dagger hilt - android - gradle - plugin: 2.28 alpha'}}Copy the code
Add dependencies to app/build.gradle file
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 - the compiler: 2.28 alpha." "
}
// enable java8 support.
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Copy the code
-
All applications that use Hilt must include an Application class annotated with @hiltAndroidApp.
@HiltAndroidApp class ExampleApplication : Application() {... }Copy the code
-
Inject dependencies into classes in Android.
Other Android classes that use the @AndroidEntryPoint annotation provide dependencies
@AndroidEntryPoint class ExampleActivity : AppCompatActivity() {... }Copy the code
These types are supported in Android
Application
(By using@HiltAndroidApp
)Activity
Fragment
View
Service
BroadcastReceiver
It’s worth noting
- Hilt only supports activities that extend ComponentActivity, such as AppCompatActivity.
- Hilt supports only extensions
androidx.Fragment
The fragments.
@AndroidEntryPoint class ExampleActivity : AppCompatActivity(a){ @Inject lateinit var analytics: AnalyticsAdapter ... } Copy the code
-
Constructor injection
class AnalyticsAdapter @Inject constructor( private val service: AnalyticsService ) { ... } Copy the code
-
Hit the module
A Hilt Module is a class annotated with @Module
An example of an adaptive interface is used using @Sharing
interface AnalyticsService { fun analyticsMethods(a) } class AnalyticsServiceImpl @Inject constructor( ) : AnalyticsService { ... } @Module @InstallIn(ActivityComponent::class) abstract class AnalyticsModule { @Binds abstract fun bindAnalyticsService( analyticsServiceImpl: AnalyticsServiceImpl ): AnalyticsService } Copy the code
Inject the instance using @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) } } Copy the code
Multiple bindings are provided for the same type
Custom annotation
@Qualifier @Retention(AnnotationRetention.BINARY) annotation class AuthInterceptorOkHttpClient @Qualifier @Retention(AnnotationRetention.BINARY) annotation class OtherInterceptorOkHttpClient Copy the code
Definition module
@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 } Copy the code
A predefined qualifier in Android
The @ApplicationContext and @ActivityContext qualifiers for the ActivityContext class.
Hit has the following components and manages the life cycle.
ApplicationComponent
Application
ActivityRetainedComponent
ViewModel
ActivityComponent
Activity
FragmentComponent
Fragment
ViewComponent
View
ViewWithFragmentComponent
with @WithFragmentBindings
The annotationsView
ServiceComponent
Service
-
Component scope
By default, all bindings in Hilt are unscoped. This means that each time a request binding is applied, Hilt creates a new instance of the required type.
-
Component hierarchy
-
Component Default binding