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.453sSamsun SSD 850 EVO M.2
磁盤上,解包(Unpack)耗時 1.644s 性能提升 10倍以上
參考:
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
參考資料
-
Put
org.gradle.caching=true
in yourgradle.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' }
-
使用構建緩存加快干凈構建的速度 | Android Developers
使用 Android 插件 2.3.0 及更高版本的項目在默認情況下會啟用構建緩存(除非您明確停用構建緩存)。
-
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.
-
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
-
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線程數量}"也有一定作用
-
【譯】我每周在構建Gradle時是如何節約出5小時的 - 簡書
# Default value: -Xmx10248m -XX:MaxPermSize=256m org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8