Hilt, Jetpack’s recommended Dependency injection (DI) solution for Android applications, is now stable. This means Hilt is fully ready for use in production environments. Hilt is much more convenient than Dagger and also helps you reduce template code, it is built for Android and integrates multiple Jetpack dependencies. Many companies already use Hilt in their applications and benefit from it.
Hilt was first previewed in June 2020 with the mission to define a standard solution for Android dependency injection, and since then we’ve received a tremendous amount of feedback from developers. This feedback not only improved Hilt, but made it clear that we were on the right track.
Instead of manually creating dependency diagrams or manually injecting and passing types, Hilt automatically generates the required code from annotations at compile time. Hilt helps you get the most out of DI’s best practices by implementing the hard parts of the work and generating all the template code instead of writing it manually. In addition, Hilt is fully integrated with Android to help you automatically manage the lifecycle of dependency diagrams for Android Framework classes.
Let’s look at Hilt’s behavior with a simple example! After configuring Hilt, injecting a ViewModel into an Activity from scratch in your project is as easy as adding annotations to your code, as shown below:
Hilt class MyApplication: Application() {... } // Make Hilt recognize the viewModel@hiltViewModel class loginViewModel@inject constructor(private val savedStateHandle: SavedStateHandle, / *... Other dependencies that Hilt is concerned about... */ ) : ViewModel() { ... } // Make the Activity use the correct ViewModel factory and inject other dependencies @androidEntryPoint class LoginActivity: AppCompatActivity() { private val loginViewModel: LoginViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState) // loginViewModel already available}}Copy the code
In addition to the above, what other reasons would you choose to use Hilt in your application?
So much easier than Dagger
Hilt is built on the popular DI library Dagger, so it can benefit from compile-time verification provided by Dagger, good runtime performance, extensibility, and Android Studio support. Some Dagger annotations are also commonly used for Hilt, such as @inject (which tells how Dagger/Hilt provides an instance of a type). But Hilt is much more convenient than Dagger.
I highly recommend using Dagger for dependency injection in Android applications, however using Dagger alone can lead to excessive memory usage at creation time. When used in conjunction with the complex, life-cycle aware components of Android development, many pitfalls can emerge, such as memory leaks: dependencies scoped for activities are accidentally passed into the ViewModel. Hilt, tailored for Android, helps you avoid some of the pitfalls of the Dagger basic use.
— Marcelo Hernandez, Tinder senior software engineer
If you already have a Dagger in your application and want to migrate to Hilt, don’t worry! Dagger and Hilt can coexist and applications can be migrated as needed.
Less template code
Hilt is customized — meaning that it makes some decisions for you in order to save you from writing code. Hilt defines standard components and dependency diagrams, and fully integrates Android Framework classes such as Application, Activity, Fragment, and View. It also defines scoped annotations that limit the scope of type instances to those components.
Hilt can automatically generate test applications and test components with the @HiltAndroidTest annotation. After migrating to Hilt, we were able to remove 20-40% of our test-related template code.
— Jusun Lee, YouTube software engineer
We have only scratched the surface on Hilt migration. However, in one of the modules that migrated to Hilt, we saw a +72/-182 change in lines of code.
— Marcelo Hernandez, Tinder senior software engineer
Tailored for Android
Unlike Dagger, the dependency injection solution for Java programming language applications, Hilt only supports Android applications. Some annotations are for Hilt’s use and define a proprietary Android DI approach. These annotations include @HiltAndroidApp, @AndroidEntryPoint, and @HiltViewModel.
Finally, Hilt provides a built-in Dagger component that recognizes the Android lifecycle. With Hilt, we can just focus on the Dagger @modules and not worry about the component, subcomponent, and component provider schema, etc.
— Marcelo Hernandez, Tinder senior software engineer
Standardization of components and bindings
Instead of recognizing the Dagger, Hilt uses a single-component system to simplify the dependency diagram, resulting in less code generated at compile time.
With Hilt’s single-component system, binding definitions are provided once and can be shared across all classes that use the component. This is a big step up from the days when YouTube used a multi-component system where modules had to be manually connected to custom components and there were a lot of repetitive binding definitions.
— Jusun Lee, YouTube software engineer
Since our Gradle module separation allows for isolation of development functionality, we tend to be too flexible with our Dagger. We found that migrating these modules to Hilt exposed an unintended separation of concerns violation.
— Marcelo Hernandez, Tinder senior software development engineer
Integrate with other Jetpack libraries
You can use your favorite Jetpack library right out of the box. So far, we have direct injection support for ViewModel, WorkManager, Navigation, and Compose.
See the documentation to learn more about Jetpack’s support.
I am very grateful for the way Hilt is used out of the box with ViewModel and the way it eliminates the viewModel.factory template code that must be set if you simply use Dagger.
— Marcelo Hernandez, Tinder senior software development engineer
Hilt Learning Resources
Hilt is Jetpack’s recommended DI solution for Android apps. To learn more and start using it in your application, see the following resources:
-
Understand the benefits of using dependency injection
-
Know how to use Hilt in your application
-
Dagger to Hilt migration guide
-
Step by step learning Hilt in Codelabs:
- Use Hilt in Android applications
- Migrate the Dagger application to Hilt
-
Code examples:
- Google I/O 2020 applications
- Sunflower application
-
A memo on the difference and usage of Hilt and Dagger annotations