在路径 DexPathList 上未找到类 "android.support.multidex.MultiDexApplication"。

65

我正在使用新的MultiDex Support尝试我的应用程序,到目前为止,我已成功编译了我的应用程序,但在运行时,我遇到了以下异常:

java.lang.RuntimeException: Unable to instantiate application android.support.multidex.MultiDexApplication: java.lang.ClassNotFoundException: Didn't find class "android.support.multidex.MultiDexApplication" on path: DexPathList[[zip file "/data/app/me.myapp.main-2.apk"],nativeLibraryDirectories=[/data/app-lib/me..main-2, /vendor/lib, /system/lib]]
    at android.app.LoadedApk.makeApplication(LoadedApk.java:507)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4382)
    at android.app.ActivityThread.access$1500(ActivityThread.java:139)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1270)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5086)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.multidex.MultiDexApplication" on path: DexPathList[[zip file "/data/app/me.myapp.main-2.apk"],nativeLibraryDirectories=[/data/app-lib/me.myapp.main-2, /vendor/lib, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
    at android.app.Instrumentation.newApplication(Instrumentation.java:998)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:502)

这是我的Gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 21
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

repositories {
    mavenCentral()
    mavenLocal()
}


dependencies {
    compile 'com.android.support:multidex:1.0.0'
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:21.0.0'
    compile 'com.android.support:support-v13:21.0.0'
    compile 'com.android.support:appcompat-v7:21.0.0'
}

我的 AndroidManifest.xml 文件:

<application
android:name="android.support.multidex.MultiDexApplication">

我不明白问题出在哪里。我认为我按照文档做了一切。还有其他需要注意的事项吗?我确保从SDK管理器安装了最新的支持库和仓库。


1
@PaulR 我有21.1版本的构建工具和堆栈跟踪属于调试版本。 - Roberto Betancourt
@dustedrob 奇怪。首先验证 MultiDexApplication 是否是您的 maindexlist 的一部分。此文本文件可以在 /app/build/intermediates/multi-dex/{debug/release}/maindexlist.txt 下找到。 - Alex Lipov
@dustedrob 很好。你能反编译你的主dex文件(从输出apk中提取它)来验证MultiDexApplication类是否实际存在于其中吗?你可以在这个答案中找到如何做到这一点:https://dev59.com/knM_5IYBdhLWcg3wvF3J#4177581 - Alex Lipov
@dustedrob 你能在一个新项目中重现它吗?还是只发生在特定的项目上?你可以分享这个项目吗(如果不行,也许可以删除所有专有代码,分享一个只有简单活动的项目)? - Alex Lipov
清理 -> 重建 在你认为已经按照所有步骤操作,但仍然出现相同错误时非常有用。 - Rohit Singh
显示剩余5条评论
15个回答

67

这个解决方案对我没有帮助,因为我使用的是Jetpack版本即AndroidX库。

按照官方文档doc进行操作。
并且
我不得不将名称更改为androidx....Multidex。

<application
            android:name="androidx.multidex.MultiDexApplication" >
        ...
</application>

希望这能帮助其他正在寻找使用Jetpack添加多Dex的人。


6
你的答案是正确的,许多人从文档中复制粘贴代码,却没有注意到他们正在使用 androidx。 - matshidis
你的解决方案也适用于使用Mono的Unity 2018版本,谢谢。 - VectorX
你是绝对正确的 @Man,我也遇到过几次从官方文档中复制支持库,结果发现我使用的是Jetpack。 - Ally
只有在不扩展Application(如文档中所述)时,此方法才能正常工作。如果您正在扩展Application,则需要执行以下操作:Application extends MultiDexApplication。 - Steve Moseley

26
在我的情况下,通过在清单中更改以下代码来解决问题:
android:name="android.support.multidex.MultiDexApplication"

为了

android:name="androidx.multidex.MultiDexApplication"

2
我迁移到了AndroidX,却忘了切换,真是个愚蠢的错误。谢谢。 - Fabii

19

我的配置:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.14.+' // 0.14.1, 2014-11-6
    }
}

dependencies {
    compile 'com.android.support:multidex:1.0.0'
}

android {
    compileSdkVersion = 21
    buildToolsVersion = "21.1.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 21
        multiDexEnabled true
    }
}

很遗憾,我也遇到了同样的问题。但是我发现了一个奇怪的情况:

build/intermediates/dex/debug:

-rw-rw-r--  1 andrew andrew  2221176 Nov  6 20:18 classes2.dex
-rw-rw-r--  1 andrew andrew  8357596 Nov  6 20:18 classes.dex

解压apk,路径为build/outputs/apk:

-rw-rw-r-- 1 andrew andrew 8357596 Nov  6 20:18 classes2.dex
-rw-rw-r-- 1 andrew andrew 2221176 Nov  6 20:18 classes.dex

在apk中,classes.dex的主要类应该比classes2.dex大,但实际上并非如此。我还使用dex2jar和unzip jar来检查类,发现应用程序类不在classes.dex中,而恰恰在classes2.dex中。

不过,我应该已经修复了这个问题。这是我打过补丁的android gradle插件,你可以尝试一下:

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://github.com/yongjhih/android-gradle-plugin.m2/raw/master/' }
    }
    dependencies {
        classpath 'com.infstory.tools.build:gradle:0.14.+'
    }
}

