java.lang.RuntimeException: 无法实例化应用程序 com.example.MyApp: java.lang.ClassNotFoundException: 在路径上未找到类 "com.example.MyApp"
java.lang.RuntimeException: Unable to instantiate application com.example.MyApp: java.lang.ClassNotFoundException: Didn't find class "com.example.MyApp" on path: DexPathList[[zip file "/data/app/..."],nativeLibraryDirectories=[/data/app/...]]
ID: android/multidex-keep-cannot-find-class
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Android 4.4 - 14 | active | — | — | — |
| Android Gradle Plugin 3.6 - 8.2 | active | — | — | — |
| multidex:1.0.3 | active | — | — | — |
根因分析
由于主 dex 文件未包含自定义 Application 类,MultiDex 应用程序类未加载;可能未在足够早的阶段调用 MultiDex.install(),或使用了旧版 multidex 但未正确配置 keep。
English
MultiDex application class not loaded because the main dex file does not include the custom Application class; MultiDex.install() may not be called early enough or using legacy multidex without proper keep configuration.
官方文档
https://developer.android.com/build/multidex解决方案
-
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')'. -
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); } }' -
Use 'androidx.multidex:multidex:2.0.1' and enable multidex in build.gradle: 'defaultConfig { multiDexEnabled true }' and set minSdkVersion to 21 if possible.
无效尝试
常见但无效的做法:
-
50% 失败
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.