I have a problem with Hilt and ViewModel. I'm getting RunTimeException:
MainViewModel> has no zero argument constructor
I spent hours to find what is wrong...
Here are sources from my code:
AppModule:
@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
}
}
MainActivity:
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val viewModel: MainViewModel by viewModels()
MainViewModel:
@HiltViewModel
class MainViewModel @Inject constructor(
private val repository: MainRepository,
private val dispatchers: DispatcherProvider
) : ViewModel() {
MainRepository
class DefaultMainRepository @Inject constructor(
private val api: CurrencyApi
) : MainRepository {
Hee are Gradle dependencies:
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'
}
Application:
@HiltAndroidApp
class CurrencyApplication: Application()
Manifest:
<application
android:name=".CurrencyApplication"
Im trying to find solution everywhere like here: possible reason
I will be grateful for any help