Hilt another DI?

Meet Hilt - Dependency Injection (DI) in JetPack, but that's not true as Hilt is just a wrapper around Dagger2. For small projects it will be able to stand up as a more convenient tool and integrates well with the rest of the products in JetPack.



I will not describe how to add to the project, everything is well described in the article



What for?



Dagger2 is a rather complex DI, you can easily get confused what and where you put it. Coming up with an infinite number of components and increasing the number of abstractions. And the larger the project, the more confusion increases.



And if you want to use Dagger2, but with minimal effort, then Hilt was invented for this.



What was simplified for us:



  • Ready-made components (from the names it is clear what they refer to)

    • ApplicationComponent
    • ActivityRetainedComponent
    • ActivityComponent
    • FragmentComponent
    • ViewComponent
    • ViewWithFragmentComponent
    • ServiceComponent
  • In the module you specify which component to add
  • Through @AndroidEntryPoint Hilt, the compiler generates the entire bolierplate for component creation and storage (for example, ActivityRetainedComponent will save the entity after screen rotation, ActivityComponent will re-create).


This code looks pretty elegant (the entire boilerplate will be generated for us)



@AndroidEntryPoint
class ExampleActivity : AppCompatActivity() { 
    @Inject lateinit var testService: TestService
}


Features:



Application is required



You need to declare Application and mark @HiltAndroidApp, without it Hilt will not work.



@HiltAndroidApp
class App : Application() { }


Hierarchical dependency



Hilt , Activity @AndroidEntryPoint



View @WithFragmentBindings Fragment @AndroidEntryPoint, Activity Fragment





Dagger2, , @InstallIn. , .



@InstallIn(ApplicationComponent::class)
@Module
class NetworkModule {
    @Singleton
    @Provides
    fun provideHttpService(): HttpService {
        return object : HttpService {
            init {
                Log.e("Tester", "HttpService initialized")
            }
            override fun request() {
                Log.e("Tester", "HttpService::request")
            }
        }
    }
}


Hilt, @InstallIn, , .



Component Subcomponent



, Dagger2, Hilt . :



DaggerLoginComponent.builder()
        .context(this)
        .appDependencies(
          EntryPointsAccessors.fromApplication(
            applicationContext,
            LoginModuleDependencies::class.java
          )
        )
        .build()
        .inject(this)




, . ( ), Dagger2.



@AndroidEntryPoint



  • Activity ComponentActivity AppCompatActivity
  • Fragment androidx.Fragment
  • Retain




Hilt :



  • Dagger Component-
  • Application, Activity, Fragment, View ., @AndroidEntryPoint
  • Dagger


ActivityRetainedComponent



ViewModel arch :



this.viewModelProvider =
        new ViewModelProvider(
            activity,
            new ViewModelProvider.Factory() {
              @NonNull
              @Override
              @SuppressWarnings("unchecked")
              public <T extends ViewModel> T create(@NonNull Class<T> aClass) {
                ActivityRetainedComponent component =
                    ((GeneratedComponentManager<LifecycleComponentBuilderEntryPoint>)
                            activity.getApplication())
                        .generatedComponent()
                        .retainedComponentBuilder()
                        .build();
                return (T) new ActivityRetainedComponentViewModel(component);
              }
            });




:



  • Dagger2
  • ( )
  • boilerpate .
  • Dagger2 ( , ..)


:



  • , , Dagger2
  • ,
  • Dagger2
  • , , Fragment Activity c @AndroidEntryPoint


:






All Articles