我正在用Dagger-Hilt注入一个在ViewModel中依赖于@ActivityContext的类,这个模块安装在ActivityComponent中,作用域为活动,每当我试图编译时,它都会抛出一个错误。关于您的信息,我还有ActivityRetainedComponent和SingletonComponent injecting @ApplicationContext的其他模块。
现在我想弄清楚这个错误意味着什么。
error: [Dagger/MissingBinding] com.rober.fileshortcut_whereismyfile.utils.PermissionsUtils cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract static class SingletonC implements App_GeneratedInjector,
^
com.rober.fileshortcut_whereismyfile.utils.PermissionsUtils is injected at
com.rober.fileshortcut_whereismyfile.ui.fragments.filefragment.FileViewModel(�, permissionsUtils, �)
com.rober.fileshortcut_whereismyfile.ui.fragments.filefragment.FileViewModel is injected at
com.rober.fileshortcut_whereismyfile.ui.fragments.filefragment.FileViewModel_HiltModules.BindsModule.binds(vm)
@dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.rober.fileshortcut_whereismyfile.App_HiltComponents.SingletonC ? com.rober.fileshortcut_whereismyfile.App_HiltComponents.ActivityRetainedC ? com.rober.fileshortcut_whereismyfile.App_HiltComponents.ViewModelC]这是代码(我不认为这里有什么问题)
@Module
@InstallIn(ActivityComponent::class)
object PermissionModule {
@ExperimentalCoroutinesApi
@Provides
@ActivityScoped
fun providePermissionsHelper(
@ActivityContext context: Context,
) = PermissionsUtils(context)
}@HiltViewModel
@ExperimentalCoroutinesApi
class FileViewModel @Inject constructor(
private val class1: Class1,
private val class2: Class2,
private val class3: Class3,
private val permissionsUtils: PermissionsUtils //Here's the one I'm injecting
)我的猜测:它告诉我我不能注入ActivityComponent,因为FileViewModel是在ActivityComponent中注入的,因为这个组件中安装了提供给视图模型的其他依赖项。
问题:到底是怎么回事?我遗漏了什么,是否有任何使用ActivityComponent的解决方案?我真的需要那个班的@ActivityContext!
SingletonComponent/ViewModelComponent/ActivityRetainedComponent,编辑:编辑:当我切换到@InstallIn(ActivityRetainedComponent::class)并将上下文转换为@ApplicationContext context: Context时,这是可行的,请注意,它与一起工作是有意义的。但我还是不知道如何让它在@ActivityComponent上工作
编辑2:我得出的结论是不可能的,因为您正在ViewModel中安装,与ViewModel一起工作的组件是 Singleton/ActivityRetained/ViewModel,所以无法在ViewModel中注入ActivityContext,因为它需要组件@ActivityComponent,这比ViewModelComponent低1级。
发布于 2021-04-24 21:38:58
因此无法在ViewModel中注入ActivityContext,因为它需要组件@ActivityComponent,这比ViewModelComponent低1级。
你说对了。
从生命周期的角度考虑Dagger/Hilt组件(和作用域)。在安卓系统中,ViewModel的生命周期要比包含Activity或Fragment的生命周期长。这就是全班的重点,不幸的是这个名字。把它们看作是RetainedInstance或AnObjectThatSurvivesActivityConfigurationChanges可能会有帮助。是啊,这些不像ViewModel那么吸引人。
如果将一个短期对象( Activity)注入长寿对象( ViewModel),则后者将保留对前者的引用。这是内存泄漏-- Activity及其包含的所有内容(例如视图、位图)都保留在内存中,即使不再使用它。其中的几个,你冒着OutOfMemoryError的风险。
然而,反过来--即将一个长寿的物体注入一个短命的物体--是安全的。这就是为什么您让它使用Singleton/ActivityRetained/ViewModel组件。
Hilt的组件层次结构使您更难创建内存泄漏。所有这些都发生在编译时,与基于运行时的解决方案相比,这给了您更快的反馈。比你的用户发现的崩溃要好得多!
https://stackoverflow.com/questions/66945616
复制相似问题