解决AndroidStudio使用build过慢的问题--stackoverflow

本文介绍两种有效提升Gradle构建速度的方法:一是通过配置gradle.properties文件来启用Gradle Daemon及优化JVM参数;二是结合使用离线模式进一步提高构建效率。文章详细解释了配置步骤,并分析了Gradle运行缓慢的原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法地址: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

方法二:
配置gradle
这两个方法可以同时使用,那样你的studio就飞起来了。注意那个–offline的配置,无足轻重。可以不配置。
不同的版本可能配置略有不同,但大同小异,我的studio版本:
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值