android build_error ai_generated true

错误:无法将请求的类放入单个 dex 文件中。请尝试提供 main-dex-list。

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

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

其他格式: JSON · Markdown 中文 · English
90%修复率
86%置信度
1证据数
2023-04-05首次发现

版本兼容性

版本状态引入弃用备注
Android 5.0 (API 21) active
Android 6.0 (API 23) active
Android Gradle Plugin 7.0 active
Android Gradle Plugin 8.0 active

根因分析

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

English

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

官方文档

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

解决方案

  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 限制以下。

无效尝试

常见但无效的做法:

  1. 60% 失败

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

  2. 90% 失败

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