32

简单几招提速 Kotlin kapt编译 - 技术小黑屋

 4 years ago
source link: https://droidyue.com/blog/2019/08/18/faster-kapt/?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

简单几招提速 Kotlin Kapt编译

Aug 18th, 2019

应用Kotlin之后,涉及到注解的注解处理器依赖也会由annotationProcessor替换成kapt,和最初应用Kotlin一样,总会让人一种感觉,一番应用Kotlin和Kapt之后,编译耗时更长了,不过好在Kotlin和Google 在这一方面做了很多的优化和改进,本文将简单介绍一些配置,来实现项目编译关于kapt这方面的加速。

开启Gradle 构建缓存支持(Gradle build cache support)

默认情况下,kapt注解处理任务并没有利用开启gradle的构建缓存,需要我们手动增加配置开启

开启方式:在项目的app module下的 build.gradle 文件增加如下代码

1
2
3
kapt {
    useBuildCache = true
}
  • kapt配置和android配置同一层级。
  • 该特性支持从Kotlin 1.2.20开始。
  • 上述配置生效需Gradle为4.3及以上,且开启build-cache。(增加—build-cache 选项或在gradle.properties文件添加org.gradle.caching=true

并行执行kapt任务

为了加快构建速度,我们可以利用Gradle worker API实现并行执行kapt任务。

开启方式,在gradle.properties文件中增加

1
kapt.use.worker.api=true
  • Gradle worker API需依赖Gradle4.10.3及以上。
  • 该特性支持自Kotlin 1.2.60
  • 启用并行执行,会引发更高的内存占用

启用kapt编译规避

除此之外,我们可以利用Gradle compile avoidance(编译规避)来避免执行注解处理。

注解处理被略过的场景有

  • 项目的源文件没有改变
  • 依赖的改变是ABI(Application Binary Interface)兼容的,比如仅仅修改某个方法的方法体。

开启方式:

  • 对于注解依赖需要使用kapt显式声明
  • gradle.properties文件中增加kapt.include.compile.classpath=false
  • 该特性需 Kotlin 1.3.20 及以上

增量注解处理

Kotlin 自1.3.30引入了一个实验功能,即支持注解增量处理。

开启需要很简单,在gradle.properties中加入

1
kapt.incremental.apt=true

但是还需要有一个前提,就是开启Gradle的增量编译(Kotlin 1.1.1已默认开启)。

除此之外,关键的因素还是需要开依赖的注解处理器是否支持增量处理。

如何查看注解处理器是否支持增量编译

1
2
3
4
5
6
7
8
9
10
11
12
13
./gradlew aDeb -Pkapt.verbose=true | grep KAPT

[INFO] Incremental KAPT support is disabled. Processors that are not incremental:
  com.bumptech.glide.annotation.compiler.GlideAnnotationProcessor,
  dagger.internal.codegen.ComponentProcessor,
  android.arch.lifecycle.LifecycleProcessor.
[INFO] Incremental KAPT support is disabled. Processors that are not incremental:
  butterknife.compiler.ButterKnifeProcessor,
  com.alibaba.android.arouter.compiler.processor.AutowiredProcessor,
  com.alibaba.android.arouter.compiler.processor.InterceptorProcessor,
  com.alibaba.android.arouter.compiler.processor.RouteProcessor,
  dagger.internal.codegen.ComponentProcessor,
  com.google.auto.service.processor.AutoServiceProcessor.

更新依赖至最新版

上面我们看到了glide,butterknife等依赖,我们都可以通过将这些依赖更新到最新版来解决

更新加手动配置

以Dagger为例,除了更新到最新版之外,还需要增加如下的配置

1
2
3
4
5
6
7
8
9
android {
   defaultConfig {
      javaCompileOptions {
         annotationProcessorOptions {
            arguments << ["dagger.gradle.incremental": "true"]
         }
      }
   }
]

参考链接https://github.com/google/dagger/issues/1120

Troubleshooting

  • 如果启用上面的方案导致问题,可以找到对应的配置,关闭该特性。

最后的建议

  • 积极保持依赖为最新(稳定)版,否则时间越长升级成本越大。

References

Posted by androidyue Aug 18th, 2019Android, Gradle, Kotlin, cache, katp, 优化, 注解处理器, 编译

« Kotlin编译与Intrinsics检查 Contract,开发者和 Kotlin 编译器之间的契约 »


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK