• Android gradle编译错误OOM:java heap space的解决办法


    Android开发中,在编译较大规模项目时,出现内存溢出错误:

    OutOfMemeryError OOM:java heap space;

    尝试解决办法:

    1. 开发项目根项目下增加/demo/gradle.properties配置文件

    1. ## For more details on how to configure your build environment visit
    2. # http://www.gradle.org/docs/current/userguide/build_environment.html
    3. #
    4. # Specifies the JVM arguments used for the daemon process.
    5. # The setting is particularly useful for tweaking memory settings.
    6. # Default value: -Xmx1024m -XX:MaxPermSize=256m
    7. # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
    8. #
    9. # When configured, Gradle will run in incubating parallel mode.
    10. # This option should only be used with decoupled projects. More details, visit
    11. # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
    12. # org.gradle.parallel=true
    13. #Mon Dec 21 15:53:55 CST 2020
    14. org.gradle.jvmargs=-Xmx8192M -Dkotlin.daemon.jvm.options\="-Xmx8192M"

    2. 项目目录/demo/gradle/wrapper/gradle-wrapper.properties

    1. #Tue Jul 09 14:47:41 CST 2019
    2. distributionBase=GRADLE_USER_HOME
    3. distributionPath=wrapper/dists
    4. zipStoreBase=GRADLE_USER_HOME
    5. zipStorePath=wrapper/dists
    6. distributionUrl=https\://services.gradle.org/distributions/gradle-5.5-all.zip
    7. #Set jvmargs as 4096M avoid to OOM:java heap space
    8. org.gradle.jvmargs=-Xmx8192M
    9. # Enables R8 for Android Library modules only. (default true, you can disable)
    10. android.enableR8.libraries = true
    11. # Enables R8 for all modules.(default true, you can disable)
    12. android.enableR8 = true

    通过上面两个配置文件,编译项目还是出现OutOfMemeryError OOM:java heap space

    猜测可能事配置没有写对,/demo/gradle.properties中应该配置:

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

    最后找到了全局的 /home/hulk/.gradle/gradle.properties,做如下修改,其他代码库也能使用.

    3. 修改系统gradle全局的 gradle.properties

    /home/hulk/.gradle/gradle.properties  (无此文件就新建一个)

    1. ## For more details on how to configure your build environment visit
    2. # http://www.gradle.org/docs/current/userguide/build_environment.html
    3. #
    4. # Specifies the JVM arguments used for the daemon process.
    5. # The setting is particularly useful for tweaking memory settings.
    6. # Default value: -Xmx1024m -XX:MaxPermSize=256m
    7. # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
    8. #
    9. # When configured, Gradle will run in incubating parallel mode.
    10. # This option should only be used with decoupled projects. More details, visit
    11. # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
    12. #开启并行编译,相当于多条线程再走
    13. # org.gradle.parallel=true
    14. #开启线程守护,第一次编译时开线程,之后就不会再开了
    15. org.gradle.daemon=true
    16. #Set jvmargs as 4096M avoid to OOM:java heap space
    17. #配置编译时的虚拟机大小:具体数值根据本机内存大小进行设置如机器为16G内存,可设置为8G(8192M)
    18. org.gradle.jvmargs=-Xmx8192m -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
    19. #仅设置前面虚拟内存大小也行,如下
    20. #org.gradle.jvmargs=-Xmx8192M   
    21. #启用新的孵化模式
    22. org.gradle.configureondemand=true
    23. #代理服务器
    24. systemProp.http.proxyHost=proxy.lfk.xxx-inc.cn
    25. systemProp.http.proxyPort=3128
    26. systemProp.https.proxyHost=proxy.lfk.xxx-inc.cn
    27. systemProp.https.proxyPort=3128

    通过以上的全局配置修改调整: 把虚拟内存设置为8G: org.gradle.jvmargs=-Xmx8192M

    再编译,就没有问题了,记录一下,希望码友们用得着 

    gtadle文件链接如下

    Android编译gradle.properties文件及说明-Android文档类资源-CSDN下载

  • 相关阅读:
    细胞衰老β-半乳糖苷酶染色试剂盒丨艾美捷解决方案
    java 中为什么重写 equals 后需要重写 hashCode
    Python爬虫要掌握哪些东西
    PXIE板卡,4口QSFP+,PCIE GEN3 X8,XILINX FPGA XCVU3P设计
    vue如何动态加载显示本地图片资源
    颠覆性语音识别:单词级时间戳和说话人分离 | 开源日报 No.53
    MySQL8 间隙锁在11种情况下的锁持有情况分析
    华为机试:最小传输时延
    从c++到Java,关于Java面向对象基础的学习(二)
    Spring Cloud Gateway核心过滤器之请求限流详解
  • 原文地址:https://blog.csdn.net/zhanghao_Hulk/article/details/126055390