【发布时间】:2021-02-16 13:37:38
【问题描述】:
我正在尝试将项目迁移到 Hilt,但遇到以下问题,不确定如何使用 Hilt 传递上下文。如果我删除 provideContext 方法,那么它会抱怨以下错误:
error: [Dagger/MissingBinding] @dagger.hilt.android.qualifiers.ApplicationContext android.content.Context cannot be provided without an @Provides-annotated method.
但我的理解是,在 Hilt 中我们不需要 provideContext 方法,我们可以像下面这样使用 @ApplicationContext:
@Inject
public CardLayoutManager(@ApplicationContext Context context) {
mContext = context;
}
我错过了什么吗?
【问题讨论】:
-
AFAIK 您不能直接使用该注释注入上下文。这是为模块保留的。
-
请查看developer.android.com/training/dependency-injection/…的示例 public class AnalyticsServiceImpl implements AnalyticsService { private final Context context; @Inject AnalyticsAdapter(@ApplicationContext 上下文上下文) { this.context = context;那么我们还需要一个provideContext方法吗?
-
您是否创建了一个模块来为 CardLayoutManager 提供范围(即 SingletonScope)?如果是,请分享代码
-
@AndroidDev 你找到解决方案了吗?这让我发疯了
标签: android kotlin dependency-injection dagger dagger-hilt