Hilt is a library for Android applications that helps you reduce boilerplate code in your project by automatically handling the creation and injection of dependencies, making your code more modular and easier to maintain.
In this tutorial, we will configure your project to use Hilt for dependency injection,
with the goal of providing your ViewModels with an instance of the BluetoothRepository class.
Begin by adding the hilt-android-gradle-plugin plugin to your project-level build.gradle file:
plugins {
//...
id("com.google.dagger.hilt.android") version "2.51.1" apply false
}Afterwards, add the following dependecies to your app-level build.gradle file:
plugins {
//...
id("kotlin-kapt")
id("com.google.dagger.hilt.android")
}
android {
//...
}
dependencies {
//...
implementation(libs.hilt.android)
implementation(libs.androidx.hilt.navigation.compose)
kapt("com.google.dagger:hilt-android-compiler:2.51.1")
}
// Allow references to generated code
kapt {
correctErrorTypes = true
}Hilt requires Java 8 features,
so ensure that you have set both sourceCompatibility and targetCompatibility to at least Java 8 in your app-level build.gradle file.
Hilt requires that your project contains an Application class annotated with @HiltAndroidApp.
It tells Hilt to generate the necessary code to manage and inject dependencies into Android components throughout your app.
In the following code, I've created an Application class and arbitrarily named it MainApplication:
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp
class MainApplication : Application() {
}Place this class file in the root package of your app (typically in src/main/java/com/*YOUR_APP*/).
Hilt provides dependencies to Android classes annotated with @AndroidEntryPoint.
Be sure to add this annotation to your activity as shown in the following example for MainActivity:
//...
import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint // Add this annotation
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
//...
}
}
}Make sure to add the android:name=".MainApplication" entry inside the tag in your AndroidManifest.xml file to register your Application class:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:name=".MainApplication" <!-- Add this line -->
<!-- ... -->
</application>
</manifest>Now that everything is set up, you can begin injecting the BluetoothRepository into your ViewModel.
- Extend ViewModel: Ensure that your custom
ViewModelclass extendsViewModel. - Add Annotations:
- Add the
@HiltViewModelannotation to yourViewModelclass. - Add the
@Injectannotation to the constructor of theViewModelto allow Hilt to inject dependencies.
- Add the
- Inject the Repository: Add the
BluetoothRepositoryas a parameter in the constructor of theViewModel.
Here’s the full code for my custom MainViewModel class:
@HiltViewModel
class MainViewModel @Inject constructor(
private val bluetoothRepository: BluetoothRepository
) : ViewModel() {
// Your code goes here
}You can also provide your composable functions access to the ViewModel through dependency injection.
Simply add the following parameter to your function:
@Composable
fun MainScreen(
viewModel: MainViewModel = hiltViewModel(),
//...
) {
// Your code goes here
}If you have followed all the steps correctly, your dependency injection should now be set up and working.
You can now return to the main tutorial to continue with the BluetoothRepository class.