gradle构建

In this tutorial we’ll look at things that can be done with the Gradle build to speed up the build time.

在本教程中,我们将介绍Gradle构建可以加快构建时间的事情。

加快您的Android Gradle构建 (Speed up your Android Gradle Build)

As our Android Studio project size increases, the gradle build performance becomes critical. The gradle speed for even the simplest project is pretty slow. Though every project comes up with it’s own complexity and uniqueness which causes it to have a different build speed. Nevertheless one thing that is pretty common in regard to the build speeds is that it takes our precious time that in return hampers our productivity. A few basic tricks can help us save those extra seconds per build and that makes a big difference to the productivity.

随着我们Android Studio项目规模的增加,gradle构建性能变得至关重要。 即使最简单的项目,gradle的速度也相当慢。 尽管每个项目都有其自身的复杂性和独特性,这导致其具有不同的构建速度。 然而,关于构建速度非常普遍的一件事是,这花费了我们宝贵的时间,反过来又阻碍了我们的生产率。 一些基本的技巧可以帮助我们节省每次构建的额外时间,这对生产率有很大的影响。

  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.

    注意 :Java 1.8快于1.6。 确保它也已更新。

  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.尽量减少模块的使用。 在很多情况下,我们需要派生库来对其进行修改以适应我们的需求。 一个模块比jaraar依赖项花费的时间长4倍。 发生这种情况是由于每次都需要从头开始构建模块。
  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.从首选项->构建,执行,部署->构建工具-> Gradle启用gradle离线工作。 这将不允许gradle在构建过程中访问网络,并强制其从缓存本身解析依赖性。
    注意:仅当所有依赖项都一次下载并存储在缓存中后,此方法才有效。 如果您需要修改或添加新的依赖项,则必须禁用此选项,否则构建将失败。
  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 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

    Another important property is;

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

    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.

    This is how the gradle.properties file should look like:

    gradle.properties文件。 在其中添加以下代码行。

    org.gradle.daemon=true

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

    org.gradle.parallel=true

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

    • 重新使用配置以进行未更改的项目
    • 项目级别是最新检查
    • 使用预先构建的工件代替构建依赖项目

    添加以下代码行也有助于我们加快构建速度。

    org.gradle.configureondemand=true

    另一个重要特性是;

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

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

    这是gradle.properties文件的外观:

  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.

    compile 'com.google.maps.android:android-maps-utils:0.4+'

    由于动态依赖项每次都会不断搜索最新的版本,因此它们会减慢您的生成速度。 为了提高性能,我们需要修复该版本。

  6. Use only those dependencies that you need. For example google maps dependency, instead of importing
    compile 'com.google.android.gms:play-services:8.4.0' just import
    compile 'com.google.android.gms:play-services-maps:8.4.0'.仅使用您需要的那些依赖项。 例如,谷歌地图依赖,而不是导入
    compile 'com.google.android.gms:play-services:8.4.0'只需导入
    compile 'com.google.android.gms:play-services-maps:8.4.0'

Bringing such tweaks into use in our project saves a lot of time in the long run. I hope these gradle build tips will help you in improving your project build time.

从长远来看,在我们的项目中使用这些调整可以节省大量时间。 我希望这些gradle构建技巧可以帮助您缩短项目构建时间。

翻译自: https://www.journaldev.com/12333/increase-gradle-build-speed

gradle构建

