【问题标题】:Gradle dependency not shared with dependent moduleGradle 依赖不与依赖模块共享
【发布时间】: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,还有其他方法吗?

【问题讨论】:

    标签: android gradle koin


    【解决方案1】:
    • 因为您让 Koin 类型出现在 common 的 API 中,所以 common 的消费者需要了解 Koin 类型。它们实际上变成了 API。
    • api 配置是您应该使用并且应该工作的配置
    • 最有可能的解释是,一边的Android/Kotlin项目和另一边的Java/Kotlin项目对api是什么的定义不同,消费配置apiElements是如何构建或访问的或...

    为了调试它,我建议创建一个简单的项目来重现问题并且可以共享,因为 android 或 kotlin 插件中可能存在错误。

    【讨论】:

      猜你喜欢
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 2019-04-10
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 2016-02-18
      相关资源
      最近更新 更多