Kotlin
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Null Safety
-
Section 4: Object-Oriented Kotlin
-
Section 5: Functional Kotlin
-
Section 6: Coroutines
-
Section 7: Collections Deep Dive
-
Section 8: Type System Deep Dive
-
Section 9: Interop and Tooling
-
Section 10: Kotlin DSLs and Patterns
-
Section 11: Testing and Quality
-
Section 12: Server-Side Kotlin
-
Section 13: Practical Projects
-
Section 14: Interview Practice
-
Section 15: More Practice Exercises
-
Section 16: More Standard Library
-
Section 17: Multiplatform Kotlin
-
Section 18: kotlin.collections In Depth
-
Section 19: kotlin.text In Depth
-
Section 20: kotlin.ranges and kotlin.sequences
-
Section 21: kotlin.io and File Handling
-
Section 22: kotlinx.coroutines Deep Dive
-
Section 23: kotlin.reflect
-
Section 24: Android Development with Kotlin Overview
-
Section 25: Kotlin Multiplatform Deep Dive
-
Section 26: Kotlin for Backend Deep Dive
-
Section 27: Kotlin Design Patterns
-
Section 28: Advanced Language Features
-
Section 29: More Practice Exercises
-
Section 30: More Interview Practice
-
Section 31: Kotlin Type System Deep Dive
-
Section 32: Kotlin Null Safety Advanced
-
Section 33: Kotlin Testing Deep Dive
-
Section 34: Kotlin Build Tooling Deep Dive
-
Section 35: Kotlin Serialization
-
Section 36: Kotlin Performance Considerations
-
Section 37: Kotlin Native Overview
-
Section 38: Kotlin for Data and Scripting
-
Section 39: More Coroutines Practice
-
Section 40: More Android-Adjacent Patterns
-
Section 41: More Practical Projects
-
Section 42: More Design and Architecture Practice
-
Section 43: Kotlin Language Evolution
-
Section 44: More Interview and Review
-
Section 45: Kotlin Delegation Patterns Deep Dive
-
Section 46: Kotlin Annotations Deep Dive
-
Section 47: Kotlin for Gradle Plugin Development
-
Section 48: Kotlin Concurrency Beyond Coroutines
-
Section 49: Kotlin Compiler Internals
-
Section 50: Real-World Kotlin Case Studies
-
Section 51: Final Practice Projects
-
Section 52: Kotlin for Server-Side Reactive Programming
-
Section 53: More Practice and Drills
-
Section 54: Kotlin Security Practices
127: Dependency Injection with Dagger/Hilt
Wait, why do I actually need Hilt? Can't I just pass things in the constructor?
You absolutely can. In a tiny project, manual dependency injection is actually cleaner. But once you hit a certain scale, you run into the "dependency nightmare." Imagine your UserRepository needs a UserApi, which needs an OkHttpClient, which needs a Cache and an Interceptor. To create one repository in your Activity, you'd have to instantiate five other objects in the correct order first.
I've spent far too many hours in legacy codebases manually wiring these graphs. It's tedious and error-prone. Hilt basically acts as the "manager" for your objects. Instead of you saying "Create a Cache, then an OkHttp client, then an Api," you just tell Hilt, "I need a UserRepository," and Hilt looks at the constructor, sees what's missing, and goes and finds (or creates) those dependencies for you automatically.
How do I handle classes I didn't write, like Retrofit or Room?
This is where learners usually get stuck. You can't just put @Inject constructor() on a Retrofit class because you didn't write the Retrofit source code. You can't just go into the library and add annotations. For these "third-party" dependencies, we use Hilt Modules.
Think of a Module as a guidebook. You're telling Hilt: "Whenever someone asks for a NewsApi, run this specific function to create it." Here is how I usually set up a network module:
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Provides
@Singleton
fun provideRetrofit(): Retrofit {
return Retrofit.Builder()
.baseUrl("https://api.news.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
}
@Provides
@Singleton
fun provideNewsApi(retrofit: Retrofit): NewsApi {
return retrofit.create(NewsApi::class.java)
}
}
Notice that provideNewsApi takes Retrofit as a parameter. Hilt is smart enough to see that, look at the other function in the same module, and provide the Retrofit instance automatically. It's a chain reaction.
Why is @Inject used in two different ways?
You'll see @Inject on constructors and @Inject on properties. They do very different things. Constructor injection is the gold standard. When you put it on a constructor, you're telling Hilt: "This is how you make this object, and here is what it needs."
class NewsRepository @Inject constructor(
private val api: NewsApi
) {
// Hilt knows exactly how to provide NewsApi because of our Module
}
However, Android is weird. You don't instantiate your own Activity or Fragment; the Android OS does. Because you can't call the constructor yourself, you can't use constructor injection. That's where "Field Injection" comes in. You mark a variable with @Inject, and Hilt "plugs" the dependency in after the object is created.
@AndroidEntryPoint
class NewsActivity : AppCompatActivity() {
@Inject lateinit var repository: NewsRepository
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// By this point, Hilt has already assigned the repository
}
}
Just a word of advice: avoid field injection whenever possible. Use it for Android components, but for your own internal logic classes, always stick to constructor injection. It makes your code much easier to unit test because you can just pass in a mock object without needing the whole Hilt framework running.
📋 Practical Task
Exercise: Wiring a Weather Data Repository with Hilt
You are building a weather app. You have a WeatherApi (interface) and a WeatherRepository (class). Currently, the WeatherActivity is manually instantiating everything, which is making the code messy.
Your task:
- Create a
WeatherModuleusing@Moduleand@InstallIn(SingletonComponent::class). - Add a
@Providesmethod that returns aWeatherApi(you can mock the return value by returning a dummy implementation of the interface). - Update the
WeatherRepositoryto use@Inject constructorso Hilt knows how to provide it. - Annotate
WeatherActivitywith@AndroidEntryPointand use field injection to get theWeatherRepositoryinstance.
There are no comments for now.