gradle构建_如何提高Gradle的构建速度?相关推荐

  1. docker容器构建_我如何容器化构建系统

    docker容器构建 构建系统由用于从源代码过渡到正在运行的应用程序的工具和过程组成. 这种过渡还涉及将代码的读者从软件开发人员更改为最终用户,无论最终用户是运营方面的同事还是部署系统的同事. 在使用 ...

  2. autocad完全应用指南_如何提高CAD画图的速度?有哪些途径和技法?【AutoCAD教程】...

    点击蓝色字关注我哟 ☀每天推送CAD软件安装及下载.CAD工具应用.CAD技巧.CAD教程.CAD素材.CAD疑难问题解答等文章 ☀想认识志同道合的朋友一起学习CAD?请加入我们的QQ群 369845 ...

  3. gradle 安装_如何安装Gradle

    gradle 安装 Gradle是一个依赖管理/构建工具,结合了Maven和Ant的优点,使其成为功能强大且可自定义的工具. 它还使用了光滑的Groovy DSL而不是Maven和Ant的XML方法, ...

  4. 大数据平台构建_如何像产品一样构建数据平台

    大数据平台构建 重点 (Top highlight) Over the past few years, many companies have embraced data platforms as a ...

  5. 语音库构建_在10分钟内构建一个多功能语音助手

    语音库构建 Nowadays people don't have time to manually search the internet for information or the answers ...

  6. 模型训练平台的构建_用5行代码构建自定义训练的对象检测模型

    模型训练平台的构建 如今,机器学习和计算机视觉已成为一种热潮. 我们都已经看到了有关自动驾驶汽车和面部识别的新闻,并且可能想象到建立我们自己的计算机视觉模型将会多么酷. 但是,进入该领域并不总是那么容 ...

  7. cjson构建_[置顶] cJSON库(构建json与解析json字符串)-c语言

    一.c语言获取json中的数据. 1.先要有cJOSN库,两个文件分别是cJSON.c和cJSON.h. 2.感性认识 char * json = "{ \"json\" ...

  8. 命令构建gradle项目_【Android 修炼手册】Gradle 篇 -- Gradle 源码分析

    预备知识 理解 gradle 的基本开发 了解 gradle task 和 plugin 使用及开发 了解 android gradle plugin 的使用 看完本文可以达到什么程度 了解 grad ...

  9. java gradle构建_在Gradle中为JPMS构建Java 6-8库

    java gradle构建 通过提供Java 9 module-info.class了解如何使用Gradle构建支持JPMS( Java平台模块系统 )的Java 6-8库. 介绍 如果您需要JPMS ...

最新文章

  1. Pentaho Data Integration 界面开发
  2. DataTables实现rowspan思路
  3. android 能自动选择的listview,Android ListView多选模式
  4. qt开发环境 - c++字符串操作
  5. mysql innodb缓存策略之Buffer Pool
  6. 深入理解 Node.js 中 EventEmitter源码分析(3.0.0版本)
  7. Gtk实现GUI键盘并终端显示
  8. LeetCode Convert Sorted List to Binary Search Tree 解题报告
  9. 抄答案就是了,两套详细的设计方案,解决头疼的支付掉单问题
  10. 小组作业:学生信息管理系统需求分析
  11. 【JavaWEB】Redis基础
  12. CTC blank 理解
  13. 风变编程python小课课件_待风变编程的 Python 网课是什么? Python 网课怎么
  14. win7桌面右下角的音量图标不见了怎么办
  15. matplotlib命令与格式:系统字体与显示中文
  16. 优化算法(四)——粒子群优化算法(PSO)
  17. 第二证券|A股集体收涨,汽车产业链爆发!房地产延续强势
  18. Wampserver 如何连接远程数据库,云数据库
  19. java计算机毕业设计旅游景点推荐系统源码+系统+mysql数据库+lw文档
  20. 2022年高处安装、维护、拆除培训试题模拟考试平台操作

热门文章

  1. js判断变量类型是否为字符串,不符合条件则赋值为‘无’
  2. 数据结构之散列表实现
  3. nodejs初探(四)实现一个多人聊天室
  4. 关于公司通过CMMI3级认证
  5. 关于asp.net上传图片自动生成缩略图
  6. [转载] Python性能优化技巧总结
  7. [转载] 卷积神经网络做mnist数据集识别
  8. 如何实现一个Servlet中的多个功能
  9. 迷人的bug--torch.load
  10. springboot-mybatis-多数据源