android build_error ai_generated true

Error: Cannot fit requested classes in a single dex file. Try supplying a main-dex-list.

ID: android/multidex-main-dex-list-error

Also available as: JSON · Markdown · 中文
90%Fix Rate
86%Confidence
1Evidence
2023-04-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Android 5.0 (API 21) active
Android 6.0 (API 23) active
Android Gradle Plugin 7.0 active
Android Gradle Plugin 8.0 active

Root Cause

The number of methods in the app exceeds the 64K limit per dex file, and the build system cannot determine which classes must go into the primary dex.

generic

中文

应用中的方法数超过了每个 dex 文件的 64K 限制,且构建系统无法确定哪些类必须进入主 dex。

Official Documentation

https://developer.android.com/studio/build/multidex

Workarounds

  1. 90% success Create a main-dex-list.txt file in the app's root directory listing classes that must be in the primary dex (e.g., Application subclass, Activity, Service). Then configure build.gradle: 'multiDexKeepProguard file('main-dex-list.txt')'.
    Create a main-dex-list.txt file in the app's root directory listing classes that must be in the primary dex (e.g., Application subclass, Activity, Service). Then configure build.gradle: 'multiDexKeepProguard file('main-dex-list.txt')'.
  2. 85% success Enable multidex in build.gradle: 'defaultConfig.multiDexEnabled true' and add the multidex dependency: 'implementation 'androidx.multidex:multidex:2.0.1''. For apps targeting API < 21, ensure the Application class extends MultiDexApplication.
    Enable multidex in build.gradle: 'defaultConfig.multiDexEnabled true' and add the multidex dependency: 'implementation 'androidx.multidex:multidex:2.0.1''. For apps targeting API < 21, ensure the Application class extends MultiDexApplication.
  3. 80% success Use ProGuard or R8 to shrink the code and remove unused methods, reducing the overall method count below the 64K limit.
    Use ProGuard or R8 to shrink the code and remove unused methods, reducing the overall method count below the 64K limit.

中文步骤

  1. 在应用根目录创建 main-dex-list.txt 文件,列出必须放入主 dex 的类(例如 Application 子类、Activity、Service)。然后在 build.gradle 中配置:'multiDexKeepProguard file('main-dex-list.txt')'。
  2. 在 build.gradle 中启用 multidex:'defaultConfig.multiDexEnabled true' 并添加 multidex 依赖:'implementation 'androidx.multidex:multidex:2.0.1''。对于目标 API < 21 的应用,确保 Application 类继承 MultiDexApplication。
  3. 使用 ProGuard 或 R8 进行代码压缩,移除未使用的代码,将总方法数减少到 64K 限制以下。

Dead Ends

Common approaches that don't work:

  1. 60% fail

    For apps with very large method counts, automatic resolution may fail if certain classes must be in the primary dex for startup.

  2. 90% fail

    This is an extreme measure that breaks functionality; the real fix is proper multidex configuration.