简体   繁体   English

使用 gradle 编译我的 android 应用程序时出现 OutOfMemoryError

[英]OutOfMemoryError when compiling my android app with gradle

I'm trying to build my app via gradle and I'm currently having this issue after running a ./gradlew build :我正在尝试通过 gradle 构建我的应用程序,并且在运行./gradlew build后我目前遇到了这个问题:

:myApp:compileDebug


The system is out of resources.
Consult the following stack trace for details.
java.lang.OutOfMemoryError: Java heap space
    at com.sun.tools.javac.util.Position$LineMapImpl.build(Position.java:139)
    at com.sun.tools.javac.util.Position.makeLineMap(Position.java:63)
    at com.sun.tools.javadoc.DocCommentScanner.getLineMap(DocCommentScanner.java:438)
    at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:512)
    at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:550)
    at com.sun.tools.javac.main.JavaCompiler.parseFiles(JavaCompiler.java:804)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:727)
    at com.sun.tools.javac.main.Main.compile(Main.java:353)
    at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:115)
    at org.gradle.api.internal.tasks.compile.jdk6.Jdk6JavaCompiler.execute(Jdk6JavaCompiler.java:40)
    at org.gradle.api.internal.tasks.compile.jdk6.Jdk6JavaCompiler.execute(Jdk6JavaCompiler.java:33)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.delegateAndHandleErrors(NormalizingJavaCompiler.java:95)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:48)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:34)
    at org.gradle.api.internal.tasks.compile.DelegatingJavaCompiler.execute(DelegatingJavaCompiler.java:29)
    at org.gradle.api.internal.tasks.compile.DelegatingJavaCompiler.execute(DelegatingJavaCompiler.java:20)
    at org.gradle.api.internal.tasks.compile.IncrementalJavaCompilerSupport.execute(IncrementalJavaCompilerSupport.java:33)
    at org.gradle.api.internal.tasks.compile.IncrementalJavaCompilerSupport.execute(IncrementalJavaCompilerSupport.java:24)
    at org.gradle.api.tasks.compile.Compile.compile(Compile.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1047)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:877)
    at org.gradle.api.internal.BeanDynamicObject$MetaClassAdapter.invokeMethod(BeanDynamicObject.java:216)
    at org.gradle.api.internal.BeanDynamicObject.invokeMethod(BeanDynamicObject.java:122)
    at org.gradle.api.internal.CompositeDynamicObject.invokeMethod(CompositeDynamicObject.java:147)
    at org.gradle.api.tasks.compile.JavaCompile_Decorated.invokeMethod(Unknown Source)
    at groovy.lang.GroovyObject$invokeMethod.call(Unknown Source)
:myApp:compileDebug FAILED

Any idea?任何的想法?

I think I fixed it.我想我修好了。 I got the solution from this post .我从这篇文章中得到了解决方案。 ie: IE:

replacing in gradlew:在gradlew中替换:

GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""

by经过

GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\" \"-Xmx1024m\" \"-Xms256m\" \"-XX:MaxPermSize=1024m\""

In my project there was a gradle.properties file with these lines:在我的项目中,有一个带有以下gradle.properties行的gradle.properties文件:

# 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

I uncommented the last line, and that worked.我取消了最后一行的注释,这奏效了。

I also faced this problem on regular java project.我在常规 Java 项目中也遇到过这个问题。 Our test execution was extensive and used to run out of memory or permgen error.我们的测试执行范围很广,经常会出现内存不足或 permgen 错误。

So there are two soluton 1. Set the parameters and the run the build所以有两个解决方案 1.设置参数并运行构建

export JAVA_OPTS="-Xmx1024M -XX:MaxPermSize=512M -XX:ReservedCodeCacheSize=512M" 
export GRADLE_OPTS="-Dorg.gradle.daemon=true"

Second option simplifies the solution within in gradle file第二个选项简化了 gradle 文件中的解决方案

test {
    jvmArgs "-XX:MaxPermSize=256m"
}

I recommend second option as its permanent fix.我推荐第二个选项作为它的永久修复。

dexOptions {

        incremental true

       //javaMaxHeapSize=1024M for 32bit Java,2048M for 64bit Java

       javaMaxHeapSize "1024M"

       //javaMaxHeapSize "2048M"
    }

FWIW,我通过将一个非常大的字符串硬编码到源文件中来重现“系统资源不足”错误(但使用另一个堆栈跟踪)。

Changing the JAVA_HOME folder to the 64-bit installation helped me too.将 JAVA_HOME 文件夹更改为 64 位安装也对我有帮助。 Use 64-bit runner for IDEA after that.之后为 IDEA 使用 64 位运行程序。

我在构建服务器上遇到了同样的问题,一旦我将 JAVA_HOME 文件夹更改为 Java 的 64 位安装,错误就消失了。

For anyone using Gradle and Kotlin , nothing here worked and I spent hours looking for a solution.对于任何使用 Gradle 和Kotlin 的人来说,这里没有任何效果,我花了几个小时寻找解决方案。 Here's what worked for me:以下是对我有用的内容:

Add this anywhere to your build.gradle :将其添加到build.gradle任何位置:

compileKotlin {
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
        freeCompilerArgs = ['-Xjsr305=strict', '-XXLanguage:-NewInference']
    }
}

Ok I had this issue as well with a springboot project.好的,我在 springboot 项目中也遇到了这个问题。 I ended up by running the clean task and the problem was solved.我最终通过运行清理任务并解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM