方法地址:http://stackoverflow.com/questions/16775197
该方法对小项目的速度提升可能不太明显。
简单的翻译一下:
方法一:添加配置文件,找到下面的目录:
/home/<username>/.gradle/ (Linux)
/Users/<username>/.gradle/ (Mac)
C:\Users\<username>\.gradle (Windows)
在相应的目录下,创建文件:gradle.properties,如果你只想在某一个项目中用这种配置,你可以取而代之将该文件放在项目的根目录下面。
并添加以下内容:
# 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
org.gradle.daemon=true
方法二:
这两个方法可以同时使用,那样你的studio就飞起来了。注意那个–offline的配置,无足轻重。可以不配置。
不同的版本可能配置略有不同,但大同小异,我的studio版本:
原因分析
Chapter 6. The Gradle Daemon这是一篇关于Daemon的官方文档。以上提高gradle运行过慢的原理就是开启了Daemon。这个后台程序。
Gradle runs on the Java Virtual Machine (JVM) and uses several supporting libraries that require a non-trivial initialization time. As a result, it can sometimes seem a little slow to start. The solution to this problem is the Gradle Daemon: a long-lived background process that executes your builds much more quickly than would otherwise be the case. We accomplish this by avoiding the expensive bootstrapping process as well as leveraging caching, by keeping data about your project in memory. Running Gradle builds with the Daemon is no different than without. Simply configure whether you want to use it or not - everything else is handled transparently by Gradle.
Gradle在java虚拟机上运行并且使用了一些支持库,这些就浪费了一些初始化的时间。最终导致了,看起来启动很慢的样子。这个问题的解决方案就是开启Gradle Daemon:一个长生命周期的后台进程,使得build的过程变得比先前更快一些。这是一个空间换时间的例子,虽然提高了速度,但是对内存的要求较高。而使用Daemon运行gradle与不使用所得到的结果无异。简单的配置就可以完成这一切。
org.gradle.daemon=true