You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Jetpack Compose中Hilt注入Repository至ViewModel时出现Dagger/MissingBinding错误求助

Fixing Dagger/MissingBinding Error When Injecting WellthiRepository into AuthenticationViewModel

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 @Inject to 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 @HiltAndroidApp to 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 (or build.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 Project in Android Studio
  • Then run Build > Rebuild Project
  • If that doesn't work, delete the .gradle and build folders 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 01:52:36