twiz
twiz

Reputation: 10558

Azure DevOps Gradle "Java heap space" error

I am using an Azure DevOps Pipeline for CI/CD for a ReactNative Android app.

It has been working great for a while now, but in my latest release the Gradle build is running into the following error:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:signProductionReleaseBundle'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.FinalizeBundleTask$BundleToolRunnable
   > Java heap space

The relevant part of my azure-pipelines.yml file looks like this:

pool:
  vmImage: "macos-latest" # I am using this image because I am also doing iOS builds (not sure if it's relevant to the problem)

jobs:
  - job: DeployAndroid
    steps:
      - task: Gradle@2
        inputs:
          gradleWrapperFile: "MyApp/android/gradlew"
          cwd: "MyApp/android"
          tasks: "bundleProductionRelease"
          publishJUnitResults: false
          javaHomeOption: "JDKVersion"
          sonarQubeRunAnalysis: false

Java heap space isn't a very descriptive error, but it seems reasonable to assume it is a memory issue. I attempted to increase the max JVM memory by adding the gradleOptions argument:

- task: Gradle@2
  inputs:
    gradleOptions: "-Xmx3072m"

The default value is -Xmx1024m, so I thought tripling the memory (-Xmx3072m) might work. Unfortunately I am still getting the same error.

Does anyone have any other ideas on how I can fix this error?

Upvotes: 4

Views: 2800

Answers (1)

twiz
twiz

Reputation: 10558

I discovered there were several problems that needed to solved to get this to work.

For some reason the gradleOptions argument in azure-pipelines.yml wasn't increasing the memory, even though the documentation shows it being used specifically for the -Xmx flag. (I imagine something about my setup is just overriding it)

Instead I added the following line to my gradle.properties file:

org.gradle.jvmargs=-Xmx10240m -XX:MaxPermSize=4096m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

I found that the MacOS Azure images have the following specs:

Mac pros with a 3 core CPU, 14 GB of RAM, and 14 GB of SSD disk space

So, I set the Xmx flag (max JVM heap memory size) to a relatively high 10GB (10240m).

I don't actually know what XX:MaxPermSize does, but it is also related to memory. The other flags are just related to debugging.


This ended up only being half of my problem, but the other half isn't particularly relevant to my original question.

I originally placed all the blame on Azure, but I realized that I had only run the debug bundle locally, and the production bundle was also failing with the same error on my local machine.

My other problem was specific to React Native. It resulted in the same error though, so I imagine both are somehow related.

The issue was that I had upgraded to a more recent version of React Native, but missed some of the changes that had been made to android/app/build.gradle.

I doubt the specifics of what I happened to be missing would be very helpful to anyone, but if you run into this error, I suggest you check the React Native Upgrade Helper to see if you're missing anything in your build.gradle file.

Upvotes: 3

Related Questions