If you are getting an error like “16 KB Memory Issue” or messages such as:
Execution failed for task ':app:packageReleaseBundle'.
Error: Java heap space
or
FileSize exceeds limit — cannot allocate 16384 bytes (16 KB
This means that your build process is running out of memory.
When you build an app for release, the Android build tools (Gradle, Java, or the NDK) need enough memory to complete all packaging and compiling tasks. If your system does not have enough allocated memory, or if Gradle is not configured correctly, it can cause this “16 KB” memory issue.
This guide explains, in very simple words, how to fix it step-by-step — including which files to edit, what exact code to add, how to change the NDK version, and what commands to run.
It works for both Flutter and native Android projects.
Before You Start
Before you make any changes, do these basic safety steps:
Understanding the Main Causes
-
Gradle (the build system) is not getting enough memory.
-
The Android Gradle Plugin or Gradle version is outdated.
- The NDK version is not stable or does not match your dependencies.
- The project has too many large files or unoptimized resources.
Step 1: Increase Gradle and JVM Memory
# Increase Gradle and Java memory
org.gradle.jvmargs=-Xmx4096m -XX:MaxMetaspaceSize=1024m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# Optional: make Gradle faster
org.gradle.parallel=true
org.gradle.configureondemand=true
# Kotlin memory (if used)
kotlin.daemon.jvm.options=-Xmx2048m
Explanation
-
-Xmx4096m means Gradle can use up to 4 GB of memory. If your system has more than 16 GB of RAM, you can increase it to 6144m.
-
-XX:+HeapDumpOnOutOfMemoryError helps generate a file if Gradle runs out of memory, which can help debug later.
- The parallel build option makes Gradle work on multiple modules at the same time.
Step 2: Update Gradle and Android Gradle Plugin
A) Update the Gradle Wrapper
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-all.zip
B) Update the Android Gradle Plugin
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.0.2'
}
}
Step 3: Change or Fix the NDK Version
A) Choose a Stable Version
-
21.4.7075529 (recommended for compatibility)
-
23.1.7779620 (for newer builds)
B) Install NDK
- Go to Tools → SDK Manager → SDK Tools.
- Check “NDK (Side by side)”.
- Choose the version you want and click Apply.
Using Command Line:
sdkmanager "ndk;23.1.777962
C) Add to local.properties (optional)
Open <project-root>/local.properties and add:
sdk.dir=/Users/you/Library/Android/sdk
ndk.dir=/Users/you/Library/Android/sdk/ndk/23.1.7779620
(Change the path based on your OS.)
D) Pin NDK Version in Gradle
Open <project-root>/app/build.gradle and inside the android {} block, add:
android {
compileSdk 35
ndkVersion "23.1.7779620"
}
Now your project will always use this version.
Step 4: Optimize Packaging and Assets
A) Compress Large Files
-
Optimize images using TinyPNG or ImageOptim.
-
Move large video or data files to the cloud and load them when the app runs.
B) Exclude Extra Files
In app/build.gradle, inside android {}, add:
packagingOptions {
resources {
excludes += ['META-INF/DEPENDENCIES', 'META-INF/LICENSE', 'META-INF/NOTICE']
}
jniLibs {
useLegacyPackaging = true
}
}
C) Use Only Required ABIs
defaultConfig {
ndk {
abiFilters 'armeabi-v7a','arm64-v8a'
}
}
This prevents Gradle from building unnecessary native files for all architectures, which saves time and memory.
D) Shrink Resources (optional)
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
Step 5: Tune Gradle Workers
In gradle.properties:
org.gradle.workers.max=2
./gradlew assembleRelease --max-workers=2
Step 6: Clean and Rebuild
For Flutter projects:
flutter clean
flutter pub get
flutter build appbundle --release
For native Android:
./gradlew clean
./gradlew assembleRelease
If problems persist, clear the Gradle cache:
rm -rf ~/.gradle/caches/
rm -rf ~/.gradle/daemon/
Then rebuild again.
Step 7: Update Your CI or Build Server
Example GitHub Actions setup:
name: Android CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
env:
JAVA_TOOL_OPTIONS: "-Xmx4096m -Dfile.encoding=UTF-8"
steps:
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- name: Install Android SDK/NDK
run: |
yes | sdkmanager --licenses
sdkmanager "platform-tools" "platforms;android-35" "build-tools;35.0.2" "ndk;23.1.7779620"
- name: Build
run: ./gradlew assembleRelease -Dorg.gradle.jvmargs="-Xmx4096m" --max-workers=2
This ensures your CI environment has enough memory to build the project without errors.
Step 8: Sparkle Web’s Proven Setup
-
Same NDK version pinned for every developer and CI.
-
Optimized gradle.properties settings for faster builds.
- Docker-based build machines with high memory.
- Heap dump monitoring for advanced troubleshooting.
Step 9: Debugging Tips
./gradlew assembleRelease --stacktrace --info
Look for lines with OutOfMemoryError and check which task failed (e.g., DEX, AAPT2, or NDK).
Step 10: Final Checklist (Copy and Paste Ready)
org.gradle.jvmargs=-Xmx4096m -XX:MaxMetaspaceSize=1024m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.configureondemand=true
kotlin.daemon.jvm.options=-Xmx2048m
org.gradle.workers.max=2
android.enableBuildCache=true
gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-all.zip
build.gradle (project-level)
classpath 'com.android.tools.build:gradle:8.0.2'
app/build.gradle
android {
compileSdk 35
ndkVersion "23.1.7779620"
defaultConfig {
applicationId "com.example.app"
minSdk 23
targetSdk 35
multiDexEnabled true
}
packagingOptions {
resources {
excludes += ['META-INF/DEPENDENCIES', 'META-INF/LICENSE', 'META-INF/NOTICE']
}
jniLibs {
useLegacyPackaging = true
}
}
defaultConfig {
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
}
}
dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
}
local.properties
sdk.dir=/Users/you/Library/Android/sdk
ndk.dir=/Users/you/Library/Android/sdk/ndk/23.1.7779620
Step 11: If the Problem Still Happens
-
Try on a computer with more memory (at least 16 GB RAM).
-
Check large assets again and reduce them.
- Try splitting the build per ABI or module.
- If the issue still occurs, it could be a bug in the Android toolchain. You can file a report on the Android issue tracker with your logs.
Before Uploading to Play Console
Summary
-
Always use stable versions.
-
Keep your Gradle files optimized.
- Clean and rebuild regularly.
Conclusion
Need expert help fixing build or Play Console issues? Let us Flutter & DevOps team, streamline your process and eliminate build-time errors.
Mohit Kokane
A highly skilled Flutter Developer. Committed to delivering efficient, high-quality solutions by simplifying complex projects with technical expertise and innovative thinking.
Reply