1. 程式人生 > >Android studio遇到 java.lang.OutOfMemoryError: GC app:transformClassesWithDexForDebug解決方法 以及gradle優化

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編譯錯誤,後面相關設定也優化了編譯時間問題,希望對大家有所幫助.
大家發現有更好的解決與優化版本可以一起分享與溝通一下.