Search code examples
androidkotlindependency-injectionandroid-viewmodeldagger-hilt

Dagger Hilt @ViewModelInject is deprecated how to use @ApplicationContext in @HiltViewModel


It was android studio problem I think. It's started working automatically after 2 days maybe Restart android studio that's all it takes. I was using 2.31.2-alpha version.

I'm using @ViewModelInject in my ViewModel as shown in below but now It's deprecated so When I tried to use @HiltViewModel but I can't use @ApplicationContext init.

So my question is How to use common dependency which I annotated with @InstallIn(SingletonComponent::class) in @HiltViewModel ?

How to use @ApplicationContext in @HiltViewModel , ViewModelComponent::class ?

My code Which Work fine with @ViewModelInject are below

1. AppModule()

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class SplashApiInterface

@Module
@InstallIn(SingletonComponent::class)
class AppModule() {
    internal var pref_name = Common.pref_name

    @Provides
    @Singleton
    fun mySharedPreference(@ApplicationContext context: Context): SharedPreferences {
        return context.getSharedPreferences(pref_name, Context.MODE_PRIVATE)
    }

    @Provides
    @Singleton
    fun connectionDetector(@ApplicationContext context: Context): ConnectionDetector {
        return ConnectionDetector(context)
    }

    @Provides
    fun myCompositeDisposable(): CompositeDisposable {
        return CompositeDisposable()
    }

    @SplashApiInterface
    @Provides
    @Singleton
    fun mAPIServiceSplash(@ApplicationContext context: Context): ApiInterface {
        return ApiUtils.getAPIServiceSpl(context)
    }

}`

2.SplashVModel

@ActivityScoped
@Singleton
class SplashVModel @ViewModelInject constructor(@ApplicationContext val context: Context,
                                                val sharedPreferences: SharedPreferences,
                                                @SplashApiInterface val mAPIService: ApiInterface,
                                                val myCompositeDisposable: CompositeDisposable,
                                                var cd: ConnectionDetector
) : ViewModel() {
    
    // here I removed use cases of constructor value for brevity

    }

}

So now if I use @HiltViewModel how to use SingletonComponent common function? Now If create ViewModelComponent::class then how to use that common function again in this? So What should I do ? Do I have to remove all common cases from SingletonComponent and use individually in each ViewModel()?


Solution

  • A Viewmodel should not be annotated with @ActivityScoped or @Singleton, as dagger-hilt will provide this instance in a rather "special" way and not as usual as it provides the other dependencies (lifecycle and stuff).

    First make sure, that you have the following dependencies and there are all up to date:

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

    As you didn't provide any details about what's not "working", this should:

    @HiltViewModel
    class YourViewModel @Inject constructor(
        @Applicationcontext context: Context
    ) : ViewModel()