Mastering Hilt - Android | Kotlin | Hilt (Dagger 2) [ Part 4]
Becoming Proficient in Dependency Injection with Hilt for Android (Kotlin)
![Mastering Hilt - Android | Kotlin | Hilt (Dagger 2) [ Part 4]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1697041036577%2F0587818f-2560-4684-b4a8-a7521eb20fbb.jpeg&w=3840&q=75)
Senior Android Engineer from Bangladesh. Love to contribute in Open-Source. Indie Music Producer.
Search for a command to run...
Becoming Proficient in Dependency Injection with Hilt for Android (Kotlin)
![Mastering Hilt - Android | Kotlin | Hilt (Dagger 2) [ Part 4]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1697041036577%2F0587818f-2560-4684-b4a8-a7521eb20fbb.jpeg&w=3840&q=75)
Senior Android Engineer from Bangladesh. Love to contribute in Open-Source. Indie Music Producer.
No comments yet. Be the first to comment.
This course focuses on mastering Dependency Injection (DI) using Hilt, a widely used library in Android development.
Becoming Proficient in Dependency Injection with Hilt for Android (Kotlin)
A practical guide to choosing the right coroutine primitive for your Android/Kotlin projects

This guide explains what Channels are, when to use them, how to use them correctly, and when NOT to use them — in simple, professional language. 1. What problem do Channels solve? In Kotlin coroutines, you often have multiple coroutines running at t...

Learn how to build preview-safe ViewModels in Jetpack Compose using Hilt.

HandlerInterceptor vs Filter in Spring Boot: A Practical Guide with JNDI Injection Prevention

Simple In-Memory Caching: A Tiny Trick with Massive Impact

Injecting Dependencies into Activities
Injecting Dependencies into Fragments
Injecting Dependencies into ViewModels
Injecting Dependencies into Activities:
To inject dependencies into Activities, you can use the @Inject annotation on the constructor of the Activity. Hilt will automatically provide the required dependencies when the Activity is created.
Example - Injecting a dependency into an Activity:
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var myDependency: MyDependency // Injected dependency
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Now, we can use myDependency in our Activity
// ...
}
}
In this example, the MainActivity is marked with @AndroidEntryPoint, enabling Hilt to perform dependency injection. The MyDependency instance is annotated with @Inject, indicating that it should be provided by Hilt when the MainActivity is created. The myDependency variable is then injected with the provided instance.
Injecting Dependencies into Fragments:
To inject dependencies into Fragments, you can use the @Inject annotation on the constructor of the Fragment. Similar to Activities, Hilt will automatically provide the required dependencies when the Fragment is created.
Example - Injecting a dependency into a Fragment:
@AndroidEntryPoint
class MyFragment : Fragment() {
@Inject
lateinit var myDependency: MyDependency // Injected dependency
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_my, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Now, we can use myDependency in our Fragment
// ...
}
}
In this example, the MyFragment is marked with @AndroidEntryPoint, enabling Hilt to perform dependency injection. The MyDependency instance is annotated with @Inject, indicating that it should be provided by Hilt when the MyFragment is created. The myDependency variable is then injected with the provided instance.
Injecting Dependencies into ViewModels:
To inject dependencies into ViewModels, you can use the @ViewModelInject annotation on the ViewModel's constructor. Hilt will automatically provide the required dependencies when creating the ViewModel.
Example - Injecting a dependency into a ViewModel:
class MyViewModel @ViewModelInject constructor(
private val myDependency: MyDependency // Injected dependency
) : ViewModel() {
// Now, you can use myDependency in your ViewModel
// ...
}
In this example, the MyViewModel is annotated with @ViewModelInject, enabling Hilt to perform dependency injection. The MyDependency instance is declared as a parameter in the ViewModel's constructor and annotated with @ViewModelInject, indicating that it should be provided by Hilt when the ViewModel is created.
By using constructor injection in Hilt, we can create cleaner, more maintainable code and ensure that our dependencies are automatically provided wherever they are needed in our Android app.