A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 马伟奇 黑马帝   /  2015-6-15 16:23  /  42986 人查看  /  32 人回复  /   23 人收藏 转载请遵从CC协议 禁止商业使用本文


优化Android Studio/Gradle构建

       使用Android Studio进行开,随着项目的增大,依赖库的增多,构建速度越来越慢,现在最慢要6分钟才能build一个release的安装包,在网上查找资料,发现可以通过一些配置可以加快速度,这里跟大家分享一下。

开启gradle单独的守护进程

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

  • /home/<username>/.gradle/ (Linux)
  • /Users/<username>/.gradle/ (Mac)
  • C:\Users\<username>\.gradle (Windows)
  • 把下面配置复制gradle.properties文件也可以优化:
    1. # Project-wide Gradle settings.
    2. # IDE (e.g. Android Studio) users:
    3. # Settings specified in this file will override any Gradle settings
    4. # configured through the IDE.
    5. # For more details on how to configure your build environment visit
    6. # http://www.gradle.org/docs/current/userguide/build_environment.html
    7. # The Gradle daemon aims to improve the startup and execution time of Gradle.
    8. # When set to true the Gradle daemon is to run the build.
    9. # TODO: disable daemon on CI, since builds should be clean and reliable on servers
    10. org.gradle.daemon=true
    11. # Specifies the JVM arguments used for the daemon process.
    12. # The setting is particularly useful for tweaking memory settings.
    13. # Default value: -Xmx10248m -XX:MaxPermSize=256m
    14. org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
    15. # When configured, Gradle will run in incubating parallel mode.
    16. # This option should only be used with decoupled projects. More details, visit
    17. # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
    18. org.gradle.parallel=true
    19. # Enables new incubating mode that makes Gradle selective when configuring projects.
    20. # Only relevant projects are configured which results in faster builds for large multi-projects.
    21. # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
    22. org.gradle.configureondemand=true
    复制代码
    同时上面的这些参数也可以配置到前面的用户目录下的gradle.properties文件里,那样就不是针对一个项目生效,而是针对所有项目生效。
    上面的配置文件主要就是做, 增大gradle运行的java虚拟机的大小,让gradle在编译的时候使用独立进程,让gradle可以平行的运行。

    1.申请大内存

    installation path\studio64.exe.vmoptions or studio.exe.vmoptions

    使用文本编辑器打开,找到起始两行,如下

    -Xms128m
    -Xmx750m


    修改最小值和最大值,建议为

    -Xms256m
    -Xmx2048m


    2 优化编译

     file->setting->compile

     勾选除第二项之外的其他选项,并在VM options里填入:

     -Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8


经过测试,编译速度有很大提升,比较大的项目从之前的5-8分钟可以减少到1-2分钟。


用命令行Build

可以参考这个讨论串

https://plus.google.com/u/0/+RicardoAmaral/posts/e9PG6vSN5w3

gradle assembleDebug

实测速度快50%左右 原理不明

回头写个脚本 build完成再自动安装运行应该就好了


我觉得TMD还是把公司的破电脑扔了换个新的才能从根本上解决问题

评分

参与人数 3黑马币 +40 收起 理由
relice + 10 很给力,as快了不少!
meng + 20 很给力!
jp1017 + 10 赞一个!

查看全部评分

32 个回复

倒序浏览
学习了 感谢楼主分享
回复 使用道具 举报
最后一句话是亮点
回复 使用道具 举报
听完课来顶一下
回复 使用道具 举报
冒个泡收藏下
回复 使用道具 举报
我的用的Android Studio 1.3
在执行你的优化编译时步骤时 file->setting->compile   在VM options里填入 。。
   
没用发现VM option 选项
回复 使用道具 举报 1 0
qq897425998 发表于 2015-7-8 10:47
我的用的Android Studio 1.3
在执行你的优化编译时步骤时 file->setting->compile   在VM options里填入 。 ...

我用的1.2.2,1.3还没有正式版本。没有用过
回复 使用道具 举报
qq897425998 发表于 2015-7-8 10:47
我的用的Android Studio 1.3
在执行你的优化编译时步骤时 file->setting->compile   在VM options里填入 。 ...

同问!!!我用的as1.3也没有
回复 使用道具 举报
刚刚听完课,顶一下
回复 使用道具 举报
请问“黑马币”和“黑马逼”、“黑吗?逼”,有什么区别?
回复 使用道具 举报
2676014118 发表于 2015-7-8 12:03
同问!!!我用的as1.3也没有

不好意思。由于1.3没有出正式版。我还没有用过。我用的是正式版本的1.2.2.等出了1.3我在研究下。
回复 使用道具 举报
绝对要换AndroidStudio啊,一步一步学习中,谢谢老湿。。。
回复 使用道具 举报
1.2.2 配置好后 更新到1.3提示这个 如图片 好像是说不支持 gradle arguments ,英文不是很好,最近在研究gradle和groovy,如果有好的视频资料记得分享啊,我是51期的,大家互相学习下哈。
QQ截图20150803145315.png
回复 使用道具 举报
看了小马哥的头像,只能说,岁月是把杀猪刀。。。
回复 使用道具 举报
顶一个,小马哥
回复 使用道具 举报
老师 说的是这个位置吧~~确实快了好多~爽~[img]
android studio.png
回复 使用道具 举报 1 0
本帖最后由 relice 于 2016-7-5 21:34 编辑

{:2_36:}.......
回复 使用道具 举报
qq897425998 发表于 2015-7-8 10:47
我的用的Android Studio 1.3
在执行你的优化编译时步骤时 file->setting->compile   在VM options里填入 。 ...

1.3以上的叫 Command-line Options在里面输入即可
回复 使用道具 举报 0 1
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶
回复 使用道具 举报
本帖最后由 甜菜包子 于 2015-10-30 14:03 编辑
relice 发表于 2015-10-7 11:04
1.3以上的叫 Command-line Options在里面输入即可

在Command-line Options里输入-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
的话会报错:
Error:Failed to complete Gradle execution.

Cause:
Unknown command-line option '-X'.

请问你是怎么输入的呀?

回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马