时间: 2023-05-22 阅读: 1 字数:5545
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编译错误,后面相关设置也优化了编译时间问题,希望对大家有所帮助. 大家发现有更好的解决与优化版本可以一起分享与沟通一下.
本站主要用于日常笔记的记录和生活日志。本站不保证所有内容信息可靠!(大多数文章属于搬运!)如有版权问题,请联系我立即删除:“abcdsjx@126.com”。
QQ: 1164453243
邮箱: abcdsjx@126.com