Android Gradle Implementation与CompileOnly性能的比较

36
文档提到implementation相比compile/api可以显著提高构建时间。那么compileOnly呢?
我的使用场景是一个多模块(抱歉,我不喜欢Gradle的多项目术语)项目,其中我有一个Android应用程序和多个库依赖于它(implementation)。其中一些库也相互依赖。在库模块中声明依赖关系时,我应该使用implementation还是compileOnly?我的应用程序模块将使用implementation来依赖这些构件,因此我不需要它们通过库模块进行传递。

2
compileOnly 的意思是这些依赖项仅在编译时可访问,而在运行时不可访问。如果您的模块在运行时不需要这些依赖项,我认为声明它们为 compileOnly 是没有问题的。 - azizbekian
1
这并没有回答问题,问题是关于性能的。 - Eliezer
1个回答

36
api配置应用于被导出到外部模块的依赖项(传递性依赖)。相反,implementation配置应用于组件内部的依赖项(非传递性依赖)implementation vs 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

以下图表描述了这些配置:

enter image description here

性能?

我认为api需要更多的内存,因为gradle将快照所有传递依赖模块中的每个类,反之implementation是首选配置,因为(如上所述)它用于其自己的内部实现。


还不太行。compileOnly 的 Gradle 文档说:“在编译时需要 API,但其 implementation 由消费库、应用程序或运行时环境提供的依赖项。”我的问题是,将我的库的依赖项声明为 compileOnlyimplementation 是否有任何好处,因为两者都可以工作(唯一的限制是消费模块需要将这些依赖项声明为 implementation,如果它们在公共 API 中)。 - Eliezer
正如@azizbekian在评论中建议的那样,如果您不需要在运行时使用模块,则可以使用compileOnly来减少内存使用。 - user6490462
比起实现,更重要还是同等级别的吗? - Eliezer
1
@Eliezer 抱歉,我不明白你的意思是什么? - user6490462
1
使用 compileOnly 会比使用 implementation 能够提供更好的性能吗?不确定。 - user6490462
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接