Reputation: 982
recently I migrate the ViewModelInject
in dagger hilt to @HiltViewModel
by I am getting the error like this.
java.lang.RuntimeException: Cannot create an instance of class io.chativo.chat.viewmodel.TicketViewModel
Original Code
class TicketViewModel @ViewModelInject constructor(
private val ticketRepository: TicketRepository
): ViewModel() {
.....
}
Updated code
@HiltViewModel
class TicketViewModel @Inject constructor(
private val ticketRepository: TicketRepository
): ViewModel() {
The original code works perfectly, but after I migrate to @HiltViewModel
, I keep getting the Cannot create an instance of class ViewModel
error. Any idea why this happen?
build.gradle(app)
apply plugin: 'dagger.hilt.android.plugin'
// dagger - hilt (dependency injection)
implementation 'com.google.dagger:hilt-android:2.31-alpha'
kapt "com.google.dagger:hilt-android-compiler:2.29.1-alpha"
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
kapt 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha03'
build.gradle
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.29.1-alpha'
Upvotes: 0
Views: 454
Reputation: 982
Finally, I get the correct solution by myself. The compiler version should be the same with its library version.
// dagger - hilt (dependency injection)
implementation 'com.google.dagger:hilt-android:2.31-alpha'
kapt "com.google.dagger:hilt-android-compiler:2.31.1-alpha"
Hope this helps someone who struggle for an answer.
Upvotes: 1