【发布时间】:2019-01-30 14:44:34
【问题描述】:
上下文
我的项目中有两个模块:
- Java/Kotlin 模块
common - Android/Kotlin 模块
app
common 依赖于Koin,这是一个用于依赖注入的 Kotlin 库:
dependencies {
implementation 'org.koin:koin-core:1.0.2'
}
使用示例:
class MyPresenter: KoinComponent {
...
}
app 不依赖 Koin 库,因为我不需要在 Android 代码中注入任何东西,所有注入都在公共代码中(演示者、拦截器等)。
但是app 依赖于common:
dependencies {
implementation project(':common')
}
使用示例:
class MyFragment {
private val presenter = MyPresenter()
}
问题
我可以编译common,我可以在common 中运行单元测试,但是当我尝试编译app 时出现此错误:
以下类的超类型无法解析。请做出来 确保您在类路径中有所需的依赖项: xxx.common.presenter.MyPresenter 类,未解析的超类型:org.koin.standalone.KoinComponent
当我运行./gradlew :app:dependencies
debugCompileClasspath
+--- project :common
debugRuntimeClasspath
+--- project :common
| +--- org.koin:koin-core:1.0.2
依赖项在 runtime 配置中,但在 compile 配置中缺失。
到目前为止我所做的尝试:
显然我不想在app 中声明 Koin 依赖项,所以我尝试了几件事:
更改 api 的 Koin 依赖项:
dependencies {
api 'org.koin:koin-core:1.0.2'
}
不工作 - 我得到了与 implementation 完全相同的依赖树。
更改项目依赖配置:
dependencies {
implementation project(path: ':common', configuration: `compile`)
}
不工作 - 我不确定这个,但我希望它能在compile 配置中获得common 的依赖项。
更改 compile 的 Koin 依赖项:
dependencies {
compile 'org.koin:koin-core:1.0.2'
}
正在运行! 依赖项出现在 debugCompileClasspath 中,我能够运行 app。
问题
现在我很困惑:
- 由于
app不直接使用Koin,我虽然不需要依赖。为什么会这样?是不是因为MyPresenter的静态类型是KoinComponent? - 我认为
api与已弃用的compile相同。好像没有。 - 除了使用已弃用的
compile,还有其他方法吗?
【问题讨论】: