不知不觉写了三年的Android,以前视图过些博客但一直坚持下来,或者写到一半就丢弃忙其他去了。现在的部门老大是个喜欢写博客的大牛,在老大的感染下,开始写博客记录下写码过程中的一些坑和解决思路吧。
先说一下编译慢的原因:
1、编译的时候,Java的设定虚拟机太小,目前的AndroidStudio个人觉得是比较吃电脑配置的。可以通过增大gradle运行时的设定的虚拟机大小来加快速度。
2、引用比较庞大的Module或者多个Module,如果Module不怎么修改的话,可以把Module打包成aar文件
在此引用博主 少有人走的路博客中的图片http://blog.csdn.net/zero_and_one/article/details/42009487
repositories{
//mavenCentral()
flatDir{
dirs 'libs'
}
//maven { url "https://jitpack.io" }
}
dependencies {
compile(name: 'Module的aar文件名字', ext: 'aar')
}
3、引用太多第三方模块,如今大部分Android需要快速开发,都是引用大量的第三方模块,在gradle中引用第三方模块时采用maven方式依赖,每次编译gradle都会先通过网络请求maven的中央仓库,即使库文件已经下载到本地,也需要请求一次仓库。引用的方式为
dependencies {
//引用EventBus
compile 'org.greenrobot:eventbus:3.0.0'
}
解决的思路就是第一次同步代码后,把第三方库都下载到本地,将gradle设置为offline模式。
具体优化的操作如下:
1、修改项目下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=-Xmx4096m -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
2、修改主module下的build.gradle文件
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.xxx.xxx"
minSdkVersion 17
targetSdkVersion 25
versionCode 1
versionName "1.0.0"
//优化编译速度
dexOptions {
dexInProcess true
preDexLibraries true
javaMaxHeapSize "4g"//越大越好
incremental true
}
}
}
3、 gradle 设为离线编译模式方法
file->setting->build,execution,deployment->gradle
勾选offline work。如果引入新的第三方库先取消掉,同步编译完再勾上。