android runtime_error ai_generated true

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

Also available as: JSON · Markdown · 中文
82%Fix Rate
88%Confidence
1Evidence
2023-03-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Android 4.4 - 14 active
Android Gradle Plugin 3.6 - 8.2 active
multidex:1.0.3 active

Root Cause

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.

generic

中文

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

Official Documentation

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

Workarounds

  1. 85% success 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')'.
    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. 90% success 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); } }'
    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. 95% success Use 'androidx.multidex:multidex:2.0.1' and enable multidex in build.gradle: 'defaultConfig { multiDexEnabled true }' and set minSdkVersion to 21 if possible.
    Use 'androidx.multidex:multidex:2.0.1' and enable multidex in build.gradle: 'defaultConfig { multiDexEnabled true }' and set minSdkVersion to 21 if possible.

中文步骤

  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.

Dead Ends

Common approaches that don't work:

  1. 50% fail

    The Application class must be explicitly kept in the main dex; simply adding other classes does not solve the instantiation failure.

  2. 50% fail

    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.

  3. 50% fail

    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.