Dependency injection (DI) is a widely used technology in programming and is suitable for programming Android where dependencies are provided for the class instead of creating them. By following the DI principles, we create a good foundation for our application with the ability to reuse large code and easily test. Have you ever tried manually injecting dependency code? Even with many dependency injection libraries available today, it requires a lot of code as the project gets bigger, because we have to build every class and its dependencies manually, and create containers to Use and management dependencies
The Hilt library defines a standard for implementing DI in your application by providing containers in every Android class in your project and automatically managing their lifecycle for you.
Hilt is built based on a famous DI library, Dagger, so it inherits the exact compile time, runtime performance, scalability and Android Studio support that Dagger provides. Because of this Dagger accounts for 74% of the top 10k apps in Google Play. However, due to compile-time code generation, the application build time increases
Because many android framework classes are created by the operating system itself. There should be a related summary when using Dagger in Android apps. Unlike Dagger, Hilt is integrated with Jetpack libraries and framework classes and eliminates most of the templates to allow you to focus on the critical parts of identifying and managing dependencies without having to worry about managing all setups and bridges of Dagger. It will automatically create and deliver
- Components to integrate Android framework class with Dagger that you need to create manually
- Scope annotations for the components are automatically created by Hilt
- Predefined bindings and qualifiers.
Because Dagger and Hilt can coexist, we can easily migrate when needed
Hint in action
In this example, I will show you how easy Hilt is to use by injecting AnalyticsAdapter into MainActivity.
First, enable Hilt in your application with the HiltAndroidApp annotation that activates Hilt gen code
1 2 3 | <span class="token annotation builtin">@HiltAndroidApp</span> <span class="token keyword">class</span> MyApplication <span class="token operator">:</span> <span class="token function">Application</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">..</span> <span class="token punctuation">.</span> <span class="token punctuation">}</span> |
Second, tell Hilt how to provide the instance of AnalyticsAdapter by annotating with the constructor with Inject
1 2 | <span class="token keyword">class</span> AnalyticsAdapter <span class="token annotation builtin">@Inject</span> <span class="token keyword">constructor</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">..</span> <span class="token punctuation">.</span> <span class="token punctuation">}</span> |
To inject the AnalyticsAdapter instance into MainActivity, enable Hilt in the activity with the AndroidEntryPoint annotation and perform the injection using the Inject annotation.
1 2 3 4 5 6 7 8 9 10 | <span class="token annotation builtin">@AndroidEntryPoint</span> <span class="token keyword">class</span> MainActivity <span class="token operator">:</span> <span class="token function">AppCompatActivity</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token annotation builtin">@Inject</span> <span class="token keyword">lateinit</span> <span class="token keyword">var</span> analytics <span class="token operator">:</span> AnalyticsAdapter <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">onCreate</span> <span class="token punctuation">(</span> savedInstanceState <span class="token operator">:</span> Bundle <span class="token operator">?</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span> <span class="token punctuation">.</span> <span class="token function">onCreate</span> <span class="token punctuation">(</span> savedInstanceState <span class="token punctuation">)</span> <span class="token comment">// analytics instance has been populated by Hilt</span> <span class="token comment">// and it's ready to be used</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
Hilt with Jetpack support
You can use your favorite Jetpack libraries with Hilt. Hilt has provided direct injection support for ViewModel and WorkManager
For example, to inject LoginViewModel into LoginActivity: annotate LoginViewModel with ViewModelnject and use it in the desired activity and fragment.
1 2 3 4 5 6 7 8 9 10 11 12 | <span class="token keyword">class</span> LoginViewModel <span class="token annotation builtin">@ViewModelInject</span> <span class="token keyword">constructor</span> <span class="token punctuation">(</span> <span class="token keyword">private</span> <span class="token keyword">val</span> analyticsAdapter <span class="token operator">:</span> AnalyticsAdapter <span class="token punctuation">)</span> <span class="token operator">:</span> ViewModel <span class="token punctuation">{</span> <span class="token operator">..</span> <span class="token punctuation">.</span> <span class="token punctuation">}</span> <span class="token annotation builtin">@AndroidEntryPoint</span> <span class="token keyword">class</span> LoginActivity <span class="token operator">:</span> <span class="token function">AppCompatActivity</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token keyword">val</span> loginViewModel <span class="token operator">:</span> LoginViewModel <span class="token keyword">by</span> <span class="token function">viewModels</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">onCreate</span> <span class="token punctuation">(</span> savedInstanceState <span class="token operator">:</span> Bundle <span class="token operator">?</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span> <span class="token punctuation">.</span> <span class="token function">onCreate</span> <span class="token punctuation">(</span> savedInstanceState <span class="token punctuation">)</span> <span class="token comment">// loginViewModel is ready to be used</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
Start using Hilt
To learn about hilt in Android app you can read this doc
If you’re new to DI or Dagger, check out this guide to add Hilt to Android apps
If you already use Dagger, you can migrate to Hilt with this doc
This is a project using Hilt: https://github.com/android/architecture-samples/tree/dev-hilt
And here are some annotations in Hilt
References: https://medium.com/androiddevelopers/dependency-injection-on-android-with-hilt-67b6031e62d