Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using useBuildCache in gradle and kapt

I would like to use the new kotlin gradle plugin capability and cache my build results, for more information please read this. Another way of boosting the build is to cache kapt tasks, but it's not enabled by default

because Gradle does not yet have a way to map inputs and outputs for annotation processors

The only kapt dependency that I have is dagger 2, from your experience is it safe to cache it ?

like image 409
Efi MK Avatar asked Mar 09 '18 20:03

Efi MK


1 Answers

The reason why caching outputs of the Kapt tasks is not considered safe is that annotation processors contain arbitrary code that may not conform to one or more of the requirements that ensure correct caching:

  • The transformation should only use the task inputs and should not take anything else into account. By default, the task inputs include the sources, the compile classpath, Android layouts, compiler and annotation processor options and implementations, and some other things, but you can register additional inputs as you see fit.

    Violating this rule leads to false cache hits, when some of the inputs that the annotation processor uses are not tracked as such, and a change in those inputs is not reflected in the cache key calculated by Gradle, resulting into a hit while running the task would actually result into different outputs.

  • All of the outputs produced by the annotation processor should be tracked as the task outputs, which are by default generated Java & Kotlin sources, and generated binary classes. If a task produces anything else, you can register it as the task output in the build script manually.

    Not following this rule will leave some of the produced outputs out of the cache, not saving them and thus not loading them.

  • The annotation processor should be pure, that is, given the same inputs, it should produce the same outpupts. It should not show randomized behavior or use the system time.

    Caching outputs of an annotation processor that does not follow this rule is futile, since it will often result into a cache miss or a false cache hit.

As far as I can tell, Dagger 2 follows these rules, and caching its outputs should work just fine, though I can't provide guarantees about that. Anyway, feel free to experiment and see how caching works for your build.

If you encounter any issues with a specific annotation processor that supposedly should get along with caching according to the rules above, please report that to the Kotlin issue tracker.

like image 167
hotkey Avatar answered Oct 14 '22 10:10

hotkey