Multiple Dex Files Define Lorg/Apache/Cordova/Buildhelper

Multiple dex files define Lorg/apache/cordova/BuildHelper

Most likely you are using the newly released cordova-android@6.3.0, which now includes BuildHelper.java (as noted in your error message) and PermissionHelper.java, but you still have the deprecated cordova-plugin-compat in your project which is causing the build to fail because it also contains these classes.

To fix this, remove cordova-plugin-compat from your project to uninstall these Java files from the cordova-android platform project::

cordova plugin rm cordova-plugin-compat --force

Update

To persist this change add cordova-plugin-compat@1.2 which includes an engine constraint to prevent the Java files being re-installed into the cordova-android@6.3+ platform:

cordova plugin add cordova-plugin-compat@1.2

Another update (copypasted from comments)

After removing and adding cordova-plugin-compat@1.2, instead of removing and adding entire Android platform you can only remove files BuildHelper.java and PermissionHelper.java from folder platforms/android

Cordova Error on Build Only for Android: com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;

Had the same problem, adding the following lines to the build.gradle file located in platforms/android did it for me.

configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}

I copied it after the line:

apply plugin: 'android'

Hope this helps.

Unable to execute dex: Multiple dex files define Lorg/apache/cordova/App$1

That error occurs when you have more than one reference to the same class in the same package.

For example you might have a class:

com.android.foo.bar.GenericClass

And then you have two jars:

tools_library.jar

more_tools_library.jar

And they both need the "GenericClass" - but one uses a different version of the class from another. In pseudo code, you might think of it like:

tools_library.jar:com.android.foo.bar.GenericClass != more_tools_library.jar:com.android.foo.bar.GenericClass

and you need it to be:

tools_library.jar:com.android.foo.bar.GenericClass == more_tools_library.jar:com.android.foo.bar.GenericClass

A common cause of this problem is using two different versions of a package like android-support-v13.jar or android-support-v4.jar which has happened to me more than few times.

It can also happen if one of the jars includes an old version of android-support-v4.jar and you need a newer version or android-support-v13.jar

While this may not specifically answer your question (there isn't enough information to figure out which specific jar or class is causing the conflict), I thought it might at least be helpful to provide a more descriptive explanation of the error compared to "Unable to execute dex" - which took me a long time to decrypt when I first saw it.

D8: Program type already present: org.apache.cordova.BuildHelper

I was able to fix the issue by removing those duplicated .java classes from the CordovaLib sub project.

UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define

The error occurs when you have the same library/directory included more than once in your build.gradle's dependencies. Ok, let’s say you have an App structure that looks like this:

Sample Image

So you have the main “app” and then you have a bunch of sub-apps/modules/libraries. The libraries are: 1) gene_test_library, 2) genes_nine_old_androids_library, & 3) swipe_list_view_library.

My name is Gene, so that’s why there are all these “gene” libraries.

Inside the build.gradle for “app”, I have:

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

compile project(':libraries:gene_test_library')
//compile project(':libraries:genes_nine_old_androids_library')
compile project(':libraries:swipe_list_view_library')
}

Inside the build.gradle for gene_test_library, I have nothing:

dependencies {
}

Inside build.gradle for gene_nine_old_androids_library, I have:

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

Inside build.gradle for swipe_list_view_library, I have:

dependencies {
compile 'com.nineoldandroids:library:2.4.0+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
}

This line of code compile fileTree(dir: 'libs', include: ['*.jar']) just means “hey, look inside the ‘libs’ folder inside this module for any jar files. I do not have anything in the libs folder of any of the modules so you can ignore that line of code.

So let’s say I uncomment out //compile project(':libraries:genes_nine_old_androids_library')
In the build.gradle for the “app” module. Then I would get the “UNEXPECTED TOP-LEVEL EXCEPTION:” error. Why is that?

Sample Image

Well, writing //compile project(':libraries:genes_nine_old_androids_library') inside the build.gradle for “app”, is the same as taking the build dependencies of “genes_nine_old_androids_library” module and putting it there. So uncommenting the //compile project(':libraries:genes_nine_old_androids_library') statement, the build.gradle for “app” module becomes:

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

compile project(':libraries:gene_test_library')
***compile fileTree(dir: 'libs', include: ['*.jar'])***
***compile 'com.android.support:appcompat-v7:21.0.0'***
compile project(':libraries:swipe_list_view_library')
}

Notice how now compile 'com.android.support:appcompat-v7:21.0.0' shows up 2x. That’s where the error is coming from.

Cordova Android build error :transformClassesWithDexForDebug FAILED

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

This indicates multiple versions of the Android Support library are causing a collision. Most likely you have 2 or more plugins requesting different versions via their Gradle config. The simplest solution is to add the cordova-android-support-gradle-release plugin to your Cordova project which will force Gradle to use the same version in all cases:

cordova plugin add cordova-android-support-gradle-release


Related Topics



Leave a reply



Submit