標籤:
已經使用Android Studio進行開發超過一年,隨著項目的增大,依賴庫的增多,構建速度越來越慢,現在最慢要6分鐘才能build一個release的安裝包,在網上尋找資料,發現可以通過一些配置可以加快速度,這裡跟大家分享一下。
開啟gradle單獨的守護進程
在下面的目錄下面建立gradle.properties檔案:
/home/<username>/.gradle/ (Linux)
/Users/<username>/.gradle/ (Mac)
C:\Users\<username>\.gradle (Windows)
並在檔案中增加:
org.gradle.daemon=true
同時修改項目下的gradle.properties檔案也可以最佳化:
# Project-wide Gradle settings.# IDE (e.g. Android Studio) users:# Settings specified in this file will override any Gradle settings# configured through the IDE.# For more details on how to configure your build environment visit# http://www.gradle.org/docs/current/userguide/build_environment.html# 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.# TODO: disable daemon on CI, since builds should be clean and reliable on serversorg.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=256morg.gradle.jvmargs=-Xmx2048m -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_projectsorg.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_demandorg.gradle.configureondemand=true
同時上面的這些參數也可以配置到前面的使用者目錄下的gradle.properties檔案裡,那樣就不是針對一個項目生效,而是針對所有項目生效。
上面的設定檔主要就是做, 增大gradle啟動並執行java虛擬機器的大小,讓gradle在編譯的時候使用獨立進程,讓gradle可以平行的運行。
修改android studio配置
在android studio的配置中,開啟offline模式,以及修改配置。實際上的配置和上面的一大段一樣,主要是在這個地方配置的只會在ide構建的時候生效,命令列構建不會生效。
命令列構建
基於上面的配置,命令列構建時在命令後面加上這個參數即可 --daemon --parallel --offline。
引入依賴庫時使用aar
使用網上第三方的依賴庫時盡量使用aar,可以在mavenhttp://gradleplease.appspot.com/或者githuhttps://github.com/Goddchen/mvn-repo搜尋。
自己的庫模組也可以打包成aar,關於這個可以參考stormzhang的文章http://www.stormzhang.com/android/2015/03/01/android-reference-local-aar/。
後記
經過這樣一番折騰,我原來需要4,5分鐘才能構建完成的項目,現在只需要30秒左右就可以構建完成了。當然了,如果你這樣還是不可以的話,那麼,你應該換電腦了,ssd的硬碟,更大的記憶體,必須的上了。
原文地址:http://blog.isming.me/2015/03/18/android-build-speed-up/,轉載請註明出處。
加速Android Studio/Gradle構建