android - Gradle 依赖不与依赖模块共享

标签 android gradle koin

上下文

我的项目中有两个模块:

  • 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/

相关文章:

Android:写入/sys/目录

android - 在膨胀 View 上设置 onClick 后,Viewpager 不再可滑动

android - Android Gradle构建无法添加第三方库

kotlin - Koin 中 Dagger 的 @Named 是什么?

android - Koin + Retrofit 在运行时添加标题

java - 为什么我得到的 fragment 是 "Unreachable Statement Error"?

android - Android构建在Bamboo上失败并显示Aapt2Exception

gradle - Gradle初始化操作(加载,解析,检查类)

android - 如何修复 startKoin() 方法上的 NoSuchMethodError

Android Studio 1.4 RC1 未知命令行选项 '-X'