Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio - Gradle build failing - Java Heap Space

Android Studio 2.2.3. I've been building an APK for android with my current project with success until very recently. As far as I know, nothing was changed except a data file, and my "Build APK" step mysteriously started failing with this message in the gradle console:

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':VrSamples:Native:myproject:Projects:Android:packageDebug'.
> Java heap space

and in my "messages" window I see this error:

Error:Execution failed for task ':VrSamples:Native:myproject:Projects:Android:packageDebug'.
> Java heap space
mple, the following line, in the gradle.properties file, sets the maximum Java heap size to 1,024 MB:
<em>org.gradle.jvmargs=-Xmx1024m</em>
<a href="http://www.gradle.org/docs/current/userguide/build_environment.html">Read Gradle's configuration guide</a><br><a href="http://docs.oracle.com/javase/7/docs/technotes/guides/vm/gc-ergonomics.html">Read about Java's heap size</a>

I've tried many things:

  • Reset version control to previous version I know was generating APK's fine
  • Re-clone the entire project in a new location to rule out and intermetiate files that might be pulled in with the .gitignore, to guarantee it's a fresh build
  • Rebooted my computer
  • re-installed android studio
  • Run the clean step, rebuilt, manually deleted intermediate files
  • Added org.gradle.jvmargs=-Xmx1024m to my gradle.properties file
  • increased the 1024m to 4096m
  • manually increased the -Xmx value in both Android Studio\bin\studio.exe.vmoptions and the 64-bit equivalent

I'm puzzled as to why I'm suddenly hitting this issue now, and why I can't seem to be able to re-create the APK's I've been building without problems until recently. As far as I'm aware, I also haven't updated any libraries either.

Would appreciate any suggestions to resolve this issue, tips on how to debug this kind of thing down the road, etc.

like image 462
Lucky Mike Avatar asked Jan 14 '17 01:01

Lucky Mike


People also ask

How do I fix Java heap space error?

OutOfMemoryError: Java heap space. 1) An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", this will immediately solve your OutOfMemoryError.

How do I fix expiring daemon because JVM heap space is exhausted?

This can be fixed by increasing the configured max heap size for the project. Through GUI: In the Settings, search for 'Memory Settings' and increase the IDE max heap size and Daemon max heap size as per the system RAM availability.


2 Answers

I noticed that when I open the gradle.properties folder in my Android Studios project, and hovered over any line, it gave a warning about "unused property". That put me on the path to do another google search for "Android Studio gradle.properties unused property" and I found this:

gradle.properties. Unused property

and this:

JVM arguments gradle cannot be set in gradle.properties file for Android Studio 2.1.1

The issue is that, at least for me, Windows+Android Studio seems to ignore the gradle.properties file in my project. You need to create one here: C:\Users\<username>\.gradle\gradle.properties

Then, as suggested in other threads such as the one linked to by @Kevin Tan above, I was able to increase the memory by adding a line such as this:

org.gradle.jvmargs=-XX:MaxHeapSize=256m -Xmx256m

Now I no longer hit the issue

like image 123
Lucky Mike Avatar answered Sep 21 '22 16:09

Lucky Mike


Use this in gradle.properties

org.gradle.parallel=true
org.gradle.daemon=true
org.gradle.jvmargs=-Xmx12800M
org.gradle.configureondemand=true

Worked for me.

like image 25
Shiza Imtiaz Avatar answered Sep 20 '22 16:09

Shiza Imtiaz