17个小技巧让你的Gradle构建速度飞起来!

保持你的工具是最新的

Android工具包支持的构建往往是最新的,所以官方的一些建议和技巧往往都是针对于最新版本的。所以,为了充分利用最新的定制化,保持一下工具是最新的:
Android Studio and SDK tools
The Android plugin for Gradle

创建变体

当你开发你的app的时候,发布版本的gradle当中的很多配置完全是不需要的。启用不必要的构建进程会降低你的构建速度,所以通过配置build variant 可以在你开发app时构建你所需要的配置。下面的例子是创建一个dev版本的和一个prod版本:

android {
  ...
  defaultConfig {...}
  buildTypes {...}
  productFlavors {
    // When building a variant that uses this flavor, the following configurations
    // override those in the defaultConfig block.
    dev {
      // To avoid using legacy multidex when building from the command line,
      // set minSdkVersion to 21 or higher. When using Android Studio 2.3 or higher,
      // the build automatically avoids legacy multidex when deploying to a device running
      // API level 21 or higher—regardless of what you set as your minSdkVersion.
      minSdkVersion 21
      versionNameSuffix "-dev"
      applicationIdSuffix '.dev'
    }

    prod {
      // If you've configured the defaultConfig block for the release version of
      // your app, you can leave this block empty and Gradle uses configurations in
      // the defaultConfig block instead. You still need to create this flavor.
      // Otherwise, all variants use the "dev" flavor configurations.
    }
  }
}

避免编译不需要的资源

android {
  ...
  productFlavors {
    dev {
      ...
      // The following configuration limits the "dev" flavor to using
      // English stringresources and xxhdpi screen-density resources.
      resConfigs "en", "xxhdpi"
    }
    ...
  }
}

关掉Crashlytics对于debug构建

android {
  ...
  buildTypes {
    debug {
      ext.enableCrashlytics = false
    }
}

使用静态构建配置

int MILLIS_IN_MINUTE = 1000 * 60
int minutesSinceEpoch = System.currentTimeMillis() / MILLIS_IN_MINUTE

android {
    ...
    defaultConfig {
        // Making either of these two values dynamic in the defaultConfig will
        // require a full APK build and reinstallation because the AndroidManifest.xml
        // must be updated (which is not supported by Instant Run).
        versionCode 1
        versionName "1.0"
        ...
    }

    // The defaultConfig values above are fixed, so your incremental builds don't
    // need to rebuild the manifest (and therefore the whole APK, slowing build times).
    // But for release builds, it's okay. So the following script iterates through
    // all the known variants, finds those that are "release" build types, and
    // changes those properties to something dynamic.
    applicationVariants.all { variant ->
        if (variant.buildType.name == "release") {
            variant.mergedFlavor.versionCode = minutesSinceEpoch;
            variant.mergedFlavor.versionName = minutesSinceEpoch + "-" + variant.flavorName;
        }
    }
}

使用静态依赖版本

当你使用版本依赖在build.gradle中,你应该避免使用带+号的本本,例如'com.android.tools.build:gradle:2.+'.。使用动态的版本号可能会引起无法预期的版本更新,增加解决版本差异的难度,并且由于Gradle检查更新会降低构建速度。所以你应该使用静态的版本数字号来代替。

打开Offline模式

17个小技巧让你的Gradle构建速度飞起来!_第1张图片
QQ20170613-115423.png
  1. 打开Preferences,点击File>Settings.
  2. 左边面板,点击Build,Execution,Deployment>Gradle.
  3. 勾选offline work选项.
  4. 点击Apply 或者 Ok.https://docs.gradle.org/current/userguide/more_about_tasks.html

打开配置

17个小技巧让你的Gradle构建速度飞起来!_第2张图片
configure on demand.png
  1. 打开Preferences,点击File>Settings.
  2. 左边面板,点击Build,Execution,Deployment>Compiler.
  3. 勾选Configure on demand项.
  4. 点击Apply 或者 Ok.

创建library模块

找出你app中可以模块化的代码。模块化你的代码可以令构建系统只编译你修改的modules并且为未来的构建缓存。它还可以令 configuration on demand& parallel project execution更加有效率。

创建Task对于自定义构建逻辑

当你构建项目的时候,如果你发现构建log日志中出现了很长时间的Configuring Projects,这时候回顾一下build.gradle文件中的语法,然后找出你可以引入到一个自定义的Gradle task中。这样的好处是,构建的结果可以被缓存。了解更多,阅读official Gradle documentation。

配置dexOptions打开pre-dexing

Android plugin 提供了dexOptions block,所以你可以配置可以提高构建速度的DEX build属性:

  • preDexLibraries
  • maxProcessCount
  • javaMaxHeapSize
    例如:
android {
  ...
  dexOptions {
    preDexLibraries true
    maxProcessCount 8
    // Instead of setting the heap size for the DEX process, increase Gradle's
    // heap size to enable dex-in-process. To learm more, read the next section.
    // javaMaxHeapSize "2048m"
  }
}

增加Gradle的堆内存大小

org.gradle.jvmargs = -Xmx2048m

转换images成WebP

WebP是一种提供有损压缩(类似JPEG)当然也有透明度(类似PNG),但是能够提供更好的压缩性能比起JPEG和PNG。在未执行构建时间内的压缩图片来达到减少图片文件大小,并且可以加速你的构建速度,特别是当你的app使用了很多个图片资源。然而,你也许会注意到设备CPU的使用量会有一小段上升当解压WebP图片。使用Android Studio,你就可以很容易的使用 convert your images to WebP。

禁用PNG crunching

如果你不想把你的PNG图片转换成WebP,你仍然可以在每次build你的应用的时候,通过图片自动压缩加速你的构建速度。

android {
  ...
  aaptOptions {
    cruncherEnabled false
  }
}

使用Instant Run

Instant Run能够很明显的减少构建时间。在某种情况下,甚至可以不用重启的当前的Activity。使用Instant Run通过点击Apply Changes当你的app发生改变之后,并且当你使用如下操作时它默认是打开的:

  • 使用debug build variant
  • 使用Android plugin for Gradle版本2.3或者更高
  • minSdkVersion设置成15或者更高版本
  • 把你的应用运行在Android 5.0(API level 21)或者更高的版本上
    如果你在工具栏上看不到Apply Changes按钮,确保你在IDE中没有关掉通过以下步骤:
  1. 打开Settings or Preferences
  2. 导航到Build,Execution,Deployment > Instant Run.
  3. 确保Enable Instant Run是勾选的。

使用Build Cache

Build Cache存储了当构建时Android Plugin Gradle生成的一些未解压的AARspre-dexed remote dependencies输出。当使用了高速缓存cache之后你的clean build速度会大大加快,因为构建系统可以在一连串的构建中通过不重新创建他们的情况下复用这些缓存文件。

禁用注解annotation

当使用注解时Incremental Java compilation是不可用的。所以尽量避免使用注解,这样你就可以在构建的时候只编译你修改的classes文件这样的方式来获取好处。

剖析你的build

很多大型的项目,或者是一些实现了自己的构建逻辑,也许需要你仔细的观察构建过程并找到瓶颈所在。你可以分析在build生命周期中的每一个阶段和构建任务中Gradle所花费的时间。

你可能感兴趣的:(17个小技巧让你的Gradle构建速度飞起来!)