How to resolve Java heap space errors in Codemagic builds

Last updated: December 22, 2025

Java heap space errors occur when memory-hungry Gradle tasks are executed during your build process. This happens when the allocated memory is insufficient for the build requirements.

Solution Options

There are several ways to resolve this issue:

1. Increase Java heap size using environment variables

Add the JAVA_TOOL_OPTIONS environment variable to your codemagic.yaml file and set it to allocate more memory:

workflows:
  android-workflow:
    environment:
      vars:
        JAVA_TOOL_OPTIONS: "-Xmx5g"

2. Switch to Linux machines (Paid plans)

If you're on a paid plan, you can switch to Linux machines which have 32GB RAM by using instance_type: linux_x2 in your workflow configuration.

3. Upgrade to macOS M4 machines

For more demanding builds, consider upgrading to more powerful macOS M4 machines with higher specifications.

Additional Resources

For more detailed information about Java heap space errors and solutions, you can read the comprehensive blog post: Java heap space error and how Codemagic helps to remediate it

Try implementing the environment variable solution first, as it's the quickest fix for most cases. If the issue persists, consider upgrading your machine type for additional memory resources.