# java.lang.RuntimeException: 无法实例化应用程序 com.example.MyApp: java.lang.ClassNotFoundException: 在路径上未找到类 "com.example.MyApp"

- **ID:** `android/multidex-keep-cannot-find-class`
- **领域:** android
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

由于主 dex 文件未包含自定义 Application 类，MultiDex 应用程序类未加载；可能未在足够早的阶段调用 MultiDex.install()，或使用了旧版 multidex 但未正确配置 keep。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Android 4.4 - 14 | active | — | — |
| Android Gradle Plugin 3.6 - 8.2 | active | — | — |
| multidex:1.0.3 | active | — | — |

## 解决方案

1. ```
   Add the custom Application class to the main dex keep list by creating a file 'multidex.keep' in the app directory with content: 'com/example/MyApp.class', then in build.gradle: 'multiDexKeepFile file('multidex.keep')'.
   ```
2. ```
   Override attachBaseContext() in the Application class to call MultiDex.install() before super.attachBaseContext(): 'public class MyApp extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } }'
   ```
3. ```
   Use 'androidx.multidex:multidex:2.0.1' and enable multidex in build.gradle: 'defaultConfig { multiDexEnabled true }' and set minSdkVersion to 21 if possible.
   ```

## 无效尝试

- **** — The Application class must be explicitly kept in the main dex; simply adding other classes does not solve the instantiation failure. (50% 失败率)
- **** — If the app still targets lower API levels or uses legacy multidex, the error persists; also, this may not be feasible for apps supporting older devices. (50% 失败率)
- **** — The root cause is a missing class in the main dex, which cannot be fixed by a simple rebuild; the multidex keep configuration is still incorrect. (50% 失败率)
