How to fix android Multiple dex files define exception

随声附和 提交于 2019-12-10 23:00:33

问题


Android 4.2 ZBarCodescan java application needs to compiled in Windows 7 x64 computer.

Its source code is located here.

Android Studio is installed, and code is imported using Github import command. In gradle file API was changed to 23

compileSdkVersion 23 

according to

How to use eclipse project from github for Android development

Android studio Build APK command produces exception

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:579)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:535)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:517)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:164)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
    at com.android.dx.command.dexer.Main.run(Main.java:277)
    at com.android.dx.command.dexer.Main.main(Main.java:245)
    at com.android.dx.command.Main.main(Main.java:106)

Trying to fix this by adding lines

dexOptions {
    preDexLibraries = false
}

to both project gradle lines according to

Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat

causes errors

Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoIcsImpl;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoJellyBeanMr2;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoStubImpl;

How to fix this ?

Answer here recommends to examine dependency tree. How to get this tree in Android Studio and how to fix it ?

After removing android-support-v4.jar exception

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:579)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:535)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:517)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:164)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
    at com.android.dx.command.dexer.Main.run(Main.java:277)
    at com.android.dx.command.dexer.Main.main(Main.java:245)
    at com.android.dx.command.Main.main(Main.java:106)

Still occurs.

Update. I tried to importfixes branch using https://github.com/cricket007/DeviceSDK/tree/fixes url but Test button and import produce error

How to import this branch ?


回答1:


I made a pull request to the library after getting it to work in Android Studio.

If you get this error when trying to build

SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

Or you don't see that, but any error saying local.properties is missing when doing an import, then add a local.properties file that points to your Android SDK location, then trying to import/rebuild.

For example on a Mac, that file would contain this line, but obviously change the file path to point at your respective SDK's location

sdk.dir=/usr/local/opt/android-sdk

Once done with that, it should load into Android Studio without a problem.

The code was also tested on a 4.2 emulator.


The easy way via a Git Terminal (could do the same with Git Desktop).

git clone https://github.com/cricket007/DeviceSDK /path/to/download/
cd /path/to/download/
git branch fixes

Then import /path/to/download/DeviceSDK into Android Studio as Import Project Gradle


The long way (via Android Studio)

Use https://github.com/cricket007/DeviceSDK.git as the Git repo

Open Project, you can ignore most errors that popup

Go the the Menu Bar, select VCS > Enable Version Control Integration

Choose Git

Go back to VCS > Git > Branches

Checkout fixes as a new local branch

Name the branch.

Choose Force Checkout if prompted

I don't know what clicking the Run button does with a Gradle project, but you should open Gradle View on the right side and open TestDemo, select installDebug

From here on you need the solution above for the local.properties file and enable ADB debugging on a physical device or test on an emulator.




回答2:


UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;

It happens becuase you are adding twice the same class.

In your case you are using:

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.android.support:appcompat-v7:23.1.1'
}

The appcompat-v7 has a dependency with the support-v4.
You have the android-support-v4.jar in your libs folders.

Remove the android-support-v4.jar (you don't need it because appcompat has already it).




回答3:


When I had the same error I had to comment out the following line in my build.gradle file:

compile fileTree(dir: 'libs', include: '*.jar')



来源:https://stackoverflow.com/questions/36103262/how-to-fix-android-multiple-dex-files-define-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!