Android Studio中Gradle Building速度提升大法

比起两三年前,现在开发Android应用已经可以很顺手了,无论是IDE还是开发框架、第三方库,都比较成熟了。但是呢,使用Android Studio开发时,免不了在调试时,看着gradle building一直在Loading,对于一般的项目也是需要一杯咖啡的时间了,特别是只想测下一丁点的小问题时,也要等这么长时间,实在是没有耐心。那么,经过一轮的Google与实践探索,终于得一大法,仿佛张无忌在昆仑洞下练成了九阳真经。

得此大法,可解决Android Studio Gradle building慢的问题了!将原来的编译时间从五到十分钟提升到30秒左右!

提速一:本地化引用第三方aar或jar包

在开发时,一般会引用到第三方库,例如,在项目中引用了fabprogresscircle库,那么一般在项目的app目录下的build.gradle文件里会有这么一条记录:

compile 'com.github.jorgecastilloprz:fabprogresscircle:1.01@aar'

Gradle在第一次Building时,会将引用的第三方库包下载到缓存里。下载到缓存里的文件应该是fabprogresscircle-1.01.aar。注意:如果build.gradle引用时没有@aar的话,那一般下载到缓存里的是 .jar 文件。如果不知道gradle的缓存目录在哪里,可以在Android Studio编译过一次之后,使用搜索fabprogresscircle-1.01.aar的方法找到目录。

把缓存里的aar或者是jar包复制到项目的libs目录里,然后编辑项目的app目录下的build.gradle文件,将引用的包改为引用本地的包。

build.gradle文件里先增加内容:

repositories {
    flatDir {
        dirs 'libs'
    }
}

上面写的内容意思是告诉Gradle本地引用的包仓库在libs目录,然后把

compile 'com.github.jorgecastilloprz:fabprogresscircle:1.01@aar'

改为下面的:

compile(name: 'fabprogresscircle-1.01', ext: 'aar')

注意

aar与jar包是不同的。

如果一开始引用时,没有@aar,如下:

compile 'com.github.jorgecastilloprz:fabprogresscircle:1.01'

那下载到缓存的是fabprogresscircle-1.01.jar文件,由于在build.gradle文件里,在dependencies下第一条内容就是:

compile fileTree(include: ['*.jar'], dir: 'libs')

上面内容意思是引用本地目录libs下的所有以jar为后缀的包。所以,将fabprogresscircle-1.01.jar文件复制到libs目录后,可以直接删除下面这么条内容:

compile 'com.github.jorgecastilloprz:fabprogresscircle:1.01'

注意

使用此方法,不能将Android本身相关包本地化引用,例如遇到以下的引用就不可以使用此方法了,不然会出错:

compile 'com.android.support:appcompat-v7:23.0.1'

一般以com.google.androidcom.android.support开头的相关包都不能使用此方法。

提速二:优化Android Studio的设置

在Android Studio软件里,点击”Files” -> “settings”,打开设置对话框,在左边导航里点击”Build, Execution, Deployment” -> “Gradle”,在右边,在offline work前打勾。这样子,Gradle就是离线模式,避免了Gradle Building时联网超时的问题。但是如果项目是第一次同步或编译,则不能应用此设置。

另外,编译设置也要作同样的优化,如下图:

《Android Studio中Gradle Building速度提升大法》 Android Studio 设置

提速三:开启gradle单独的守护进程

在下面的目录下面创建gradle.properties文件:

  • /home/<username>/.gradle/ (Linux)
  • /Users/<username>/.gradle/ (Mac)
  • C:\Users<username>.gradle (Windows)

复制以下内容到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 servers
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=-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_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

上面的配置文件主要就是做,增大gradle运行的java虚拟机的大小,让gradle在编译的时候使用独立进程,让gradle可以平行的运行。

将以上内容复制到gradle.properties文件里后,就是对Gradle全局应用,对所有项目都有效的。如果只想在某一项目应用此设定,可以在项目下的gradle.properties里增加以上设定内容。

本文同步发布到作者博客:http://garth.im/2016/11/android-studio-gradle-speed-up/
更多文章请访问作者博客:http://garth.im/

    原文作者:Garth
    原文地址: https://www.jianshu.com/p/21db045134c3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