Android Jetpack Compose中Hilt注入Repository至ViewModel时出现Dagger/MissingBinding错误求助
Hey, let's break down this error and fix it step by step. That Dagger/MissingBinding error.NonExistentClass message is a bit misleading, but it usually points to issues with how Hilt can resolve your dependencies.
1. First, Fix Your WellthiRepository Class Definition
Looking at your Repository code, you're missing the class keyword! That's a critical syntax error making Hilt unable to recognize it as an injectable class. Here's the corrected version:
class WellthiRepository @Inject constructor( private val tokenManager: TokenManager, private val tokenService: TokenService, private val userManager: UserManager ){}
Without the class keyword, the compiler doesn't treat this as a valid class, so Dagger can't generate the necessary injection bindings.
2. Ensure All Dependencies of WellthiRepository Are Injectable
Your WellthiRepository depends on three classes: TokenManager, TokenService, and UserManager. Each of these needs to be accessible to Hilt:
- If they're your custom classes, add
@Inject constructor()to each of them (just like you did for the ViewModel and Repository). For example:class TokenManager @Inject constructor() { // Your implementation here } - If they're third-party classes or you can't add
@Injectto them, create a Hilt Module to provide instances using@Provides:@Module @InstallIn(SingletonComponent::class) object AppModule { @Provides fun provideTokenManager(): TokenManager { return TokenManager() // Adjust this to match your actual initialization } @Provides fun provideTokenService(): TokenService { // If TokenService uses Retrofit, for example, initialize it here return Retrofit.Builder() .baseUrl("your_base_url") .build() .create(TokenService::class.java) } @Provides fun provideUserManager(): UserManager { return UserManager() } }
If even one of these dependencies doesn't have a valid binding, Dagger will throw that MissingBinding error—often hiding the actual missing class behind that vague NonExistentClass message.
3. Verify Hilt's Core Setup Is Complete
Don't overlook the basics:
- Add
@HiltAndroidAppto your Application class: This is required to initialize Hilt's dependency graph.@HiltAndroidApp class YourApplication : Application() { // Application code here } - Check your Gradle dependencies: Make sure you have all required Hilt and ViewModel-Hilt dependencies in your module-level
build.gradle(orbuild.gradle.kts):plugins { id 'com.android.application' id 'kotlin-android' id 'dagger.hilt.android.plugin' id 'kotlin-kapt' // Required for annotation processing } dependencies { // Hilt core implementation 'com.google.dagger:hilt-android:2.48' kapt 'com.google.dagger:hilt-android-compiler:2.48' // Hilt for ViewModels implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0' kapt 'androidx.hilt:hilt-compiler:1.0.0' }
Double-check that kotlin-kapt is enabled (it's needed for processing Hilt's annotations).
4. Clean and Rebuild Your Project
Sometimes Dagger's build cache causes weird, false errors. Try these steps:
- Go to
Build > Clean Projectin Android Studio - Then run
Build > Rebuild Project - If that doesn't work, delete the
.gradleandbuildfolders in your project root, then recompile
Walk through these steps one by one, and you should get that injection working. If you're still stuck, double-check that none of your dependency classes are marked as internal (which can prevent Hilt from accessing them) and that all annotations are imported correctly (e.g., dagger.hilt.android.lifecycle.HiltViewModel, not a different ViewModel annotation).
内容的提问来源于stack exchange,提问作者Ankit Shah

