Unity 2017.3 fails gradle build due to java heap memory

Since the first Unity 2017.3.0f3 and with later patch versions we can not build our project for Android anymore. It requires Gradle and the issue is:

But the project compiles perfectly in the previous Unity 2017.2.1. I see 2 potential changes in Unity 2017.3 that could be causing the new behaviour:

I have tried everything by now. I have created my gradle.properties file in my .gradle user folder that looks like:

I can say Gradle is getting some of the information of this file and using it, like daemon and parallel settings, as you can see in the build log:

BUT, I think org.gradle.jvmargs is being overriden by the gradle call that Unity is generating. If I take a look to the call to java for launching the gradle process you can see the max memory is being set with 2g:

So as Unity is calling this process with this setting, it is impossible to me to increase the memory when gradle process starts and the project fails when Gradle comes to action. The only way I can build the game by now is exporting the project in Gradle and then use Android Studio to create the final APK. This is a big issue as many other developers would want to test some new features and would be nice to not need external tools like Android Studio to achieve it.

So, I would expect some kind of new Gradle option in Android playersettings for setting this amount of memory Unity is sending to the command, or Unity should remove the ā€œ-Dorg.gradle.jvmargs=-Xmx2048mā€ from the command line for Gradle to be able to get this information from user gradle.properties.

This problem is also happening in Unity beta 2018.1.0b4.

1 Like

Anyone else is experimenting this issue in a big project?

Iā€™m experiencing this problem and my project isnā€™t that large. Same issues.

Iā€™am having the same issue with ā€œheavyā€ project.

There is a bug in issuetracker quite similar we should follow:

I am not using assetbundles, but if they solve itā€¦

The same problems - project is not large

And I suspect problems begins when I move lots of resources to Resource folder.
Also Split Binary OGG helps to build - no errors

1 Like

Same problem. Had to downgrade to 2017.2 to get the APK building again.

Same problem on 2017.4.1f1 LTS
Same problem on 2017.3
2017.2 contains critical issue that cause gameplay crash on Android

The only way is use split build. This is so sad.

Same issue for me. Selecting Internal Build instead of Gradle bypassed it for my work.

6 Likes

Youā€™re a lifesaver! Thanks!! Internal Build worked perfectly.

1 Like

AMAZING!!!

This worked for me too!

1 Like

Worked for me as well.

1 Like

THANKS FRIEND IT WORKS!!

In ā€œBuild systemā€ I used ā€œInternalā€ and the problem desappeared

Until you get to zipalign crashing if your apk is too large, if only we could disable zipalign from Unityā€™s internal build to bypass it.

You can export build as Gradle project and build apk with Java heap parameter

For debug build
java -classpath org.gradle.launcher.GradleMain -p ā€œ-Dorg.gradle.jvmargs=-Xmx4096mā€ ā€œassembleDebugā€

For release build
java -classpath org.gradle.launcher.GradleMain -p ā€œ-Dorg.gradle.jvmargs=-Xmx4096mā€ ā€œassembleReleaseā€

Gradle launcher could be found at:
\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-.jar

Built apk will output to
\build\outputs\apk\debug
\build\outputs\apk\release
If you build with obb, the obb file will be exported at Gardle project root

1 Like

This has been finally fixed in Unity 2017.4.13f1 LTS. Making build again from Unity without any problem.

Changelog to check in other Unity versions:

  • Android: Increased JVM heap size from 2gb to 4gb. (969563)
1 Like

I had the same problem in unity 2018.4

Iā€™m having this problem. Iā€™ve tried exporting and running the above command. But it just spits out an error:
Error: Could not find or load main class assembleRelease

Nevermind. The same command works fine on another machine.