Dagger Hilt 'Assisted' and 'ViewModelInject' is deprecated. in Dagger Hilt View Model 1.0.0-alpha03

Viewed 14598

In Dagger Hilt View Model 1.0.0-alpha01

    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01"
    implementation 'com.google.dagger:hilt-android:2.28-alpha'
    kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
    kapt 'com.google.dagger:hilt-android-compiler:2.28-alpha'

I can use the below

class MyViewModel @ViewModelInject constructor(
    private val repository: Repository,
    @Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel(), LifecycleObserver {

    // Some codes...
}

However, when I migrate to Dagger Hilt View Model 1.0.0-alpha03

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

I got the warnings

'Assisted' is deprecated. Deprecated in Java
'ViewModelInject' is deprecated. Deprecated in Java
'ViewModelInject' is deprecated. Deprecated in Java
'Assisted' is deprecated. Deprecated in Java

What's the new way of working on it?

5 Answers

In alpha03, Use the new @HiltViewModel and the normal @Inject now as shown below.

@HiltViewModel
class MyViewModel @Inject constructor(
    private val repository: Repository,
    private val savedStateHandle: SavedStateHandle
) : ViewModel(), LifecycleObserver {

    // Some code
}

In the last update of dagger hilt, they made few changes, so in your case, you can use @HiltViewModel and @Inject to use it with ViewModel.

@HiltViewModel
class MyViewModel @Inject constructor(
    private val repository: Repository,
    private val savedStateHandle: SavedStateHandle
) : ViewModel(), LifecycleObserver {
    // Some codes...
}

Also, if you were using ApplicationComponent, in the latest update it is changed to SingletonComponent.
So in your module class this way.

@Module
@InstallIn(SingletonComponent::class.java)
object hiltmodel....{}

@ViewModelInject has been deprecated and has been replaced by @HiltViewModel.

The ViewModel annotated with HiltViewModel will be available for creation by HiltViewModelFactory. The HiltViewModel containing a constructor annotated with Inject will have its dependencies defined in the constructor parameters injected by Dagger's Hilt. https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel

A simple ViewModel will now look like :

@HiltViewModel
class MainViewModel @Inject constructor(application: Application) :
AndroidViewModel(application) {
}

or

@HiltViewModel
class MainViewModel @Inject constructor() :
ViewModel() {
}

whatever your use case might be.

Got below error after upgrading hilt to v2.31+ ?:

2021-04-02 20:05:22.443 3718-3718/com.demo.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.demo.app, PID: 3718
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demo.app/com.demo.app.ux.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.demo.app.ux.viewmodels.MainViewModel
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) .....

Tried most of things mentioned in here and also adding a seperate module with model view provider API, but that none of that worked one thing missed while upgrading was to change the classpath version too.

So to make this work we need to update the classpath to 2.31 and above which is present in your project gradle :

classpath "com.google.dagger:hilt-android-gradle-plugin:2.31-alpha"

releases before jan 2021 dont support the latest @HiltViewModel annotation.

So in the project level gradle in dependencies replace hilt version to 2.33-beta

buildscript{
    ext.hiltVersion = "2.33-beta"
    dependencies{
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hiltVersion"
    }
}

and in the view model class instead of @viewmodelinject before constructor remove that and do this

@HiltViewModel
class TasksViewModel @Inject constructor(val taskDao: TaskDao) : ViewModel() {...}
Related