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

k10s72fa  于 2023-03-28  发布在  Android
关注(0)|答案(3)|浏览(295)

我遇到了一个问题,与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() {

MainRepository

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

以下是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
我将感激任何帮助

1wnzp6jl

1wnzp6jl1#

首先,检查anotationProcessor以确保hilt能够生成正确的绑定,因此请转到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'

...
}
i86rm4rw

i86rm4rw2#

您使用了两次'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"
iovurdzv

iovurdzv3#

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

@HiltAndroidApp
class MyOwnApplication : Application() {

}

然后在你的manifest文件中添加android:name=".MyOwnApplication"到你的application标签中。

相关问题