Android build fails: too many references for one DEX file
trouble 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.
Fix it fast
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*2Match what you see, then jump to its fix
- 1
The error appears for a build with minSdk 20 or belowminSdk is 20 or lower and multidex is not enabledFix - 2
The count jumped after adding one library, and release shrinking is offLarge or unused dependencies fill the DEX fileFix - 3
java.lang.NoClassDefFoundError at start-up in a multidex appApp crashes with NoClassDefFoundError after multidex is onFix
- 1
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.xml2Fix
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 classesConfirmed by developer.android.com
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:dependencies2Fix
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 assembleReleaseConfirmed by developer.android.com
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 classes2Fix
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 installReleaseConfirmed by developer.android.com
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
Flutter Android build fails because the app has too many methods
You may try using --multi-dex option.
One DEX file can reference at most 65,536 methods.
2 causes, each with a fixMobile apps
adb cannot start because something already holds its port
adb start-server failed
The adb server must bind local TCP port 5037, and every adb client talks to it there.
2 causes, each with a fixMobile apps
The phone shows as unauthorized and Android Studio will not run the app
adb devices lists the phone but Android Studio cannot deploy to it
An unauthorized phone means it has not trusted this computer's RSA key.
3 causes, each with a fixMobile 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 fixMobile apps
adb cannot connect to its daemon or start the server
* daemon started successfully *
The adb server is a background process on your computer that listens on TCP port 5037.
2 causes, each with a fixMobile apps
adb prints 'server version doesn't match this client; killing...'
adb server version doesn't match this client; killing...
adb runs a background server, and every adb command is a client.
3 causes, each with a fix