【问题标题】:Android Gradle Implementation vs CompileOnly PerformanceAndroid Gradle 实现与 CompileOnly 性能
【发布时间】:2018-03-15 08:27:05
【问题描述】:

The docs 提到 implementationcompile/api 提供了显着的构建时间改进。 compileOnly呢?

我的用例是一个多模块(抱歉,我不喜欢 Gradle 的多项目术语)项目,其中我有一个 Android 应用程序,以及该应用程序依赖的多个库 (implementation)。一些库还相互依赖。在库模块中声明依赖项时,我应该使用implementation 还是compileOnly?我的应用模块将使用 implementation 来依赖这些工件,因此我不需要它们通过库模块传递。

【问题讨论】:

  • compileOnly 表示这些依赖项只能在编译时访问,而在运行时不可访问。如果您的模块在运行时不需要这些依赖项,我认为声明它们没有问题compileOnly
  • 这并没有解决关于性能的问题。

标签: android gradle android-gradle-plugin android-gradle-3.0


【解决方案1】:

api 配置应该用于导出到外部modules (传递依赖) 的依赖项。 Vice-Versa implementation 配置应该用于组件内部的依赖(不是传递依赖)

实现与compileOnly

他们的工作没有相似之处,compileOnly

  • 从 java-plugin 继承的配置
  • 编译时需要
  • 也不包含在运行时类路径中或暴露给依赖 项目。

所以compileOnly 不会替换implementation 配置作业,例如:

implementation 'com.android.support:appcompat-v7:25.1.0' // can't use compileOnly here
testCompile 'junit:junit:4.12'

compile "com.google.dagger:dagger:2.8" // can't use here also
annotationProcessor "com.google.dagger:dagger-compiler:2.8" // can't use here also
compileOnly 'javax.annotation:jsr250-api:1.0' // we can use compileOnly here because it's required on run time only.

由于您的案例是“多模块”,因此您必须使用api 配置,直到您到达最终模块,最好使用implementation

下图描述了这些配置:

性能?

我认为api 需要更多内存,因为 gradle 将快照该 transitive module 中的每个类,反之亦然 implementation 是首选配置,因为(如上所述)它用于自己的内部实现。

【讨论】:

  • 感谢您的解释,但我的问题是使用compileOnly 而不是implementation 有哪些改进(如果有的话)。
  • 还不完全在那里。 compileOnly 的 gradle 文档说“在编译时需要其 API 但其 implementation 将由消费库、应用程序或运行时环境提供的依赖项。”我的问题是,将我的库的依赖项声明为 compileOnlyimplementation 有什么好处,因为两者都可以工作(需要注意的是,消费模块需要将这些依赖项声明为 implementation 如果它们在公共 API)。
  • @Eliezer as @azizbekian 在 cmets 中建议,如果您在运行时不需要模块是的,请使用 compileOnly 这可以减少内存使用量。
  • @Eliezer 对不起,我没听懂,你的意思是什么?
  • Is using compileOnly going to give better performance than using implementation 当然。
猜你喜欢
  • 2018-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
相关资源
最近更新 更多