加速Android Studio编译

1.使用阿里的freeline

Freeline是蚂蚁金服旗下一站式理财平台蚂蚁聚宝团队在Android平台上的量身定做的一个基于动态替换的编译方案,稳定性方面:完善的基线对齐,进程级别异常隔离机制。性能方面:内部采用了类似Facebook的开源工具buck的多工程多任务并发思想, 并对代码及资源编译流程做了深入的性能优化。

怎么使用freeline以及使用freeline的相关问题?请查看关于使用阿里Freeline遇到的问题

2.更新Gradle

第一种方式:在项目根目录中找到gradle.properties文件,在文件中配置:

distributionUrl=http\://services.gradle.org/distributions/gradle-3.5-all.zip

gradle-3.5-all.zip是当前最新的版本。关于gradle的版本,可在官网上查看。

第二种方式:在项目根目中找到build.gradle文件,在文件中配置:

task wrapper(type: Wrapper) {
    gradleVersion = '3.5'
}

配置好了之后,打开Terminal终端,执行gradlew wrapper命令,这样就会自动下载3.5版本。

第三种方式:打开Project Structure,
这里写图片描述
输入相应的版本号。

3.在gradle.properties中配置参数


配置JVM参数


设置org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m

Specifies the JVM arguments used for the daemon process.
指定用于守护进程的JVM参数。
The setting is particularly useful for tweaking memory settings.
该设置对于调整内存设置特别有用。
Default value: -Xmx1024m -XX:MaxPermSize=256m
默认值

上面说明了设置这些参数的意义。这些参数分别代表的含义如下:
-Xmx:设置最大堆内存

The -Xms option sets the initial and minimum Java heap size. The Java heap (the “heap”) is the part of the memory where blocks of memory are allocated to objects and freed during garbage collection.

-XX:MaxPermSize:

Permanent Generation (non-heap): The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.

Permanent Generation(不是堆)里面存放的是类,以及方法对象等等

关于JVM更多配置参数,请参阅 Oracle 的Java HotSpot VM 选项页

开启并行编译

设置org.gradle.parallel=true

When configured, Gradle will run in incubating parallel mode.
This option should only be used with decoupled projects.

注意:这个参数的配置只在多项目中才有用。

开启守护进程

设置org.gradle.daemon=true。开启守护进程后,在下一次构建时,将会连接这个守护进程进行构建,而不是重新fork一个gradle构建进程。

编译缓存

设置android.enableBuildCache=true
注意:编译缓存功能是在AndroidStudio2.2中才添加的,默认是关闭状态。当前AndroidStudio已经2.3了,默认是打开状态。

在构建时选择Offline work

在设置中选择offline work:
这里写图片描述

或者编译时使用命令:gradlew aDebug –offline

3.开启增量编译

在项目主Module的build.gradle中添加:

dexOptions {
        incremental true
}

该配置在android元素下。

综上:通过使用阿里的Freeline和配置一些相应的参数,都加快了AndroidStudio编译项目的速度。但最快的,还是使用阿里的Freeline,因为可以将十多分钟编译的项目减少到几十秒,实在是太棒了。

猜你喜欢

转载自blog.csdn.net/wangjiang_qianmo/article/details/70159738