The team has been using Kotlin for a long time and has never been satisfied with kotlin’s compilation speed, but can live with it. Recently, when I started a new project, a lot of my colleagues came from Java, and they couldn’t take it anymore, so optimizing the compilation speed became important.

Before and after optimization

Our full compile time before optimization was 2 minutes and 21 seconds

The specific time-consuming Tasks can be found in Run Tasks:

You can see the specific time-consuming tasks above, mainly kapt-related compilation and compilation of kotlin code, and finally transformClassedWithXXX.

The full compilation time after optimization is 31s

The optimized incremental compilation time is 15s

Optimization steps:

1. Optimize gradle configuration:

Create a gradle.properties file in the project root directory

// Start gradle parallel compilation, start daemon, Adjust the JVM memory size org. Gradle. Daemon = true org. Gradle. Configureondemand = true org. Gradle. The parallel = true org. Gradle. Jvmargs = - Xmx4096m - XX: MaxPermSize = 1024 m - XX: + HeapDumpOnOutOfMemoryError - Dfile. Encoding = utf-8 gradle cache org. / / open gradle. Caching = true Android. enableBuildCache=true // Enable incremental and parallel compilation of Kotlin. incremental=true kotlin.increment. Java =true Kotlin. Incremental. Js = true kotlin. Caching, enabled = true kotlin.. The parallel tasks. In the project = true parallel compilation / / / / open kotlin kapt optimization Kapt.use.worker. API =true // Run in parallel with kapt.incremental. Apt =true // Build with incremental kapt1.3.30 supported Kapt.include.com. Running the classpath = false / / kapt avoiding if use kapt rely on content did not change, will compile reuse content completely, Save the most in the picture above: app: kaptGenerateStubsDebugKotlin timeCopy the code

In the above configuration, we first adjusted gradle configuration, then enabled caching and incremental compilation of Kotlin and Kapt.

If you use kapt in your project, use the latest version of kapt, which at the time of this writing is 1.3.31

2. Optimize app build.gradle

1. In the build.gradle file in the app directory of the project:

// Add the following configuration to kapt if necessary
kapt {
    useBuildCache = true
    javacOptions {
        option("-Xmaxerrs".500)}}/ / in the Android code block to add the following configuration: (can optimize the above transformClassDexBuilderForDebug time)
android {
    dexOptions {
        preDexLibraries true
        maxProcessCount 8}}Copy the code

2. Other less important optimizations seem to have less impact on time

Optimize the version number. If the debug version is used, do not use the dynamic version number

// defaultConfig {... minSdkVersion 19 targetSdkVersion 28 versionCode gitVersionCode() versionName currentName() ... } // Change to defaultConfig {... minSdkVersion 19 targetSdkVersion 28 versionCode 1 versionName"1.0.0". } applicationVariants.all { variant -> ...if (variant.buildType.name == "release") {
        versionName = currentName()
        versionCode = gitVersionCode()
    }
    ...
}
Copy the code

In our previous configuration, the versionCode used git submission times as the version number. In the local debug state, it is better to write the version number dead. If the version number changes, the Manifest file needs to be regenerated and the complete compilation application needs to be completed. It made InstantRun unavailable (PS we never used InstantRun). So change it to “write dead” version number and decide in applicationVariants that you will only use the normal version number if it is a Release. Another is that when using dependent versions, try not to use + version dependencies, using fixed version numbers will be faster.

I hope you can save compilation time and spend time with your family. Enjoy ~

References:

Developer.android.com/studio/buil…

www.kotlincn.net/docs/refere…

Blog.jetbrains.com/kotlin/2019…

Everything Put your journal, schedule, to-do list, and more into one App.

everythings.app/