如何使用 @HiltViewModel 正确使用 Hilt 和 ViewModel?

我对 Hilt 和 ViewModel 有疑问。我收到 RunTimeException:

MainViewModel> has no zero argument constructor

我花了几个小时才发现出了什么问题……

以下是我的代码的来源:

应用模块:

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Singleton
    @Provides
    fun provideCurrencyApi(): CurrencyApi = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
        .create(CurrencyApi::class.java)

    @Singleton
    @Provides
    fun provideMainRepository(api: CurrencyApi): MainRepository = DefaultMainRepository(api)

    @Singleton
    @Provides
    fun provideDispatchers(): DispatcherProvider = object : DispatcherProvider {
        override val main: CoroutineDispatcher
            get() = Dispatchers.Main
        override val io: CoroutineDispatcher
            get() = Dispatchers.IO
        override val default: CoroutineDispatcher
            get() = Dispatchers.Default
        override val unconfined: CoroutineDispatcher
            get() = Dispatchers.Unconfined
    }
}

主要活动:

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private val viewModel: MainViewModel by viewModels()

主视图模型:

@HiltViewModel
class MainViewModel @Inject constructor(
    private val repository: MainRepository,
    private val dispatchers: DispatcherProvider
) : ViewModel() {

主存储库

class DefaultMainRepository @Inject constructor(
    private val api: CurrencyApi
) : MainRepository {

Hee 是 Gradle 依赖项:

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'

    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.okhttp3:okhttp:4.9.0'

    implementation "androidx.activity:activity-ktx:1.4.0"

    implementation 'com.google.dagger:hilt-android:2.40.5'
    kapt 'com.google.dagger:hilt-compiler:2.40.5'
    implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
    kapt'androidx.hilt:hilt-compiler:1.0.0'

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.4.0'

    androidTestImplementation  'com.google.dagger:hilt-android-testing:2.40.5'
    kaptAndroidTest 'com.google.dagger:hilt-compiler:2.40.5'
    testImplementation 'com.google.dagger:hilt-android-testing:2.40.5'
    kaptTest 'com.google.dagger:hilt-compiler:2.40.5'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

应用:

@HiltAndroidApp
class CurrencyApplication: Application()

显现:

    <application
        android:name=".CurrencyApplication"

我试图在这里找到解决方案: possible reason

我将不胜感激任何帮助

stack overflow How to properly use Hilt with ViewModel using @HiltViewModel?
原文答案

答案:

作者头像

首先,检查 annotationProcessor 是否能够生成正确的绑定,所以转到 app.gradle 并检查这些依赖项是否存在:

dependencies {

    implementation 'com.google.dagger:hilt-android:2.30-alpha'
    kapt 'com.google.dagger:hilt-android-compiler:2.30-alpha'

    implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02'
    kapt'androidx.hilt:hilt-compiler:1.0.0-alpha02'

...
}
作者头像

你没有在这里提到,所以我想你忘了在你的应用程序中添加 @HiltAndroidApp

@HiltAndroidApp
class MyOwnApplication : Application() {

}

然后在您的清单文件中添加 android:name=".MyOwnApplication" 在您的应用程序标签内。

作者头像

您正在使用“kapt hilt-compiler”两次。

我不确定这是一个问题,但我正在使用它并且它有效:

... compose_version = '1.3.0-beta02' 
... hilt_version = '2.42'

// Hilt
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation "androidx.hilt:hilt-navigation-compose:1.0.0"