android - 带 hilt 的 API 接口依赖注入
问题描述
我是匕首的新手。
我创建了实现,在其中注入了构造函数,例如:
class ListItemsPagingSourceFactory @Inject constructor(
private val api: Api,
private val pageErrorsQueue: ListItemsPagingErrorsQueue
) {
}
interface Api {
fun getList(
count: Int,
offset: Int
) : ListItemResponse
我的 api 接口有一个错误,我只想实现我的方法以供以后使用。但是会发生错误,例如:
Error: [Hilt]
public abstract interface Api {
^
当我添加@InstallIn @EntryPoint
到 API 接口:
error: [Hilt]
@InstallIn, 'value' class is invalid or missing: @dagger.hilt.InstallIn({})
[Hilt] Processing did not complete. See error above for details.
编辑:我有非常相似的实现,但是在我使用的地方进行改造(@Get,@Query 等)我想实现我自己的“API”,它只是从服务本地获取数据而不启动数据库的方法。这是来自 retrofitApi 的代码
interface RetrofitApi {
@GET("data/mine")
suspend fun getDataItems(
@Query("count") count: Int,
@Query("size") size: Int
): DataItemsResponse
如何将其更改为仅接口或抽象类,它只有方法而不启动 DB => 以在日志中没有 Dagger 尖叫。
编辑2:
此配置也不起作用。
@InstallIn
@Module(includes = [AppModule.Api::class])
class AppModule {
// interface with @Binds
@Module
interface Api {
@Binds
fun getList(
count: Int,
offset: Int
) : ListItemResponse
}
}
错误:
@Binds methods must have exactly one parameter, whose type is assignable to the return type
解决方案
由于 API 是一个抽象接口,因此您不会在其中添加这些注释。您可以将它们添加到具体的实现中。无论什么类都有那些 Dagger 需要能够实例化,它不能实例化一个抽象类。
推荐阅读
- c# - 为什么我的标签助手无法正确解析?
- flutter - 为什么不调用自定义 ErrorWidget.builder?
- php - 将 php 从 5 升级到 7.2 和 .php MySQL 代码停止工作
- node.js - 电子中的IPC发送/打开和调用/处理有什么区别?
- html - 选择html元素时如何添加类
- python-3.x - 连接由熊猫中的另一列分组的字符串列
- ios - prefetchRowsAt 不为最后一行调用
- java - 如何确保长时间运行的 Java 线程永不死亡
- amazon-web-services - 使用 AWS 模板构建 Hyperledger 结构 - 堆栈创建错误
- php - Laravel 迁移显示“定义了多个主键”