Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Android build fails: too many references for one DEX file

Errortrouble writing output: Too many field references: 131000; max is 65536.

Short answer

One DEX file can hold at most 65,536 references. If minSdk is 21 or higher, multidex is already on. If it is 20 or lower, set multiDexEnabled to true, add androidx.multidex and use MultiDexApplication. Before that, remove unused libraries and turn on R8 shrinking, which often brings the count back under the limit.

  • Applies to Android Gradle Plugin 8.x, Gradle 8 and 9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read your minSdk. The fix depends on whether it is 20 or lower, or 21 and higher.

    grep -n "minSdk" app/build.gradle*
  2. 2Match what you see, then jump to its fix

    1. 1The error appears for a build with minSdk 20 or belowminSdk is 20 or lower and multidex is not enabledFix
    2. 2The count jumped after adding one library, and release shrinking is offLarge or unused dependencies fill the DEX fileFix
    3. 3java.lang.NoClassDefFoundError at start-up in a multidex appApp crashes with NoClassDefFoundError after multidex is onFix

What does the error look like?

  • trouble writing output: Too many field references: 131000; max is 65536.
  • You may try using --multi-dex option.
  • Unable to execute dex: method ID not in [0, 0xffff]: 65536

Cause 1 of 3

minSdk is 20 or lower and multidex is not enabled

1Check

Check that multiDexEnabled and the multidex library are missing from the module file.

grep -n "multiDex\|multidex" app/build.gradle* app/src/main/AndroidManifest.xml

2Fix

Enable multidex, add the library, and use MultiDexApplication (or call MultiDex.install in attachBaseContext if you cannot change the base class).

// app/build.gradle.kts
android { defaultConfig { multiDexEnabled = true } }
dependencies { implementation("androidx.multidex:multidex:2.0.1") }

<!-- AndroidManifest.xml, if you have no custom Application class -->
<application android:name="androidx.multidex.MultiDexApplication" ...>

3Verify

Build again; the APK should then contain classes.dex and classes2.dex.

./gradlew assembleRelease
unzip -l app/build/outputs/apk/release/*.apk | grep classes

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 3

Large or unused dependencies fill the DEX file

1Check

List the dependency tree and look for a large library used for a few helpers.

./gradlew :app:dependencies

2Fix

Drop or replace the heavy dependency, and enable code shrinking with R8 for release builds so unused code is removed. (With AGP 9.3 and higher the new optimization block does the same.)

android {
    buildTypes {
        release {
            isMinifyEnabled = true
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
}

3Verify

Build the release variant and confirm the error is gone.

./gradlew assembleRelease

Confirmed by developer.android.com

Did this fix it?

Cause 3 of 3

App crashes with NoClassDefFoundError after multidex is on

1Check

A class needed at start-up was not placed in the primary DEX file, usually because it is reached by reflection or from native code.

unzip -l app/build/outputs/apk/release/*.apk | grep classes

2Fix

List the missing classes in a keep file and point multiDexKeepProguard at it for that build type.

# multidex-config.pro
-keep class com.example.MyClass

// app/build.gradle.kts
buildTypes { getByName("release") { multiDexKeepProguard = file("multidex-config.pro") } }

3Verify

Rebuild and start the app from a cold launch.

./gradlew installRelease

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Review a new dependency's size before adding it.
  • Keep R8 shrinking on for release builds.
  • Raise minSdk to 21 when your users allow it; multidex is then on by default.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Android Studio cannot pair the phone over Wi-Fi

The Pair devices over Wi-Fi dialog lists no device

Wireless pairing needs the phone on Android 11 or higher and on the same Wi-Fi network as the computer, with current Platform-Tools.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.