30 seconds to accelerate Android Studio/Gradle Construction
Android Studio has been used for development for more than a year. As the project grows and the dependent libraries increase, the build speed slows down. Now, it takes six minutes to build a release installation package, search for information on the Internet and find that you can use some configurations to speed up the process. I would like to share with you here.
Enable separate daemon of gradle
Create the gradle. properties file under the following directory:
- /Home/<username>/. gradle/(Linux)
- /Users/<username>/. gradle/(Mac)
- C: \ Users \ <username> \. gradle (Windows)
Add the following content to the file:
- org.gradle.daemon=true
You can also modify the gradle. properties file under the project to optimize it:
- # Project-wide Gradle settings.
-
- # IDE (e.g. Android Studio) users:
- # Settings specified in this file will override any Gradle settings
- # configured through the IDE.
-
- # For more details on how to configure your build environment visit
- # http://www.gradle.org/docs/current/userguide/build_environment.html
-
- # The Gradle daemon aims to improve the startup and execution time of Gradle.
- # When set to true the Gradle daemon is to run the build.
- # TODO: disable daemon on CI, since builds should be clean and reliable on servers
- org.gradle.daemon=true
-
- # Specifies the JVM arguments used for the daemon process.
- # The setting is particularly useful for tweaking memory settings.
- # Default value: -Xmx10248m -XX:MaxPermSize=256m
- org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
-
- # When configured, Gradle will run in incubating parallel mode.
- # This option should only be used with decoupled projects. More details, visit
- # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
- org.gradle.parallel=true
-
- # Enables new incubating mode that makes Gradle selective when configuring projects.
- # Only relevant projects are configured which results in faster builds for large multi-projects.
- # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
- org.gradle.configureondemand=true
At the same time, these parameters can be configured in the gradle. properties file in the preceding user directory. This will not take effect for a project, but for all projects.
The above configuration file is mainly used to increase the size of the Java virtual machine running gradle, so that gradle can use an independent process during compilation, so that gradle can run in parallel.
Modify android studio configurations
In the configuration of android studio, enable the offline mode and modify the configuration. In fact, the configuration is the same as the previous section. The configuration in this section will only take effect during ide build, and the command line build will not take effect.
Command Line Construction
Based on the preceding configuration, you can add this parameter after the command when constructing a command line.--daemon --parallel --offline
.
Use aar when introducing dependency Libraries
When using third-party dependent libraries on the internet, try to use aar. You can search by using mavenhttp: // gradleplease.appspot.com/?githuhttps://github.com/goddchen/mvn-reposearch.
You can also package your own library modules into aar. For more information, see stormzhang's article http://www.stormzhang.com/android/2015/03/01/android-reference-local /.
Postscript
After such a hard job, it took me four or five minutes to build the project. Now it takes about 30 seconds to build the project. Of course, if you still cannot do this, you should replace the computer, ssd hard disk, and larger memory.