0

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'
Teo
  • 876
  • 9
  • 22
  • this might help https://stackoverflow.com/questions/62471849/cannot-create-instance-of-viewmodel-after-using-hilt-in-android – NehaK Mar 05 '21 at 08:11
  • here is my dependency ```implementation 'com.google.dagger:hilt-android:2.31.2-alpha' kapt "com.google.dagger:hilt-android-compiler:2.29.1-alpha" kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha03'```. All already updated to latest. but why still getting error? – Teo Mar 05 '21 at 08:16
  • ensure your `activity/fragment` is having `@AndroidEntryPoint` annotation – Usama Altaf Mar 05 '21 at 08:19
  • @AndroidEntryPoint have you added this for the activity / fragmnet ? – NehaK Mar 05 '21 at 08:19
  • try this : iamkdblue 's answer https://stackoverflow.com/questions/44998051/cannot-create-an-instance-of-class-viewmodel – NehaK Mar 05 '21 at 08:20
  • you are missing `implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'` this library – Usama Altaf Mar 05 '21 at 08:22
  • @UsamaAtlaf yes, all of the acitivty/fragment having `@AndroidEntryPoint` or else the original code wont work. and also I did apply these dependencies. I edited my question with dependencies to make it clear – Teo Mar 05 '21 at 08:26
  • I think the latest version is 2.33-beta (for the `com.google.dagger.*`) This includes `hilt-android:` and `hilt-compiler:` (but not the classpath plug-in which is still `2.32-alpha`). This may not solve the issue, but you should try to use the same "versions" where possible. – Martin Marconcini Mar 05 '21 at 08:54

1 Answers1

1

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.

Teo
  • 876
  • 9
  • 22