UNEXPECTED TOP-LEVEL EXCEPTION: Multiple dex files

2019-04-11 01:56发布

问题:

I've added a third lib to my android project. And when I pack it by ant, some errors occur:

 [dx] UNEXPECTED TOP-LEVEL EXCEPTION:
   [dx] com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
   [dx]     at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:593)
   [dx]     at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:551)
   [dx]     at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:532)
   [dx]     at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:169)
   [dx]     at com.android.dx.merge.DexMerger.merge(DexMerger.java:187)
   [dx]     at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
   [dx]     at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
   [dx]     at com.android.dx.command.dexer.Main.run(Main.java:230)
   [dx]     at com.android.dx.command.dexer.Main.main(Main.java:199)
   [dx]     at com.android.dx.command.Main.main(Main.java:103)

I've checked this problem and found this:Android Studio - UNEXPECTED TOP-LEVEL EXCEPTION: But my eclipse project does not have a build.gradle file, how can I solve the problem?

回答1:

This can be caused by both your app project and the library project bringing the android-support-v4 jar. If the library project is already bringing this jar as dependency then you'll need to exclude it from your app project classpath.