Today I took APP's compilation speed was reduced nearly 5 times

Kotlin team uses quite a long time, has been less satisfactory kotlin compilation speed, but also can endure. Recently opened a new project, there are many of my colleagues came from java, they really can not stand, optimizing compiler speed becomes very important.

Before and after contrast optimization

Prior to optimize our first complete compilation time is 2 minutes 21 seconds

Today I took APP's compilation speed was reduced nearly 5 times

Specific time-consuming task in the Run Tasks:

Today I took APP's compilation speed was reduced nearly 5 times

It can be seen as a specific time-consuming tasks, mostly related to the compilation and kapt kotlin compiled code, and the last transformClassedWithXXX.

After a full compile-time optimizations 31s

Today I took APP's compilation speed was reduced nearly 5 times

Incremental compiler optimization time after 15s

Today I took APP's compilation speed was reduced nearly 5 timesToday I took APP's compilation speed was reduced nearly 5 times

Here we are not watching has started to address him, **, which also can! Do not worry, the following will take you carry out his together in a practice, good things must share is not it?

Optimization steps

1. Optimize gradle configuration:

Gradle.properties create a file in the root directory of the project

//开启gradle并行编译,开启daemon,调整jvm内存大小
org.gradle.daemon=true
org.gradle.configureondemand=true
org.gradle.parallel=true
org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=1024m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

//开启gradle缓存
org.gradle.caching=true
android.enableBuildCache=true

//开启kotlin的增量和并行编译
kotlin.incremental=true
kotlin.incremental.java=true
kotlin.incremental.js=true
kotlin.caching.enabled=true
kotlin.parallel.tasks.in.project=true //开启kotlin并行编译


//优化kapt
kapt.use.worker.api=true //并行运行kapt1.2.60版本以上支持
kapt.incremental.apt=true //增量编译 kapt1.3.30版本以上支持
//kapt avoiding 如果用kapt依赖的内容没有变化,会完全重用编译内容,省掉最上图中的:app:kaptGenerateStubsDebugKotlin的时间
kapt.include.compile.classpath=false

In the above configuration, we first adjusted the gradle configuration, and then opened the incremental compilation and caching of kotlin and kapt.

If the project uses kapt kapt Please use the latest version, the latest version of the current kapt write the articles for 1.3.31

2. Optimize the build.gradle app

1. Modify the file in the app directory build.gradle project in:

//如果有用到kapt添加如下配置
kapt {
useBuildCache = true
javacOptions {
option("-Xmaxerrs", 500)
}
}

//在Android代码块中添加如下配置:(可优化最上图中transformClassDexBuilderForDebug的时间)
android {
dexOptions {
preDexLibraries true
maxProcessCount 8
}
}

2. Other less important optimization, as if time is not particularly large impact on the

Optimize the allocation of the version number, if it is not to use the debug version of the dynamic version number

//原配置
defaultConfig {
...
minSdkVersion 19
targetSdkVersion 28
versionCode gitVersionCode
versionName currentName
...
}
//修改为
defaultConfig {
...
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0.0"
...
}
applicationVariants.all { variant ->
...
if (variant.buildType.name == "release") {
versionName = currentName
versionCode = gitVersionCode
}
...
}

The number of previously submitted versionCode on our configuration is used git as version number, when in fact the best local debug state is hard-coded version number, if the version number changes would result in the need to regenerate the Manifest file and compile a complete application, InstantRun cause can not be used (PS In fact, we have been useless InstantRun). So modified to write the version number of the dead, and then determine if the applicationVariants is only using the normal release of version number. Then there is the use of a version of the time-dependent, try not to use depend on the version of the + sign, fixed version will be faster.

I hope you saved compile time, be able to have fun with your family. If you feel good friends please help me some followers, your love is my greatest motivation -

 


Guess you like

Origin blog.51cto.com/13673213/2424672