preface
The Android Gradle plugin 4.0 supports the use of Kotlin scripts (KTS) in Gradle build configurations. Used as an alternative to Groovy, the programming language used in Gradle configuration files. KTS is better for writing Gradle scripts than Groovy because code written in Kotlin is more readable, and Kotlin provides better compile-time checking and IDE support.
However, the document also notes that while KTS is currently better integrated into Android Studio’s code editor than Groovy, builds with KTS tend to be slower than with Groovy, so build performance should be considered when migrating to KTS.
So how does KTS perform compared to Groovy? For everyone to decide whether to migrate to KTS to provide certain reference
KTS
Performance analysis
Performance analysis tool
To analyze THE performance of KTS, we first need to measure the compile time consistently. The compile speed can be affected by many factors, such as build cache, so it is difficult to measure the impact of KTS plug-ins on performance
Performance can be accurately measured using Gradle Performance Profiler, a tool for gathering performance analysis and benchmarking analysis information for Gradle builds. With the Gradle performance profiler, you can create build scenarios and run them multiple times to prevent excessively different results and ensure reproducibility of the results.
Part of the project configuration for benchmarking analysis includes:
- The plug-in version
Gradle
versionJVM
Settings (heap size, permanent generation size, garbage collection, etc.)Gradle
Number of workers (org.gradle.workers.max
)- Click on plug-in options to further optimize performance
For example, if we need to benchmark clean builds, you can create a scenario for gradle-Profiler execution:
# <root-project>/scenarios.txt
clean_build {
tasks = [":app:assembleDebug"]
cleanup-tasks = ["clean"]
}
Copy the code
To run this scenario, use the following command:
gradle-profiler --benchmark --project-dir <root-project> --scenario-file scenarios.txt
Copy the code
With the above commands, you can run Clean Build multiple times and generate a Clean Build performance report. In addition to clan builds, Gradle-Profiler can also perform performance analysis for incremental builds, different gradle plugin versions, and different memory /CPU levels. With the gradle-profile command, build scenarios can be created and run multiple times, preventing the results from being too different and ensuring reproducibility of the results to help us better analyze performance. For details on how to use Gradle-Profile, see the documentation: Analyzing Build Performance
Gradle 6.8
Version Performance Analysis
For Gradle 6.8, we analyze KTS performance from the following four use cases
- First run (that is, clear all
build cache
) buildSrc abi
Changed (supportedabi
Most of the cache is invalidated and most of the code needs to be recompiled.buildSrc
非abi
Change (i.e.,buildSrc
General modification in)- There is no change
The following data is from a performance test run on Gradle CI. These tests were run in a large project with a large number of subprojects, and they were run on Groovy and Kotlin DSL for comparison purposes.
Use case | Groovy | Kotlin | Differences |
---|---|---|---|
First use | 🟢 33.5 s | 🔴 76.2 s | Groovy DSL is 2.2x faster |
buildSrc abi change |
🟢 13.2 s | 🔴 42.3 s | Groovy DSL is 3.2x faster |
buildSrc non-abi change |
🔴 13 s | 🟢 5.2 s | Kotlin DSL is 2.5x faster |
Nothing changes | 🔵 1.7 s | 🔵 1.8 s | Similar performance |
As you can see, Groovy scripts still have some performance advantages
- On the first run,
Groovy DSL
thanKTS
fast2.2
times - in
buildSrc abi
Change,Groovy DSL
thanKTS
fast3.2
times - in
buildSrc
nonabi
Change,KTS
thanGroovy
fast2.5
times - When the code does not change, the performance is similar
As you can see, KTS only has a performance advantage with non-ABI changes. This is because groovy changes in buildSrc make the entire project obsolete, causing the project to be recompiled, while KTS changes in buildSrc can skip compilation of unaffected build script files. So when we modify Buildsrc, KTS builds much faster than groovy plug-ins
The above data from: builds.gradle.org/buildConfig… , you can log in to view the information in visitor mode
Gradle 7.4
Version Performance Analysis
For Gradle version 7.4, we analyzed KTS performance through the following three use cases
- First run (that is, clear all
build cache
) buildSrc abi
Changed (supportedabi
Most of the cache is invalidated and most of the code needs to be recompiled.buildSrc
非abi
Change (i.e.,buildSrc
General modification in)
Use Case | Groovy | Kotlin | Difference |
---|---|---|---|
First use | 🟢 38.855 s | 🔴 63.54 s | Groovy DSL is 1.6x faster |
buildSrc abi change |
🟢 25.307 | 🔴 35.014 s | Groovy DSL is 1.4x faster |
buildSrc non-abi change |
🔴 24.526 s | 🟢 4.732 s | Kotlin DSL is 5x faster |
As you can see, KTS compilation performance has improved for Gradle 7.4, reducing the performance gap to about 1.5 times
conclusion
Overall, KTS has huge advantages over Groovy in terms of readability, ease of use, compile-time checking, and IDE support, but has some disadvantages in terms of performance, as follows:
- for
Gradle 6.8
Version, if the cache is mostly invalid or there is no cache,Groovy DSL
thanKTS
fast2
to3
times Gradle 7.4
versionKTS
Performance is improved. If most of the cache fails or there is no cache,Groovy DSL
thanKTS
fast1.5
Times the left and right sides.- when
buildSrc
Occurs in theabi
Change,kts
Script compile ratioGroovy DSL
fast4
to5
Times, that’s becausebuildSrc
In thekts
You can skip compilation of unaffected build scripts, whilegroovy
Temporary does not support - When no changes have been made to the project,
KTS
withGroovy DSL
Not much difference in compile speed
KTS has both advantages and disadvantages in terms of ease of use and performance. Gradle is also in the process of optimization. You can decide whether to migrate your build configuration from Groovy to KTS based on your own project situation
The resources
The Kotlin and Groovy DSLs should have similar performance characteristics