gpt4 book ai didi

android - Dagger Hilt 错误注入(inject) ActivityContext

转载 作者:行者123 更新时间:2023-12-04 16:37:52 24 4
gpt4 key购买 nike

我正在注入(inject) Dagger-Hilt一个依赖于 @ActivityContext 的类在 ViewModel 中,此模块安装在 ActivityComponent并且范围为 Activity ,每当我尝试编译时它都会给我一个错误。供您引用,我还有其他带有 ActivityRetainedComponent 和 SingletonComponent 注入(inject) @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
)
我的猜测:它告诉我我不能注入(inject) ActivityComponent 因为 FileViewModel 是在 ActivityRetainedComponent/SingletonComponent/ViewModelComponent 中注入(inject)的,因为提供给 viewmodel 的其他依赖项都安装在这个组件中?
问题:这里到底发生了什么?我错过了什么,有没有使用 ActivityComponent 的解决方案?我真的需要 @ActivityContext为那堂课!
编辑:这在我更改为 @InstallIn(ActivityRetainedComponent::class) 时有效和 @ApplicationContext context: Context 的上下文,请注意,它适用于 SingletonComponent/ViewModelComponent/ActivityRetainedComponent,这是有道理的。但我仍然不知道如何使它工作 @ActivityComponent 编辑 2:我得出的结论是不可能的,因为您在 ViewModel 和 中安装与 ViewModel 一起使用的组件是 Singleton/ActivityRetained/ViewModel ,所以无法注入(inject) ActivityContext在 ViewModel 中,因为它需要组件 @ActivityComponent这比 ViewModelComponent 低 1 级。
来源: https://dagger.dev/hilt/components#fn:1

最佳答案

so there's no way to inject ActivityContext in a ViewModel since it requires the component @ActivityComponent and this is 1 level below of ViewModelComponent.


你说对了。
从生命周期的角度考虑 Dagger/Hilt 组件(和范围)。在 Android 中, ViewModel具有比其包含的 Activity 更长的生命周期或 Fragment .这就是这门课的全部意义,只是名字很不幸。将它们视为 RetainedInstance 可能会有所帮助或 AnObjectThatSurvivesActivityConfigurationChanges .是的,这些不像 ViewModel 那样吸引人.
如果您将一个短期对象( Activity)注入(inject)一个长期对象( ViewModel),后者将保留对前者的引用。这是内存泄漏 - Activity并且它包含的所有内容(例如 View 、位图)即使不再使用也会保留在内存中。其中几个,你冒着 OutOfMemoryError的风险.
然而,反过来——即将一个长生命周期的物体注入(inject)一个短生命周期的物体——是安全的。这就是为什么你让它与 Singleton 一起工作的原因。/ ActivityRetained/ ViewModel成分。
Hilt 的组件层次结构使您更难造成内存泄漏。所有这一切都发生在编译时,与基于运行时的解决方案相比,这为您提供了更快的反馈。比您的用户通过崩溃发现的要好得多!

关于android - Dagger Hilt 错误注入(inject) ActivityContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66945616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com