Tran Duc Huy Tran Duc Huy - 2 months ago 38
Android Question

Multiple dex files define Landroid/support/v4/ in android studio

I can

integrate (add & work) with many libraries
.

But as for
ActionBarSherlock
library, I have so many problems with it.

First Issue is the popular issue
Multiple dex files define
,

I know The
duplicated android-support-v4.jar
causes it, but I can not solve this issue now :

Error:Execution failed for task ':fitness:dexDebug'.

Error Code:
2
Output:
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:594)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)


I followed the answer in same error. It used :

dependencies {
compile('...') {
exclude module: 'support-v4'
}

compile 'com.android.support:support-v4:18.0.+'
}


But I received Second Issue :
unsupported Gradle DSL method found: 'exclude()'!
error.
Therefore I changed
build.gradle
of
main module
like this:

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

dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':google-play-services')
compile files('/google-play-services/libs/google-play-services.jar')
compile (project(':actionbarsherlock')) {
exclude(module: 'support-v4')
}
compile 'com.android.support:support-v4:18.0.+'
}


Solved
Second Issue, But still get First Issue
Multiple dex files define
error.

I read many topics, but can not solve First Issue now,

Please help me,

Thanks,

p/s : I can sync with Gradle successully, but when run, it get above errors.

I used Android studio 0.5.5

EDIT

build.gradle
of
ActionBarSherlock
, it has only one
android-support-v4.jar
:

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


setting.gradle
like this :

include ':fitness', ':google-play-services',':actionbarsherlock'


@Garbriele : I updated new question, show
build.gradle of project
and
build.gradle of main module

build.gradle
of
project
:

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}


build.gradle
of
main module
after edited :

dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar' // Get following error in this line
}


I got this error in below :

enter image description here

Okay, thanks for @Garbriele comment, I already fixed this by using this :

repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar' // Get following error in this line
}


EDIT 2 When I put like this :

repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.google.android.gms:play-services:+'
}


Still get First Issue
Multiple dex files define
error.

What I want now is how to add
com.google.android.gms:play-services
library via
Maven
successfully,

Please help me,

Thanks,

UPDATE

After followed these steps :


  • Put the library via Maven in
    build.gradle
    of main module :
    compile 'com.google.android.gms:play-services:+'

  • Restart Android Studio

  • Sync project with gradle



No longer get this error again.

Answer

So strange,

When I setup my dependencies like this :

repositories {
            mavenCentral()
        }
dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.google.android.gms:play-services:+'
}

I still get the First Issue Multiple dex files define error.

But after following these steps:

  • Put the library via Gradle in build.gradle of main module : compile 'com.google.android.gms:play-services:+'

  • Restart Android Studio

  • Sync project with gradle

I no longer get this error.

Comments