加速Android Studio/Gradle构建

已经使用Android Studio进行开发超过一年,随着项目的增大,依赖库的增多,构建速度愈来愈慢,如今最慢要6分钟才能build一个release的安装包,在网上查找资料,发现能够经过一些配置能够加快速度,这里跟你们分享一下。 html

开启gradle单独的守护进程

在下面的目录下面建立gradle.properties文件: java

  • /home/<username>/.gradle/ (Linux) android

  • /Users/<username>/.gradle/ (Mac) jvm

  • C:\Users\<username>\.gradle (Windows) ide

并在文件中增长: gradle

1
org.gradle.daemon=true

同时修改项目下的gradle.properties文件也能够优化: 优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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
# 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
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.
org.gradle.configureondemand=true

同时上面的这些参数也能够配置到前面的用户目录下的gradle.properties文件里,那样就不是针对一个项目生效,而是针对全部项目生效。 ui

上面的配置文件主要就是作, 增大gradle运行的java虚拟机的大小,让gradle在编译的时候使用独立进程,让gradle能够平行的运行。 this

修改android studio配置

在android studio的配置中,开启offline模式,以及修改配置。实际上的配置和上面的一大段同样,主要是在这个地方配置的只会在ide构建的时候生效,命令行构建不会生效。 spa