'Unable to merge dex' error in Android Stu

2019-08-26 05:36发布

问题:

I'm trying to add a specific module into my Android project (this one over here: https://github.com/danysantiago/sendgrid-android), but while the project seems to build correctly, I get the following error when I try to run the project:

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

I'm able to confirm that it's this specific module that is causing the error, since the project works if I comment the following line out of the gradle:

compile 'com.github.danysantiago:sendgrid-android:1'

What I've tried so far:

  1. I've cleaned and rebuilt the project.
  2. Added multiDexEnabled true to android{defaultConfig{}}
  3. Added implementation 'com.android.support:multidex:1.0.2' to dependencies{}

Unfortunately, none of those items worked, and I wasn't able to find any other solutions on StackOverflow that solved the issue. Any help with this issue would be greatly appreciated!

回答1:

You need to exclude the httpclient from the library with this:

compile ('com.github.danysantiago:sendgrid-android:1'){ 
   exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}


回答2:

Update in your gradle

 defaultConfig {
  ..............

multiDexEnabled true


}

dexOptions should add..

dexOptions {
   //incremental = true;
preDexLibraries = false
javaMaxHeapSize "4g"
 }

dependency add

 compile 'com.android.support:multidex:1.0.1'

Also In Your AndroidManifest.xml add this lines android:name

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name="android.support.multidex.MultiDexApplication"
>