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

  1. 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
  1. All applications that use Hilt must include an Application class annotated with @hiltAndroidApp.

    @HiltAndroidApp
    class ExampleApplication : Application() {... }Copy the code
  2. 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 extensionsandroidx.FragmentThe fragments.
    @AndroidEntryPoint
    class ExampleActivity : AppCompatActivity(a){
    
      @Inject lateinit var analytics: AnalyticsAdapter
      ...
    }
    Copy the code
  3. Constructor injection

    class AnalyticsAdapter @Inject constructor(
      private val service: AnalyticsService
    ) { ... }
    Copy the code
  4. 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@WithFragmentBindingsThe annotationsView
    ServiceComponent Service
  5. 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.

  6. Component hierarchy

  7. Component Default binding