Gradle Worker API allows plugins to submit isolated (i.e. they don’t have access to Gradle model) units of work, that can be executed in parallel. Also Worker API allows (but not requires) plugin developers to use Worker daemon processes, which are managed by Gradle.
By default Kotlin runs the compiler and KAPT in Kotlin Compile Daemon.
KAPT runs in two stages:
- Stubs generation, which generates
.java stub files from Kotlin source files.
- Actual annotation processing, which executes annotation processors on stubs and java sources.
kapt.use.worker.api=true does two things:
- It uses Worker API for the second stage (annotation processing), so (non stubs generating) KAPT tasks can run in parallel.
- Also it moves the second stage (annotation processing) out of Kotlin Compile Daemon (mostly to avoid affecting non-KAPT compilation). Before 1.3.20 annotation processing was run using Gradle Worker processes, but it lead to increased memory consumption (because a Worker process cannot be used concurrently, see https://github.com/gradle/gradle/issues/8220). Since 1.3.20
kapt.use.worker.api runs annotation processing in the main Gradle process.
kotlin.parallel.tasks.in.project=true was added later. It simply uses Worker API for compileKotlin/compileKotlin2Js/compileKotlinCommon tasks, so these tasks can run in parallel. This mode does not use Worker processes, compilation is still performed in Kotlin Compile Daemon.
The name of the property might be a little confusing, I’ve chosen it to highlight the reason a user might want to enable it:
- Without Worker API it was possible to execute tasks from different projects (one task per project) in parallel (via
--parallel command line flag or by specifying
- Only Worker API allows for parallel execution of independent tasks from the same project.
In all cases parallel execution may increase memory usage (because compiler’s threads don’t share any data).
kapt.use.worker.api may increase memory usage in main Gradle process,
kotlin.parallel.tasks.in.project may increase memory usage in Kotlin Compiler Daemon process.
By default Kotlin Daemon uses the same amount of memory as main Gradle process (which is controlled by
org.gradle.jvmargs=-Xmx<MAX_HEAP_SIZE> project property). Kotlin Daemon’s heap size can be controlled by
kotlin.daemon.jvm.options system property.
E.g. to set main Gradle process heap size to 768MB, and Kotlin Daemon heap size to 2GB, add the following to
You can use the standard
jvisualvm utility from your JDK installation to determine the optimal heap size.