Why does my Android build fail with Java heap space errors on Codemagic?
Last updated: July 24, 2026
Context
When running an Android release build on Codemagic, you may encounter a java.lang.OutOfMemoryError: Java heap space error, or builds that time out without a clear failure message. This often happens when multiple JVM heap settings are defined in different places (e.g., GRADLE_OPTS, JAVA_TOOL_OPTIONS, and gradle.properties), causing conflicts or over-allocation of memory.
Answer
The root cause is typically conflicting JVM heap configurations spread across multiple locations. To resolve this, consolidate your heap settings into a single, consistent configuration:
Update your CI-specific Gradle properties file (e.g.,
android/gradle-ci.properties) to include the heap setting directly:org.gradle.jvmargs=-Xmx5g -XX:MaxMetaspaceSize=1g -Dfile.encoding=UTF-8 org.gradle.parallel=false org.gradle.workers.max=1Set
JAVA_TOOL_OPTIONSin yourcodemagic.yamlto a single value that matches the above:JAVA_TOOL_OPTIONS: "-Xmx5g"Remove any separate
GRADLE_OPTSheap setting (e.g.,-Xmx8g) from your environment variables or scripts. Having bothGRADLE_OPTSandJAVA_TOOL_OPTIONSdefine heap sizes can cause Gradle to receive conflicting instructions.Use only one source of truth for heap configuration. If your CI Gradle properties file replaces the default
gradle.propertiesat build time, make sure the heap setting is defined there — not in a separate environment variable.
Note: It is normal to have different Gradle properties files for different environments (e.g., a local machine with 8 GB RAM vs. a Codemagic machine with 16 GB). Just ensure that the file used during the Codemagic build contains the correct heap value for that environment, and that no conflicting overrides exist elsewhere.
Additionally, if your build succeeds when run manually via SSH but fails when triggered from the Codemagic console, check whether a warmed Gradle cache or a running Gradle daemon is influencing the result. You can stop the daemon before re-running to get a clean comparison:
cd android
./gradlew --stop
cd ..
source "$CM_ENV"
./ci-scripts/build.sh "$PLATFORM"If you also see llvm-strip errors in your build logs, these may indicate an issue with a native .so dependency and should be reviewed separately.