How do I fix java.lang.OutOfMemoryError: Java heap space
when compiling my Android project?
I get this after I upgraded to version 1 of Android Studio.
Just add this to the gradle file:
android{
testOptions {
....
unitTests.all {
jvmArgs "-Xmx2g" // to avoid outOfMemory exception
}
....
}
}
None of the answers worked for me but finally I found something that worked in my case:
Automatically managing paging files size
In windows:
I don't remember why I had this unchecked but this was causing trouble in my gradle builds (with flutter).
Here is what I did:
1st step: In your App, go to GRADLE SCRIPTS -> GRADLE.PROPERTIES -> change org.gradle.jvmargs=-Xmxm to org.gradle.jvmargs=-Xmx4G
2nd Step: In your App, go to GRADLE SCRIPTS -> build.gradle -> in the android{
//add this line:
dexOptions {
javaMaxHeapSize = "4G"
}
}
Build => Clean Project
then
Build => Rebuild Project
When I do this, it always fixes for me.
I've finally solved it by limiting the Google Play service in gradle dependency.
The problem however was caused by the Google Play service, and by simply change it to a specific library, it is now solved.
com.google.android.gms:play-services-base:6.5.87
UPDATE
More info on android guides
assign more memory to Gradle in the project's gradle.properties file.For example:
org.gradle.jvmargs=-Xmx1024m