复制官网的文章部分。记录下
Improve build times by configuring DEX resources
When you deploy a clean build, Android Studio instruments your app to allow Instant Run to push code and resource updates. Although updating the running app happens much more quickly, the first build may take longer to complete. You can improve the build process by configuring a fewDexOptionssettings:
Sets the maximum number of DEX processes that can be started concurrently. If the Gradle daemon is already running, you need to stop the process before initializing it with a new maximum process count. You can terminate the Gradle daemon by calling one of the following from theTerminalwindow:
On Windows, callgradlew --stop
On Linux/Mac OSX, call./gradlew --stop
Sets the maximum memory allocation pool size for the dex operation. When passing a value, you can append the letter 'k' to indicate kilobytes, 'm' to indicate megabytes, or 'g' to indicate gigabytes.
The following example setsmaxProcessCountto 4 andjavaMaxHeapSizeto "2g" in the module-levelbuild.gradlefile:
android{
...
dexOptions{
maxProcessCount4// this is the default value
javaMaxHeapSize"2g"
}
}
You should experiment with these settings by incrementing their values and observing the effect on your build times. You could experience a negative impact to performance if you allocate too many resources to the dexing process.
Enable dexing-in-process and incremental Java compilation
Android Plugin for Gradle version 2.1.0and higher features additional build process improvements, including incremental Java compilation and dexing-in-process. Incremental Java compilation is enabled by default and reduces compilation time during development by only recompiling portions of the source that have changed or need to be recompiled.
Dexing-in-process performs dexing within the build process rather than in a separate, external VM process. This not only makes incremental builds much faster, but also significantly speeds up full builds. To enable this feature, you need to set the Gradle daemon's maximum heap size to at least 2048 MB. You can do this by including the following in your project'sgradle.propertiesfile:
org.gradle.jvmargs=-Xmx2048m
If you have defined a value forjavaMaxHeapSizein your module-levelbuild.gradlefile, you need to set the daemon's max heap size to the value ofjavaMaxHeapSize+ 1024 MB. For example, if you have setjavaMaxHeapSizeto "2g", you need to add the following to your project'sgradle.propertiesfile:
org.gradle.jvmargs=-Xmx3072m
Exclude your project from Windows Defender