The new Hilt library is helping us to apply the _Dependency inversion principle _in the code, and structure it in a layered way. It defines a standard to do Dependency Injection by providing containers for every Android class in the project.

Hilt is created on top of the Android DI library Dagger, so it enjoys the scalability, runtime performance, and all the stuff that Dagger provides. It is also including managing lifecycles in the application automatically. Hilt library integrates Jetpack and Android framework classes. It removes most of the boilerplate code because the developer has bigger fish to fry - like defining and injecting bindings, but without managing the Dagger setup. Keep in mind that Hilt is still in alpha!

Note:Here I’ll focus on converting the **dagger-android **library to Hilt.


Our starting point will be build.gradle(:app) file - so, go back to the drawing board:

//Hilt
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"

// Hilt ViewModel extension
    def hilt_jetpack_version = "1.0.0-alpha01"
    implementation "androidx.hilt:hilt-lifecycle- viewmodel:$hilt_jetpack_version"
    kapt "androidx.hilt:hilt-compiler:$hilt_jetpack_version"

// Hilt testing dependencies
    androidTestImplementation "com.google.dagger:hilt-android-testing:$hilt_version"
    kaptAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version"
    kaptAndroidTest "androidx.hilt:hilt-compiler:$hilt_jetpack_version"

#dagger-android #android #kotlin #dagger-hilt #dagger

How to Convert Dagger-Android to Hilt
3.45 GEEK