本文分要点及具体内容,要点看不明白可以查看具体内容。

要点:

  1. 使用gradle命令:
    ./gradlew :android:assembleDebug --dry-run --profile (android是module名称)
  2. 配置gradle.properties,在电脑中./gradle根目录配置,在AS创建项目时候自动加上此配置。
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8org.gradle.parallel=trueorg.gradle.configureondemand=true ```或者使用该命令:``` ./gradlew :android:assembleDebug --dry-run --profile --configure-on-demand ```## Project-wide Gradle settings.## For more details on how to configure your build environment visit# http://www.gradle.org/docs/current/userguide/build_environment.html## Specifies the JVM arguments used for the daemon process.# The setting is particularly useful for tweaking memory settings.# Default value: -Xmx1024m -XX:MaxPermSize=256morg.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=4096m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8## 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## 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### 具体内容:我们在学习大部分语言(Android、iOS等)时候,都要去考虑这种语言的生命周期,gradle也不例外,它的编译过程大概分为3个阶段(具体gradle理论知识可以自行研究):  *  初始化:扫描项目,找出哪些内容需要被编译(indexing)  2. 配置:运行build.gradle脚本,创建任务图(downloading)  3. 执行:构建你APP有用的部分根据这一系列过程,我们以GitHub上的一个demo进行说明,项目地址如下:http://github.com/google/iosched  1. 使用dry-run(能够让gradle去跳过所有任务的执行)。gradle执行过程需要做大量工作,我们可以考虑,让它执行必须做的事情。如果是新项目并且首次执行以下命令,会去拉取所需的依赖,以后build会加快,命令如下:```./gradlew :android:assembleDebug --dry-run  ```其中,android 是module名称可以通过生成编译报告,查看具体编译时间```./gradlew :android:assembleDebug --dry-run --profile open build/reports/profile/profile-2017-05-29-20-29-14.html```![image.png ](http://upload-images.jianshu.io/upload_images/1631967-740f241b0dfe88a3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/480)![image.png](http://upload-images.jianshu.io/upload_images/1631967-dfbc3aa1b7e5af41.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/480)![image.png](http://upload-images.jianshu.io/upload_images/1631967-5c5b047245400de4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/480)2.启用按需配置,提升大概15%```./gradlew :android:assembleDebug --dry-run --profile --configure-on-demand```它不是默认启用的,所以需要手动开启,我们可以通过在电脑目录.gradle/gradle.properties加一行配置全局使用它:```org.gradle.configureondemand=true ```![image.png](http://upload-images.jianshu.io/upload_images/1631967-640f86af7306371d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/480)3.开启gradle单独的守护进程``` ./gradlew :android:assembleDebug --dry-run --no-daemon ```vs. ``` ./gradlew :android:assembleDebug --dry-run --daemon ```![image.png](http://upload-images.jianshu.io/upload_images/1631967-61162189bd12ee69.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/480)4.避免繁重的计算(这个得具体问题具体分析了)  尝试去减少gradle构建时的IO输出,使用Groovy语言,修改build.gradle中的一些脚本的实现。 5.不要动态使用依赖   gradle允许你指定项目中依赖包的范围,gradle尝试去找最新的版本,这就消耗了gradle的灵活性。```dependencies {    compile 'com.google.code.gson:gson:2.+'}```这样不仅会减慢你的项目编译,同时也会失去了重复性的构建。6.模块化项目和并行化编译模块化项目可以并行编译,并行工程的配置,复用之前的项目,项目得到及时检查,在项目编译过程中使用了预编译```org.gradle.parallel=true```7.及时更新新版本gradle是一个比较复杂的‘怪物’,大多数的项目随着每个release版本越来越快,所以用最新的版本有很大意义。### 参考文献:1. [6个技巧加速你的gradle编译](http://www.jianshu.com/p/2ff3717199da)2. [优化Android Studio/Gradle构建](http://www.cnblogs.com/hujihon/p/4843170.html)

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. GitHub 标星 2.5K+!教你通过玩游戏的方式学习 VIM!
  3. 如何在后台运行Linux命令?
  4. No.11 使用firewall配置的防火墙策略的生效模式
  5. 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
  6. Android(安卓)Studio项目混淆后输出的文件所在位置
  7. [Android]getevent,sendevent,input命令的使用
  8. github开源库(三)
  9. Ubuntu下安装配置Android(安卓)SDK

随机推荐

  1. MySQL procedure 与 function 的区别
  2. ISSUE:Mysql将Enum转换为Int
  3. 关于MySQL数据库操作
  4. MySQL中varchar最大长度是多少?
  5. mysql 第二十四篇文章~相关分片功能的测
  6. mySQL错误: The used table type doesn't
  7. 无法连接远程MySQL数据库的解决方案
  8. 使用mysql和PHP从本地主机连接到远程数据
  9. 在MySQL解释计划中“选择优化的表”的意
  10. 我是初学者,使用mysql时候总是出现comman