添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Android如何提升Gradle编译速度或减少Gradle编译时间

最终优化方案

优化效果比对

优化前:随便修改一行代码,点击"Build - make project" 编译就需要 15~28秒之间

优化后:随便修改一行代码,点击"Build - make project" 编译只需8-10秒左右,假如修改的代码曾经编译过,能命中缓存,那么基本在3~4秒即可编译完毕。

开发机相关配置

Infrastructure
Operating system   Windows 7 6.1
CPU cores          8 cores
Max Gradle workers 8 workers
Java runtime JetBrains s.r.o OpenJDK Runtime Environment 1.8.0_152-release-1343-b01
Java VM      JetBrains s.r.o OpenJDK 64-Bit Server VM 25.152-b01 (mixed mode)
Max JVM memory heap size 9544 MB

将所有项目源码,各种缓存临时目录都移动到高性能SSD磁盘上

  • TOSHIBA- TR200 虽然是SSD,但是明显不够高性能(虽然可能比一般的机械可能会稍微好一点)
  • gradle.properties 配置

    #因为电脑有20G内存,所以在原来的基础上直接乘与5倍,足够同时打开3个以上项目了。
    org.gradle.jvmargs=-Xmx5120m -XX:MaxPermSize=1280m -Dfile.encoding=UTF-8
    org.gradle.daemon=true
    org.gradle.configureondemand=true
    org.gradle.parallel=true
    org.gradle.caching=true
    #假如不启动会报错: Unable to get provider com.squareup.leakcanary.internal.LeakCanaryFileProvider: java.lang.ClassNotFoundException
    android.useAndroidX=true
    android.enableJetifier=true
    android.enableBuildCache=true
    

    碰到的问题

    3. Android Studio 设置里勾选了自动编译功能却无效的问题?

  • 关于IDEA不能实时编译的一个临时解决办法。。。。 - MyHome - OSCHINA
  • idea初使用之自动编译 - 野蜂的博客 - CSDN博客
  • Android Studio : auto build like Eclipse - Stack Overflow
  • "Make project automatically" - only while not running/debugging – IDEs Support (IntelliJ Platform) | JetBrains
  • 2. 在 Gradle Scan报告里 Timeline - FROM-CACHE - Build cache result - Unpack 时间耗时很长怎么办?

    编译缓存(Build Cache)目录所在磁盘IO性能过低,将其设置到高效能的SSD固态磁盘即可大幅度提升解包(Unpack)速度(降低解包耗时).

    同样的缓存大小: "Cache artifact 3.62 MB / 4447 entries"

    缓存目录放在

  • TOSHIBA- TR200 磁盘上,解包(Unpack)耗时 13.453s
  • Samsun SSD 850 EVO M.2 磁盘上,解包(Unpack)耗时 1.644s 性能提升 10倍以上
  • gradle.org - Build Cache
  • 1. 按照 参考资料: Android项目中开启Gradle的build scan(构建审视) - 简书 步骤来做,然后Sync时,报错

    Gradle sync failed: Could not find com.gradle:build-scan-plugin:1.8.
            Searched in the following locations:
            Required by:
            project : (2 s 761 ms)
    

    在项目根目录里增加以下代码:

    buildscript { repositories { maven { url 'https://plugins.gradle.org/m2' } dependencies { classpath 'com.gradle:build-scan-plugin:2.3' apply plugin: com.gradle.scan.plugin.BuildScanPlugin buildScan { termsOfServiceUrl = "https://gradle.com/terms-of-service"; termsOfServiceAgree = "yes"

    详情参考: Gradle Build Scan Plugin User Manual | Gradle Enterprise Docs

    Build Cache

    Put org.gradle.caching=true in your gradle.properties

    Android项目中开启Gradle的build scan(构建审视) - 简书

    A fine-grained performance profile is available: use the --scan option.

    buildscript {
        dependencies {
            #下面这句是添加的
            classpath 'com.gradle:build-scan-plugin:1.8' 
    #下面5行是添加的
    apply plugin: 'com.gradle.build-scan'   
    buildScan {                    
        licenseAgreementUrl = 'https://gradle.com/terms-of-service'
        licenseAgree = 'yes'
    

    How does android.enableBuildCache=true functions in new Android Studio 2.2? - Stack Overflow

  • Step 0:Ensure that android.dexOptions.preDexLibraries is either not set or set to true;
  • Step 1: android.enableBuildCache=true
  • Step 2: Build your Android project and check the following locations to see whether the build cache took effect. By default the cache-dir is /.android/build-cache.
  • 一些关于加速Gradle构建的个人经验 - 为程序员服务

    org.gradle.jvmargs=-Xmx5120m -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
    gradlew clean --info 会得到类似这样的信息,信息标明了开启Parallel以及每个task使用的线程信息

    How to decrease your Gradle build time by 65%? - Keval Patel - Medium

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

    gradlew android:assembleDebug --profile --configure-on-demand --daemon

    如何加快Gradle编译项目的速度? - 简书

    org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=1024m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
    org.gradle.daemon=true
    org.gradle.parallel=true
    android.enableBuildCache=true
    另外在Settings-Build,Execution,Deployment-Commond-line Options中设置"--parallel-threads={cpu线程数量}"也有一定作用