补丁已提交至:https://github.com/yongjhih/android-gradle-plugin/commit/9c2212e3b1b4c6e1f7b47f2086aba1903a6258bfhttps://android-review.googlesource.com/#/c/113331/

问题详见:https://code.google.com/p/android/issues/detail?id=78761

官方修复补丁在https://android-review.googlesource.com/#/c/113201/,已被合并。我认为这个问题可能会在下一版本中得到解决。

已在0.14.2版本(2014年11月10日)中修复。(来源:http://tools.android.com/tech-docs/new-build-system

Release notes:

0.14.2 (2014/11/10)

Fix potential multi-dex issue where the dex files could be renamed during packaging, leading to the wrong main dex file being used.
Fix versionNameSuffix support
Fix BuildType.initWith to copy shrinkResources flag
setup default proguard rule file if none are provided (SDK/tools/proguard/proguard-android.txt)
BuildType.pseudoLocalesEnabled flag to include fake locales in apk.

嘿,@Andrew Chen 和 @Roberto Betancourt,我尝试添加依赖项和存储库,但在构建时出现错误,错误信息为“Error(1,0) TEST_SDK_DIR”打开文件。 - XY-JOE
@XY-JOE,你使用最新的android-gradle插件吗?2.3.3还是3.0.1? - Andrew Chen
1
@AndrewChen 我正在使用Gradle 2.3.3,但我不确定我到底做了什么,但我不再遇到这个错误了。感谢您的帮助。 - XY-JOE
1
我甚至使用了AndroidX multidex v2.0.0,并将Application实现为MultidexApplication。但在低于API <= 19的设备上仍然崩溃。 - Neon Warge

9

将应用程序项目设置为使用multidex配置需要您根据应用程序支持的最低Android版本对应用程序项目进行以下修改。

如果您的minSdkVersion设置为21或更高版本,则只需在模块级build.gradle文件中将multiDexEnabled设置为true,如下所示:

android {
    defaultConfig {
        ...
        minSdkVersion 21 
        targetSdkVersion 26
        multiDexEnabled true
    }
    ...
}

然而,如果您的minSdkVersion设置为20或更低版本,则必须按以下方式使用multidex支持库:

  • Modify the module-level build.gradle file to enable multidex and add the multidex library as a dependency, as shown here:

    android {
        defaultConfig {
            ...
            minSdkVersion 15 
            targetSdkVersion 26
            multiDexEnabled true
        }
        ...
    }
    
    dependencies {
      compile 'com.android.support:multidex:1.0.1'
    }
    
  • Depending on whether you override the Application class, perform one of the following:

    • If you do not override the Application class, edit your manifest file to set android:name in the tag as follows:

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.myapp">
          <application
                  android:name="android.support.multidex.MultiDexApplication" >
              ...
          </application>
      </manifest>
      
    • If you do override the Application class, change it to extend MultiDexApplication (if possible) as follows:

      public class MyApplication extends MultiDexApplication { ... }
      
    • Or if you do override the Application class but it's not possible to change the base class, then you can instead override the attachBaseContext() method and call MultiDex.install(this) to enable multidex:

      public class MyApplication extends SomeOtherApplication {
        @Override
        protected void attachBaseContext(Context base) {
           super.attachBaseContext(base);
           MultiDex.install(this);
        }
      }
      

我的minSdkVersion设置为21,构建应用后出现了错误,无法找到android.support.multidex.MultiDexApplication类。 - Oliver D

7

对我来说,解决方案是切换到androidx版本。

android:name="androidx.multidex.MultiDexApplication"

implementation "androidx.multidex:multidex:2.0.1"


还要检查使用X multidex而不是legacy dex的库(com.android.support:multidex)。 - PanCrucian

7
我最近遇到了这个问题。尽管我的配置没有更改,但这个错误开始出现了。我尝试了所有建议的解决方案,包括删除虚拟设备并创建新的设备。
然而,我注意到如果我构建一个APK并将其拖入模拟器中安装,它就可以正常工作。
最终,清理项目并重新运行,然后它就可以工作了。

1
我按照文档上的步骤做了,但仍然遇到了相同的错误。我进行了“清理项目”->“重建项目”的操作,问题得以解决。谢谢。 - Rohit Singh

3
我使用最新的Android Studio并遵循官方文档添加多DEX支持https://developer.android.com/studio/build/multidex.html。但是我仍然遇到了相同的异常。我花了很多时间来解决它。最后,我发现原因是我启用了“Java异常断点 - 任何异常”和“异常断点 - 抛出任何异常”。当我禁用它们后,应用程序可以正常运行。
只需按照多DEX的官方文档https://developer.android.com/studio/build/multidex.html进行操作即可。它会有效果。

2
在我的情况下,我只改变了build.gradle文件,而保留AndroidManifest不变:
android:name=".MainApplication"

我正在使用React Native。


2

我刚刚在清单文件中更改了应用程序

android:name="android.support.multidex.MultiDexApplication"

android:name="androidx.multidex.MultiDexApplication"添加到代码中,它可以正常工作。


2
Delete ./gradle .idea and build directory
Clean project
Uninstall app if already install on device and again install

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接