APK errors

Dex limit: 65536 methods

Error text

Cannot fit requested classes in a single dex file (# fields: 70000 > 65536)
D8: The number of methods in the dex file exceeds 65536

Cause

One dex file can hold at most 65,536 methods. When the app and its libraries exceed it, D8 stops the build.

Step-by-step fix

  1. With minSdk 21 or higher just set multiDexEnabled = true.
  2. Below 21, add androidx.multidex:multidex and call MultiDex.install in your Application class.
  3. In release builds turn on R8 with isMinifyEnabled = true: unused methods are removed.
  4. Pick only the module you need from heavy libraries (for example, not all of Google Play Services).
  5. Open APK Analyzer to see which package takes the most room.

Example commands

android {
  defaultConfig {
    minSdk = 21
    multiDexEnabled = true
  }
}
dependencies { implementation("androidx.multidex:multidex:2.0.1") }   // yalnız minSdk < 21

How to prevent it

Add a big library through its specific module, not as a whole.

Author and license: Nibras Code