gradle构建_如何提高Gradle的构建速度?

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构建

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值