上下文
我的项目中有两个模块:
- Java/Kotlin 模块
通用
- Android/Kotlin 模块
app
common
取决于 Koin ,这是一个用于依赖注入(inject)的 Kotlin 库:
dependencies {
implementation 'org.koin:koin-core:1.0.2'
}
使用示例:
class MyPresenter: KoinComponent {
...
}
app
不依赖于 Koin 库,因为我不需要在 Android 代码中注入(inject)任何东西,所有的注入(inject)都在公共(public)代码中(呈现器、拦截器等)。
但是app
依赖于common
:
dependencies {
implementation project(':common')
}
使用示例:
class MyFragment {
private val presenter = MyPresenter()
}
问题
我可以编译 common
,我可以在 common
中运行单元测试,但是当我尝试编译 app
时,我得到这个错误:
Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath: class xxx.common.presenter.MyPresenter, unresolved supertypes: 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'
}
不工作 - 我得到与实现
完全相同的依赖树。
更改项目依赖配置:
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
之外,还有其他方法吗?
最佳答案
- 因为您让 Koin 类型出现在 common 的 API 中,所以 common 的消费者需要了解 Koin 类型。它们有效地成为 API。
api
配置是您应该使用并且应该工作的配置- 最有可能的解释是,一方的 Android/Kotlin 项目和另一方的 Java/Kotlin 项目对什么是
api
有不同的定义,消费配置如何apiElements
构建或访问或 ...
要调试它,我建议创建一个简单的项目来重现问题并且可以共享,因为在 android 或 kotlin 插件中可能存在错误。
关于android - Gradle 依赖不与依赖模块共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54443194/