Dagger Hilt @ViewModelInject 已弃用 如何在@HiltViewModel 中使用@ApplicationContext
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.
我在我的 ViewModel 中使用 @ViewModelInject
,如下所示,但现在它已被弃用,所以当我尝试使用 @HiltViewModel
但我不能使用 @ApplicationContext
init.
所以我的问题是如何使用我在 @HiltViewModel
中用 @InstallIn(SingletonComponent::class)
注释的公共依赖项?
如何在 @HiltViewModel
, ViewModelComponent::class
中使用 @ApplicationContext
?
我的代码可以与 @ViewModelInject
一起工作,如下所示
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()
?
不应使用 @ActivityScoped 或 @Singleton 注释 Viewmodel,因为 dagger-hilt 将以一种相当“特殊”的方式提供此实例,而不是像往常一样提供其他依赖项(生命周期和内容)。
首先确保您拥有以下依赖项并且它们都是最新的:
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"
由于您没有提供任何关于什么不“有效”的细节,这应该:
@HiltViewModel
class YourViewModel @Inject constructor(
@Applicationcontext context: Context
) : ViewModel()
@ViewModelInject 已被弃用并被@HiltViewModel 取代。
用 HiltViewModel 注释的 ViewModel 将可供 HiltViewModelFactory 创建。包含使用 Inject 注释的构造函数的 HiltViewModel 将在 Dagger 的 Hilt 注入的构造函数参数中定义其依赖项。 https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel
一个简单的 ViewModel 现在看起来像:
@HiltViewModel
class MainViewModel @Inject constructor() :
ViewModel() {
}
现在 Hilt 提供了一些预定义的限定符。例如,由于您可能需要来自应用程序或 activity 的上下文 class,因此 Hilt 提供了 @ApplicationContext 和 @ActivityContext 限定符。
@HiltViewModel
class MainViewModel @Inject constructor(@ApplicationContext
private val context: ApplicationContext) :
ViewModel() {
}
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.
我在我的 ViewModel 中使用 @ViewModelInject
,如下所示,但现在它已被弃用,所以当我尝试使用 @HiltViewModel
但我不能使用 @ApplicationContext
init.
所以我的问题是如何使用我在 @HiltViewModel
中用 @InstallIn(SingletonComponent::class)
注释的公共依赖项?
如何在 @HiltViewModel
, ViewModelComponent::class
中使用 @ApplicationContext
?
我的代码可以与 @ViewModelInject
一起工作,如下所示
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 useSingletonComponent
common function? Now If createViewModelComponent::class
then how to use that common function again in this? So What should I do ? Do I have to remove all common cases fromSingletonComponent
and use individually in eachViewModel()
?
不应使用 @ActivityScoped 或 @Singleton 注释 Viewmodel,因为 dagger-hilt 将以一种相当“特殊”的方式提供此实例,而不是像往常一样提供其他依赖项(生命周期和内容)。
首先确保您拥有以下依赖项并且它们都是最新的:
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"
由于您没有提供任何关于什么不“有效”的细节,这应该:
@HiltViewModel
class YourViewModel @Inject constructor(
@Applicationcontext context: Context
) : ViewModel()
@ViewModelInject 已被弃用并被@HiltViewModel 取代。
用 HiltViewModel 注释的 ViewModel 将可供 HiltViewModelFactory 创建。包含使用 Inject 注释的构造函数的 HiltViewModel 将在 Dagger 的 Hilt 注入的构造函数参数中定义其依赖项。 https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel
一个简单的 ViewModel 现在看起来像:
@HiltViewModel
class MainViewModel @Inject constructor() :
ViewModel() {
}
现在 Hilt 提供了一些预定义的限定符。例如,由于您可能需要来自应用程序或 activity 的上下文 class,因此 Hilt 提供了 @ApplicationContext 和 @ActivityContext 限定符。
@HiltViewModel
class MainViewModel @Inject constructor(@ApplicationContext
private val context: ApplicationContext) :
ViewModel() {
}