Android Studio的构建速度很慢 [英] Android studio is slow in gradle building

查看:775
本文介绍了Android Studio的构建速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Studio的成绩建设过程越来越慢,我在更新到新版本3.5后注意到了这个问题,有什么方法可以解决吗? 加快建设过程?

Android studio is getting slow in grade building process.I noticed this problem after updating to new version 3.5.Is there any ways to speed up the building process?

推荐答案

1-确保您使用的是最新版本的Gradle.通常,每次进行新更新都会大大提高性能. 注意:Java 1.8快于1.6.确保它也已更新.

1- Make sure you’re using the latest version of Gradle. Generally with every new update there is a significant improvement in performance. Note: Java 1.8 is faster than 1.6. Make sure it’s updated too.

2-尝试最小化模块的使用.在很多情况下,我们需要派生库来对其进行修改以适应我们的需求.模块花费的时间比jar或aar依赖项大4倍.发生这种情况的原因是每次都需要从头开始构建模块.

2- Try to minimize the use of modules. There are many cases where we need to fork the library to modify it to fit according to our needs. A module takes 4x greater time than a jar or aar dependency. This happens due to the fact that the module needs to be built from the scratch every time.

3-从Preferences-> Build, Execution, Deployment-> Build Tools-> Gradle启用gradle脱机工作.这将不允许gradle在构建期间访问网络,并强制其从缓存本身解析依赖性.

3- Enable gradle Offline Work from Preferences-> Build, Execution, Deployment-> Build Tools-> Gradle. This will not allow the gradle to access the network during build and force it to resolve the dependencies from the cache itself.

注意:仅当所有依赖项都已下载并且 一次存储在缓存中.如果您需要修改或添加新的 依赖关系,您必须禁用此选项,否则构建将 失败.

Note: This only works if all the dependencies are downloaded and stored in the cache once. If you need to modify or add a new dependency you’ll have to disable this option else the build would fail.

4-从项目的根目录打开gradle.properties文件.在其中添加以下代码行.

4-Open up the gradle.properties file from the root of your project. Add the following lines of code in it.

org.gradle.daemon=true

Gradle守护程序是后台进程.添加它会在构建时消耗一些额外的内存.

Gradle daemon is a background process. Adding this would consume some extra memory while building.

org.gradle.parallel=true

上面的代码行允许同时编译多个模块.除此之外,它还给我们带来了其他好处,例如;

The above line of code enables compilation of multiple modules at the same time. Besides that it also gives us other benefits such as;

重新使用配置以进行未更改的项目 项目级别是最新检查 使用预先构建的工件代替构建依赖项目 添加以下代码行也有助于我们加快构建速度.

Re-using the configuration for unchanged projects Project-level is up-to-date checks Using pre-built artifacts in the place of building dependent projects Adding the following line of code also aids us in speeding up the build.

org.gradle.configureondemand=true

另一个重要属性是;

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

以上行用于允许Java编译器具有高达2 GB(2048 MB)的可用内存.仅当可用内存超过2 GB时,才应使用它.

The above line is used to allow Java compilers to have available memory up to 2 GB (2048 MB). It should only be used if you have available memory more than 2 GB.

这是gradle.properties文件的外观:

5-避免动态依赖性,例如 编译"com.google.maps.android:android-maps-utils:0.4+". 由于动态依赖项每次都会不断搜索最新的版本,因此它们会减慢您的生成速度.为了提高性能,我们需要修复该版本.

5- Avoid dynamic dependencies such as compile 'com.google.maps.android:android-maps-utils:0.4+'. Dynamic Dependencies slow down your build since they keep searching for the latest builds every time. To improve the performance we need to fix the version in place.

6-仅使用所需的依赖项.例如google maps依赖,而不是import,例如:

6- Use only those dependencies that you need. For example google maps dependency, instead of importing , like :

 implementation 'com.google.android.gms:play-services:17.0.0' 
 implementation 'com.google.android.gms:play-services-maps:17.0.0'

这篇关于Android Studio的构建速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