Android studio遇到 java.lang.OutOfMemoryError: GC app:transformClassesWithDexForDebug解决方法 以及gradle优化

白色玫瑰 程序猿

时间: 2023-05-22 阅读: 1 字数:5545

{}
Android studio遇到 java.lang.OutOfMemoryError: GC app:transformClassesWithDexForDebug解决方法、gradle优化设置 节省编译时间

目录

Android studio遇到 java.lang.OutOfMemoryError: GC app:transformClassesWithDexForDebug解决方法 以及gradle优化设置

Android studio引入或者新建工程编译发现报以下错误,详细日志如下:


Error:Uncaught translation error: java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: GC overhead limit exceeded
Error:Uncaught translation error: java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: GC overhead limit exceeded
Error:2 errors; aborting
:app:transformClassesWithDexForDebug FAILED
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_74\bin\java.exe'' finished with non-zero exit value 1

Information:BUILD FAILED
Information:Total time: 6 mins 23.159 secs
Information:See complete output in console
Information:1,876 errors
Information:0 warnings

大家看到OOM一开始就是想到内存问题, app:transformClassesWithDexForDebug问题大家应该比较熟悉了, 例如常见的是包冲突或者jar冲突导致的,去检查下build.gradle,工具会提醒报错,大家修改后重新编译即可.

这里的提示的是java.lang.OutOfMemoryError: GC overhead,应该就是Android studio分配的内存不够引起的. 还有一个问题发现编译时间长达6分多钟. 所以做了以下优化测试可以解决: 1.打开AS安装目录所在的位置,bin目录下,如图: 这里写图片描述 2.用记事本软件打开1中文件. 修改为以下内容,增加分配默认内存(具体内存可以适当加大):

#
# *DO NOT* modify this file directly. If there is a value that you would like to override,
# please add it to your user specific configuration file.
#
# See http://tools.android.com/tech-docs/configuration
#
-Xms2048m
-Xmx2048m
-XX:MaxPermSize=2048m
-XX:ReservedCodeCacheSize=1024m
-XX:+UseConcMarkSweepGC
-XX:SoftRefLRUPolicyMSPerMB=50
-da
-Djna.nosys=true
-Djna.boot.library.path=

-Djna.debug_load=true
-Djna.debug_load.jna=true
-Dsun.io.useCanonCaches=false
-Djava.net.preferIPv4Stack=true
-XX:+HeapDumpOnOutOfMemoryError
-Didea.paths.selector=AndroidStudio2.0
-Didea.platform.prefix=AndroidStudio

3.打开对应的项目中的“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=-Xmx2048m -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

然后gradle优化设置: 1.修改gradle-wrapper.properties文件: 这里写图片描述 例如内容: 这里写图片描述

具体版本与实际保持一致 2.关闭编译是读取网络资源,节省编译时间 file->setting菜单进入如下设置: 这里写图片描述

然后进入以下菜单设置: 这里写图片描述

现在设置完成了,我再次编译下看下结果: 发现编译成功了,而且时间也短了不少(PS:公司电脑有点反应慢,哈哈,所以现在编译还是比较慢的,单相对于之前快了很多) 这里写图片描述

以上配置解决了遇到的java.lang.OutOfMemoryError: GC app:transformClassesWithDexForDebug编译错误,后面相关设置也优化了编译时间问题,希望对大家有所帮助. 大家发现有更好的解决与优化版本可以一起分享与沟通一下.

原文地址:https://blog.csdn.net/xiaoxing0828/article/details/52242090?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168475006116800192211331%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168475006116800192211331&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-18-52242090-null-null.142^v87^control_2,239^v2^insert_chatgpt&utm_term=java%E4%BC%98%E5%8C%96

本文章网址:https://www.sjxi.cn/detil/a4ab5aa1e5244ee796edbe706f487e42

打赏作者

本站为非盈利网站,如果您喜欢这篇文章,欢迎支持我们继续运营!

最新评论
当前未登陆哦
登陆后才可评论哦

湘ICP备2021009447号

×

(穷逼博主)在线接单

QQ: 1164453243

邮箱: abcdsjx@126.com

前端项目代做
前后端分离
Python 爬虫脚本
Java 后台开发
各种脚本编写
服务器搭建
个人博客搭建
Web 应用开发
Chrome 插件编写
Bug 修复