Gradle 依赖项未与依赖模块共享

Tij*_*jee 5 android gradle koin

语境

我的项目中有两个模块:

  • Java/Kotlin 模块 common
  • Android/Kotlin 模块 app

common依赖Koin,这是一个用于依赖注入的 Kotlin 库:

dependencies {
  implementation 'org.koin:koin-core:1.0.2'
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

class MyPresenter: KoinComponent {
  ...
}
Run Code Online (Sandbox Code Playgroud)

app 不依赖于 Koin 库,因为我不需要在 Android 代码中注入任何东西,所有注入都在公共代码中(演示者、拦截器等)。

app取决于common

dependencies {
  implementation project(':common')
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

class MyFragment {
  private val presenter = MyPresenter()
}
Run Code Online (Sandbox Code Playgroud)

问题

我可以编译common,我可以在 中运行单元测试common,但是当我尝试编译时出现app此错误:

无法解析以下类的超类型。请确保您在类路径中具有所需的依赖项:class xxx.common.presenter.MyPresenter,未解析的超类型:org.koin.standalone.KoinComponent

当我跑 ./gradlew :app:dependencies

debugCompileClasspath
+--- project :common
debugRuntimeClasspath
+--- project :common
|    +--- org.koin:koin-core:1.0.2
Run Code Online (Sandbox Code Playgroud)

依赖项在runtime配置中,但在配置中缺失compile


到目前为止我尝试过的:

显然我不想在 app所以我尝试了几件事:

更改 Koin 依赖项api

dependencies {
  api 'org.koin:koin-core:1.0.2'
}
Run Code Online (Sandbox Code Playgroud)

不工作- 我得到与implementation.

更改项目依赖配置:

dependencies {
  implementation project(path: ':common', configuration: `compile`)
}
Run Code Online (Sandbox Code Playgroud)

不工作-我不知道这一个,但我希望它会得到的依赖commoncompile配置。

更改 Koin 依赖项compile

dependencies {
  compile 'org.koin:koin-core:1.0.2'
}
Run Code Online (Sandbox Code Playgroud)

在职的!依赖关系会出现在debugCompileClasspath和我能够运行app


问题

现在我很困惑:

  • 由于app不直接使用 Koin,我认为它不需要依赖项。为什么呢?是不是因为静态类型MyPresenterKoinComponent
  • 我认为api与已弃用的compile. 似乎没有。
  • 除了使用已弃用的 ,还有其他方法compile吗?

Lou*_*met 2

  • 因为你让Koin类型出现在common的API中,那么common的消费者就需要了解Koin类型。它们实际上成为了 API。
  • api配置是您应该使用并且应该有效的
  • 最可能的解释是,一侧的 Android/Kotlin 项目和另一侧的 Java/Kotlin 项目之间对于什么是api、如何构建或访问消耗性配置apiElements或...有不同的定义。

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