When developing modern Android apps with Kotlin and Dagger Hilt, dependency injection (DI) becomes significantly more streamlined. However, many developers encounter a common and frustrating issue: Dagger Hilt not injecting dependencies properly. This article will help you understand the causes, show you how to resolve the issue, and walk you through a complete example with code.
What is Dagger Hilt?
Dagger Hilt is a dependency injection framework built on top of Dagger. It simplifies DI setup in Android by providing:
- Automatic component generation
- Predefined scopes (
@Singleton
,@ActivityScoped
, etc.) - Integration with Android lifecycle
Despite its benefits, misconfiguration or missing annotations can cause Dagger Hilt not to inject dependencies as expected.
Common Error: Hilt Not Injecting in Kotlin
Symptoms:
- App crashes on launch
- Injection targets remain null
- Unresolved references during build
- Error:
Unresolved reference: Dagger<YourComponent>
or@Inject constructor not called
Root Causes & Fixes
1. Missing @HiltAndroidApp Annotation
You must annotate your Application
class with @HiltAndroidApp
.
Fix:
@HiltAndroidApp class MyApp : Application()
Don’t forget to declare it in the AndroidManifest.xml
:
<application android:name=".MyApp" ... > </application>
2. Injecting into Non-Hilt Components
You can only inject into Hilt-supported Android classes such as Activity
, Fragment
, ViewModel
, Service
, and BroadcastReceiver
.
Fix:
Make sure your class extends a Hilt-supported class, and annotate with @AndroidEntryPoint
.
@AndroidEntryPoint class MainActivity : AppCompatActivity() { @Inject lateinit var userRepository: UserRepository }
3. Missing @Inject Constructor
Your class to be injected should have a constructor annotated with @Inject
.
Example:
class UserRepository @Inject constructor( private val apiService: ApiService )
4. Forgot to Annotate ViewModels
Use @HiltViewModel
in ViewModels and @Inject
on their constructor.
Example:
@HiltViewModel class MainViewModel @Inject constructor( private val userRepository: UserRepository ) : ViewModel()
Don’t forget to use the by viewModels()
Hilt delegate in fragments or activities:
val viewModel: MainViewModel by viewModels()
Complete Example: Fixing Hilt Injection
Let’s walk through a basic setup.
1. Add Dependencies
In build.gradle (project)
:
classpath "com.google.dagger:hilt-android-gradle-plugin:2.48"
In build.gradle (app)
:
plugins { id 'com.android.application' id 'kotlin-kapt' id 'dagger.hilt.android.plugin' } dependencies { implementation "com.google.dagger:hilt-android:2.48" kapt "com.google.dagger:hilt-compiler:2.48" }
2. App Class
@HiltAndroidApp class MyApp : Application()
3. Network Module
@Module @InstallIn(SingletonComponent::class) object NetworkModule { @Provides @Singleton fun provideApiService(): ApiService { return Retrofit.Builder() .baseUrl("https://example.com") .addConverterFactory(GsonConverterFactory.create()) .build() .create(ApiService::class.java) } }
4. Repository
class UserRepository @Inject constructor( private val apiService: ApiService ) { fun getUsers() = apiService.getUsers() }
5. ViewModel
@HiltViewModel class MainViewModel @Inject constructor( private val userRepository: UserRepository ) : ViewModel() { fun loadUsers() { // Do something with repository } }
6. MainActivity
@AndroidEntryPoint class MainActivity : AppCompatActivity() { private val viewModel: MainViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) viewModel.loadUsers() } }
Extra Tips to Debug Hilt Injection Issues
- Clean & Rebuild Project
Sometimes, stale builds cause injection failures. Use:Build > Clean Project Build > Rebuild Project
- Enable Annotation Processing
Ensure KAPT is enabled in your module. - Use Correct Scope
Match the correct scope with component (@Singleton
withSingletonComponent
, etc.) - Check Hilt Gradle Plugin
Always applydagger.hilt.android.plugin
in yourbuild.gradle
. - Avoid Manual Component Creation
Hilt handles component generation; avoid doing it manually as with vanilla Dagger.
Conclusion
If Dagger Hilt is not injecting in your Kotlin Android app, it’s likely due to missing annotations, incorrect class structure, or module misconfiguration. By following the complete setup and reviewing the common issues discussed above, you can avoid hours of frustration and get your dependencies working seamlessly.