• 第一步,需要在build.gradle中配置Hilt的插件路径:

      1. buildscript {
      2. ...
      3. dependencies {
      4. ...
      5. classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
      6. }
      7. }
    • 第二步,在app/build.gradle中添加Hilt依赖库与引入插件

      1. plugins{
      2. id:'kotlin-kapt',
      3. id:'dagger.hilt.android.plugin'
      4. }
      5. ...
      6. dependencies {
      7. implementation "com.google.dagger:hilt-android:2.28-alpha"
      8. kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
      9. }
    • 必须自定义Application,否则Hilt将无法正常工作 ```kotlin @HiltAndroidApp class MyApplication : Application() { }

    1. ```kotlin
    2. <application
    3. android:name=".MyApplication"
    4. ...>
    5. </application>
    • Hilt一共支持6个入口点,分别是
      • Application
      • Activity
      • Fragment
      • View
      • Service
      • BroadcastReceiver
    • Hilt中的InstallIn支持7中组件类型
      • @ApplicationComponent:Application
      • @ActivityRetainedComponent:ViewModel
      • @ActivityComponent:Activity
      • @FragmentComponent:Fragment
      • @ViewComponent:View
      • @ViewWithFragmentComponent:View annotated with @WithFragmentBindings
      • @ServiceComponent:@Service

    只有Application这个入口点是使用@HiltAndroidApp注解来声明的,其余全部都是@AndroidEntryPoint