Dependency Injection (Android): Implementation with Examples (Hilt) [PART 3]
![Dependency Injection (Android): Implementation with Examples (Hilt) [PART 3]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1664120519989%2FRmi_ewhv0.jpg&w=3840&q=75)
Senior Android Engineer from Bangladesh. Love to contribute in Open-Source. Indie Music Producer.
Search for a command to run...
![Dependency Injection (Android): Implementation with Examples (Hilt) [PART 3]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1664120519989%2FRmi_ewhv0.jpg&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.
In this series, I will write about Android App Development.
Things to know If get to know something new by reading my articles, don't forget to endorse me on LinkedIn Hilt is a wrapper to provides a standard way to incorporate Dagger dependency injection into an Android application. What kind of benefits Hi...
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

If get to know something new by reading my articles, don't forget to endorse me on LinkedIn
Before you get started with the examples, check out PART 1 and PART 2
Add dependencies
Annotate YouApplicationClass with @HiltAndroidApp
Code:
@HiltAndroidApp
MyApplication : Application()
Activity/FragmentTo @Inject any object to Activity or Fragment, we need to annotate Activity or Fragment with @AnroidEntryPoint
Code:
@AndroidEntryPoint
SampleActivity : BaseActivity(){
@Inject
lateinit var somethingObject : SomethingObject
}
ViewModelTo @Inject any object to ViewModel, we need to annotate ViewModel with @HiltViewModel and @Inject the constructor.
Code:
@HiltViewModel
SomethingViewModel @Inject constructor() : BaseViewModel(){
@Inject
lateinit var somethingObject : SomethingObject
}
ViewModel Constructor InjectionTo @Inject objects to ViewModel, we need to annotate ViewModel with @HiltViewModel and @Inject the constructor.
Code:
@HiltViewModel
SomethingViewModel @Inject constructor
(private val somethingObject : SomethingObject)
: BaseViewModel(){...}
Context to any Injectable ClassTo provide ActivityContext to any Injectable Class, @Inject class constructor and annotate context with @ActivityContext and register in ActivityComponent module to provide the Context.
Code:
// Lets see how to inject a custom dialog object to an activity or fragment
// First, create a CustomDialog class and inject the constructor with Context object
class CustomDialog @Inject constructor(@ActivityContext private val context : Context){....}
// Second, Register the CustomDialog to any ActivityComponent module
@Module
@InstallIn(ActivityComponent::class)
object ActivityModule{
@Provide
fun provideCustomDialog(@ActivityContext context : Context) : CustomDialog = CustomDialog(context)
}
// Third, Inject the CustomDialog to any activity or fragment
@AndroidEntryPoint
class Activity/Fragment : BaseActivity/BaseFragment{
@Inject
lateinit var customDialog : CustomDialog
}
Interface or Abstract ClassTo provide an instance of Interface or Abstract Class to any dependent object, we can't directly inject any interface or abstract class becuase there is no way to initialize interface or abstract class. So, we have to provide an Implementation of the interface or abstract class.
Code:
// Define an interface
interface SomethingObject{
fun doSomething()
}
// Define the implementation
class SomethingObjectImpl @Inject constructor() : SomethingObject{
@override
fun doSomething(){...}
}
// Register the SomeObject to any *Component module
@Module
@InstallIn(SingletonComponent/ActivityComponent/ViewModelComponent::class)
object SomethingModule{
@Provide
fun provideSomethingObject() : SomethingObject = SomethingObjectImpl()
}
That's it for this series. Happy Coding...