Set up Hilt dependency injection in Android
✓Works with OpenClaudeYou are an Android developer setting up Hilt dependency injection framework in a Kotlin project. The user wants to configure Hilt, create modules, inject dependencies, and understand the complete setup flow.
What to check first
- Verify
build.gradle.kts(Project level) has the Hilt plugin:id("com.google.dagger.hilt.android") version "2.48" apply false - Check that your
minSdkVersionis at least 21 in app-levelbuild.gradle.kts
Steps
- Add Hilt dependencies to app-level
build.gradle.ktsunderplugins:id("com.google.dagger.hilt.android") - Add Hilt libraries to
dependencies:implementation("com.google.dagger:hilt-android:2.48")andkapt("com.google.dagger:hilt-compiler:2.48") - Create an
@HiltAndroidAppannotated Application class that extendsApplication - Declare this Application class in
AndroidManifest.xmlusing theandroid:nameattribute - Create a Hilt module using
@Moduleand@InstallInannotations to provide dependencies - Use
@Providesor@Bindsmethods within the module to define how to construct instances - Inject dependencies into Activities, Fragments, or Services using
@Injecton constructor parameters or properties - Use
@Qualifierto distinguish between multiple implementations of the same interface
Code
// Step 1: Application class
package com.example.app
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp
class MyApplication : Application()
// Step 2: Repository interface and implementation
interface UserRepository {
fun getUser(id: String): String
}
class UserRepositoryImpl : UserRepository {
override fun getUser(id: String): String = "User: $id"
}
// Step 3: Hilt Module for providing dependencies
package com.example.app.di
import com.example.app.UserRepository
import com.example.app.UserRepositoryImpl
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {
@Binds
@Singleton
abstract fun bindUserRepository(
impl: UserRepositoryImpl
): UserRepository
}
// Alternative: Using @Provides for complex setup
@Module
@InstallIn(SingletonComponent::class)
object ApiModule {
@Provides
@Singleton
fun provideApiService(): ApiService {
return ApiService("https://api.example.com")
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Kotlin / Android Skills
Other Claude Code skills in the same category — free to download.
Jetpack Compose
Build Jetpack Compose UIs with state and navigation
Room Database
Set up Room database with DAOs, entities, and migrations
Retrofit
Configure Retrofit for API calls with coroutines
Kotlin Coroutines
Write coroutines with Flow, StateFlow, and error handling
Kotlin Testing
Write Android tests with JUnit, Mockk, and Espresso
Kotlin Coroutines & Flow
Use Kotlin Coroutines and Flow for reactive async streams
Android Room Database
Set up Room (SQLite) for local data persistence in Android Kotlin apps
Want a Kotlin / Android skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.