Using Hilt to Inject Huawei Mobile Services
Hello everyone,
In this article, I will talk about how we can use together Hilt and Huawei Mobile Services.
Dependency Injection is very important for the applications. There are different ways to do this in Android applications. The most popular of these options is undoubtedly Dagger. As you know, a new announcement has been made in the recent past: Hilt.
What is Hilt?
Yeah, Dagger was very powerful, but its use was a little complicated. Although Hilt is still in the beta version, the usage rate is increasing.
Hilt provides a standard way to incorporate Dagger dependency injection into an Android application.
To use Hilt, you must add @HiltAndroidApp annotation to your application’s Application class. This is because Hilt needs to initiate the code generation process for your application.
We were doing a similar thing on Dagger, but we can only do it here with Hilt and annotation.
Now, We have to let Hilt inject in the app’s classroom. We can do this with @AndroidEntryPoint annotation.
This annotation now supports the Activity, Fragment, Service, View, and BroadcastReceiver classes.
You can also use Hilt with ViewModel and WorkManager, one of the other Jetpack members. For example, to use in ViewModel, it is sufficient to fill the constructor with the @ViewModelInject annotation. For example;
Okay, so far we’ve briefly touched on Hilt. Now, let’s take a look at how we can use Hilt and HMS together.
If you want to learn integration HMS check this post. After this point, first I want to try something on Analytics Kit for example. To use Analytics Kit SDK, we need to initialize it.
When using Hilt, we need module classes as in Dagger. We can provide dependencies with these classes. We can define that the class is a module with @Module annotation.
Now let’s create a module for Analytics.
I marked this as a module with @Module. With @InstallIn we define the component to be used. The ActivityComponent::class here is created by Hilt. I created a function to provide the code that we initialized the Analytics Kit. And I marked it with @Provide to provide. Our code needs a context. Here we can take advantage of the @ApplicationContext provided by Hilt.
I can use this function we provide by filling the constructor of the repository with the help of @Inject.
Now we can call this through ViewModel. As we mentioned before, we will make use of Hilt’s @ViewModelInject annotation.
It’s a pretty simple case. Of course, we can further elaborate on these methods. Let’s try something with the Awareness Kit. Let’s use the screen status detection feature of the Awareness Kit. To realize this, we first need the Awareness Kit’s getCaptureClient() function. I created an AwarenessModule file to provide this. Since the function requires context, I use the context provided by Hilt.
Then I call the screen status function in the repository.
After returning the screen status I called, I listen to the results from the viewModel.
Tips & Tricks
When using the features of Huawei mobile services, if you set up the functions provided by the library, especially the parts that you can use in multiple places in the application, on the module, you may reduce your development cost in any update :)
Conclusion
In this article, while answering questions such as what is Hilt, how can we use it, we have also looked at the questions of how to set up HMS with Hilt.
References
If you want to review the whole project, you can check out the repo below. I’m going to keep developing this repo. The Analytics Kit, Awareness Kit, and Auth Service versions I’m talking about here are simply implemented. I’ll continue to write new articles after updates in the application :)
If you have any questions, don’t forget to visit the Huawei Forum!
Thanks for reading. Don’t forget the clap if you like this blog :)