Categories
How To Studio

Hilt is stable! Easier dependency injection on Android

Manuel Alive

Handle, Jetpack recommended Dependency injection (DI) Android app solution already available stable!!That is, Hilt is fully ready to use In production.. Hilt is simpler than Dagger, has less boilerplate code, is designed for Android, and integrates with multiple Jetpack libraries.Some companies are already starting to use Hilt in their apps

Hilt First released As an alpha version in June 2020 Standard method Since then we have received a lot of feedback from developers to do DI on your Android app. Not only did this improve the library, but it also showed that it was addressing the right issues.

Hilt uses annotations at compile time to automatically generate all the code instead of manually creating the dependency graph and manually inserting and passing the types as needed.The handle can help you Make the most of DI best practices By doing hard work in your app Generate all the fixed phrases Otherwise I would have had to write.And because it’s fully integrated with Android, Hilt manages the lifecycle of dependency graphs associated with Android framework classes. Automatically for you.

Let’s see how Hilt works with a simple example!rear Hilt-up settingsUsing it from scratch in your project, inserting a ViewModel into your activity is as easy as adding some annotations to your code like this:

Why do I need to use Hilt in my Android app apart from the above?

Hilt is built on top of the popular DI library dagger Therefore, compile-time accuracy, run-time performance, scalability, and Android Studio support Provided by Dagger. Some Dagger annotations, such as @Inject, that tell Dagger and Hilt how to provide an instance of a type are commonly used in Hilt. But Hilt is simpler than Dagger!

“We strongly recommend using Dagger for dependency injection in Android applications. However, pure vanilla daggers can be too creative. Various that are part of Android development. Mixing with the complexity of lifecycle-aware components, there’s plenty of room for pitfalls such as memory leaks. For example, you mistakenly pass activity-scoped dependencies to ViewModel. Hilt, designed specifically for Android, gives vanilla daggers. It helps to avoid the pitfalls of using it. “ — — Marcelo Hernandez Placeholder Image, Staff Software Engineer, The Tinder

If your app already uses Dagger and wants to move to Hilt, don’t be afraid.Dagger and Hilt can coexist, the app Migrated If necessary.

Hilt is controversial — this means making decisions to write less code. Hilt defines standard components or dependency graphs that are fully integrated with Android framework classes such as applications, activities, fragments, and views. It also contains scope annotations to scope instances of the type to those components.

“Hilt automatically generates test applications and test components via @HiltAndroidTest. After migrating to Hilt, we were able to remove 20% to 40% of the wire-up test code on the boiler plate!” — YouTube, software engineer, Jusun Lee

“We’re just scratching the surface in terms of migrating to Hilt, but we see +78 / -182 in that one of the modules that migrated to Hilt changed the lines in this library. Was done. “ — Tinder, Staff Software Engineer, Marcelo Hernandez

In contrast to Dagger, a dependency injection solution for Java programming language applications, Hilt works only with Android apps.Some annotations such as

@HiltAndroidApp, @AndroidEntryPoint, Or @HiltViewModel Hilt-specific and defines an opinionative way to perform DI on Android.

“Hilt has finally incorporated a Dagger component for the Android lifecycle. With Hilt, you can focus on Dagger @ Modules without worrying about components, subcomponents, component provider patterns, etc.” — Tinder, Staff Software Engineer, Marcelo Hernandez

For those who know about Dagger, Hilt uses to simplify the dependency graph. Monolithic component system Reduces the code generated at compile time.

“In Hilt’s monolithic component system, the binding definition is provided once and shared by all classes that use that component. This is as big a win as before. YouTube allows modules to be manually wired to custom components. I was using a polylithic component system that had many duplicate binding definitions. “ — YouTube, software engineer, Jusun Lee

“Separating Gradle modules makes it easier to be too creative when using Dagger. Migrating these modules to Hilt inadvertently violates separation of concerns. It turns out that the flaws I was doing were actually revealed. “ — Tinder, Staff Software Engineer, Marcelo Hernandez

With Hilt, you can start using your favorite Jetpack library right away.Provides direct injection support ViewModel, WorkManager, Navigation, and Compose So far.

For more information on Jetpack support document..

“I’m really grateful for how Hilt works out of the box in ViewModels and how to eliminate the boilerplate that requires setting up the ViewModel.Factory provider in vanilla daggers.” — Tinder, Staff Software Engineer, Marcelo Hernandez

Hilt is Jetpack’s recommended DI solution for Android apps. To find out more and get started with your app, check out the following resources:



Source

Leave a Reply

Your email address will not be published. Required fields are marked *