Android Gradle 打包提速(二)

自從升級了Gradle 2.2.之后,發(fā)現(xiàn)打包速度明顯變慢。 WTF!
現(xiàn)在gradlew clean build大約要4分鐘才能完成,到底發(fā)生了什么事情呢?

1. 初現(xiàn)端倪

通過 gradlew build --profile 可以輸出打包過程的profile report。先看一下最初的Report文件:

image_1b2513724s5o1gp14u0h4r1a09.png-58.7kB
image_1b2513724s5o1gp14u0h4r1a09.png-58.7kB

image_1b2517dd1s51prt1r8q10q9184o9.png-87kB
image_1b2517dd1s51prt1r8q10q9184o9.png-87kB

發(fā)現(xiàn)有2個問題:
a. 每次build都會執(zhí)行l(wèi)int,而這個lint report目前來說對我們完全沒有用。
b. 我只是單純的想跑個Debug的build task,為何Release相關(guān)的Task也執(zhí)行了?

2. 禁用Lint

如果想禁用某個task可以直接
gradlew build -X lint -X lintVitalRelease

或者直接永久禁用lint:
在build.gradle 文件的

apply plugin: 'com.android.application'

之前添加以下代碼:

tasks.whenTaskAdded { task ->
    if (task.name.contains("lint")) {
        task.enabled = false
    }
}

3. Debug or Release

經(jīng)過測試發(fā)現(xiàn)gradlew build這個task執(zhí)行時,不區(qū)分Debug\Release , 所有類型的task都會執(zhí)行。所以我們盡量使用以下命令:

//命令這么長,記得用簡寫哦
gradlew assembleDebug 
gradlew assembleRelease
gradlew installDebug
gradlew installRelease

4. 還是慢啊

經(jīng)過測試,時間由3分45秒提升到1分45秒左右。但是為何還是這么慢!這個app:transformClassesWithDexForDebug是個什么鬼,這么慢。


image_1b253q8rbb5911qm1dps13rh1enmm.png-73.1kB
image_1b253q8rbb5911qm1dps13rh1enmm.png-73.1kB

經(jīng)過調(diào)研發(fā)現(xiàn)這個app:transformClassesWithDexForDebug是打dex文件的核心task,要讓它提高速度,我們需要做一下處理:

dexOptions {
  preDexLibraries true
  javaMaxHeapSize "2g"
}

順帶解決下這個提示的問題吧:


image_1b25cahhrq8113ktkip1nrpu8j2a.png-71.3kB
image_1b25cahhrq8113ktkip1nrpu8j2a.png-71.3kB

5. 最后的總結(jié)

image_1b25bf4i11e8s1aiqi062v0rgg1t.png-78.1kB
image_1b25bf4i11e8s1aiqi062v0rgg1t.png-78.1kB

呵呵,還只是比最開始快2分鐘多一點。這些profile都是clean后做的Build。修改之后病歷夾clean之后點擊運行一共需要一分半吧。


image_1b25f6uij65pmdd1n921btkeks9.png-9.3kB
image_1b25f6uij65pmdd1n921btkeks9.png-9.3kB

其實日常改代碼經(jīng)常是走的Incremental Build而非全量Build,因為gradle對Incremental Build有一些優(yōu)化,它的耗時從20秒到3分半鐘都有可能,要做測試相對困難一些,總體來說經(jīng)過以上的配置,速度還是快一些的~ 至于你信不信,試了就知道嘍....

6. gradle.properties 文件也show一下吧

# The Gradle daemon aims to improve the startup and execution time of Gradle.
# When set to true the Gradle daemon is to run the build.
org.gradle.daemon=true

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
org.gradle.jvmargs=-Xmx2560m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true

# Enables new incubating mode that makes Gradle selective when configuring projects.
# Only relevant projects are configured which results in faster builds for large multi-projects.
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
org.gradle.configureondemand=true

7. 寫在刨坑之后

產(chǎn)品中使用了SonarQube做代碼質(zhì)量的監(jiān)控。直接禁用lint會導(dǎo)致SonarQube的異常。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容