Android Dex Limit: The NoClassDefFoundError Avalanche
Devsolved
Apr 5, 2024 —
16:00 UTC
MTTR:
9 hours
2 min read
88002 views
Executive Summary
Adding a massive advertising SDK to our Android app pushed our codebase over the Dalvik 64K method reference limit. Because multidex was misconfigured, the app crashed immediately on startup for thousands of users.
App Impact: The v2.4.0 release had a 100% crash rate on launch for users on older Android devices. Ratings dropped from 4.8 to 3.2 stars.
The Symptom
Symptom
Immediately after rolling out the new update to the Google Play Store, Crashlytics exploded. Thousands of crashes were logged with `java.lang.NoClassDefFoundError: Failed resolution of: Lcom/ourcompany/app/MainActivity;`. The app was literally forgetting that its own main screen existed.
Root Cause Analysis
Root
Cause
The Android Dalvik Executable (DEX) bytecode format historically limits a single `.dex` file to referencing a maximum of 65,536 methods (the 64K limit). We added a bulky third-party Ad SDK which contained 30,000 methods, pushing our total to 85,000. The build system automatically split our code into two files: `classes.dex` and `classes2.dex`. However, on older Android versions (pre-Lollipop), the OS only loads `classes.dex` on startup. Because our `MainActivity` was randomly placed into `classes2.dex` by the compiler, the OS couldn't find it when the app launched, resulting in a fatal crash.
5 Whys Root Cause Drill-Down
1
Why #1
2
Why #2
3
Why #3
4
Why #4
5
Why #5
Resolution
Resolution
We explicitly integrated the Google `multidex` support library and modified our Application class to extend `MultiDexApplication`. This forces the app to manually load `classes2.dex` during the initialization phase, before `MainActivity` is invoked. We also utilized ProGuard (R8) to aggressively strip unused methods from the Ad SDK, bringing our total method count back under 60,000.
Code
javascript
// The fix: Enabling multidex in build.gradle
android {
defaultConfig {
multiDexEnabled true
}
}
dependencies {
implementation "androidx.multidex:multidex:2.0.1"
}
Preventive Action Items
criticalRollback v2.4.0 in Play Store and deploy hotfix@Mobile Team
highConfigure Firebase Test Lab to run automated UI tests on legacy OS versions (API 19+)@QA Team
|
Discussions
0
Most recent
Embed Incident
Copy the HTML below to embed this postmortem on your company blog, Notion, or internal docs.
Discussions 0