First, the most effective way
Check that the Gradle plugin version you are using is up to date. If not, upgrade to the latest version of the Gradle plug-in.
Gradle has always been optimized for build speed during the upgrade process. Upgrading to the latest Gradle version can increase build speed by 80%, which is the most effective method.
Gradle configuration items
1, the Gradle Daemon
Start the Gradle daemon to build your project:
org.gradle.daemon=true
Parallel Project Execution
If you are building a multi-module project with complex dependencies, you can use parallel project execution:
org.gradle.parallel=true
3, Global gradle. The properties
Properties configured in the Gradle home directory take precedence over properties configured in the project. If you do not want to change the configuration project by project, you can define a common Gradle configuration file and place it in the Gradle home directory. This path is as follows:
/ Users/username /. Gradle/gradle. Properties
#Enable daemon
org.gradle.daemon=true
# Try and findout the best heap size for your project build.
org.gradle.jvmargs=-Xmx5120m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# Modularise your project and enable parallel build
org.gradle.parallel=true
# Enable configure on demand.
org.gradle.configureondemand=true
Copy the code
Eliminate time-consuming tasks
Run the following command in Terminal of the project:
gradle build -profile
Copy the code
You can get the build report, which is presented in HTML.
Based on the above optimizations, let’s take a look at the current build report:
First execution
The first time was slow, at 5 minutes and 11 seconds, because the cache was not built the first time.
Note: If Gradle detects changes in configuration files such as gralde.properties, build. Gradle, etc., it will restart the build and take longer.
Second execution
That’s a big improvement over the first time, about 1 minute 26 seconds. Let’s take a look at where the minute and 26 seconds are used:
In Task Execution, you can see that the longest running lint Task in each project is actually the Lint Task. A Lint task is a static code detection task that is executed by default whether you use Lint or not.
Since we did not use lint in our project, we removed the lint task by adding the -x lint parameter to the build command, i.e
gradle build -x lint
Copy the code
Third execution
You can see that after removing the Lint task, the build time was an acceptable 26 seconds.
Gradle builds in 26 seconds, down from an average of 4 minutes, with an optimization rate of 89%.
Tips: You can’t remove Lint tasks using the Android Studio taskbar by simply clicking the Run button, so to make it easier to install your app after you build, use the gradle installDebug -x lint command.
Four, conclusion
Gradle plugins are designed to speed up Gradle builds, so it is highly recommended to upgrade Gradle plugins to maximize build speed. Gradle plugins are designed to speed up Gradle builds. Gradle plugins are designed to speed up Gradle builds. You can build a project in 40 seconds.