如果我从命令行运行gradle assembleDebug,我会突然收到此错误:
gradle assembleDebug
UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dx.util.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl; at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:592) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:550) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:531) at com.android.dx.merge.DexMerger.mergeDexBuffers(DexMerger.java:168) at com.android.dx.merge.DexMerger.merge(DexMerger.java:186) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:300) at com.android.dx.command.dexer.Main.run(Main.java:232) at com.android.dx.command.dexer.Main.main(Main.java:174) at com.android.dx.command.Main.main(Main.java:91)
如果我 grep for v4,我会在我的构建文件夹中看到两个文件。
Binary file build/pre-dexed/debug/support-v4-19.0.0-2ba5fdd60a6c3836b3104a863fe42897da1fa9d1.jar matches Binary file build/pre-dexed/debug/support-v4-r7-227d905d79b23b20866531d4f700446c040a2ccb.jar matches
我的 gradle 文件只包含这个支持库:
compile 'com.android.support:support-v13:19.0.0'
我对如何以某种方式包含 r7 库感到困惑。我已经运行gradle clean了,当我重新运行 assembleDebug 时它总是出现在那里。
gradle clean
如果我在构建目录中对 r7 进行 grep,我会在文件中看到它:
Binary file build/exploded-bundles/ComGoogleAndroidGmsPlayServices4030.aar/classes.jar matches
如果我不包含 v13,那么其他东西就不会编译。
但是 v13 不包含 v4 支持库吗?
这是播放服务 AAR 包和 v13 库之间的不兼容吗?
我从 gradleplease.appspot.com 获取了 gradle 文件。
删除播放服务并不能解决问题;同样的错误。
我在 build.gradle 中的依赖项:
dependencies { // Google Play Services //compile 'com.google.android.gms:play-services:4.0.30' // Support Libraries //compile 'com.android.support:support-v4:19.0.0' ///compile 'com.android.support:appcompat-v7:19.0.0' //compile 'com.android.support:gridlayout-v7:19.0.0' compile 'com.android.support:support-v13:19.0.0' compile 'org.eclipse.mylyn.github:org.eclipse.egit.github.core:2.1.5' compile 'commons-codec:commons-codec:1.9' compile 'com.madgag:markdownj-core:0.4.1' compile 'com.wu-man:android-oauth-client:0.0.2' compile 'com.google.http-client:google-http-client-jackson2:1.17.0-rc' compile 'org.apache.commons:commons-lang3:3.2' compile 'com.google.code.gson:gson:2.2.4' }
运行 gradle -q dependencies (或 gradle -q :projectName:dependencies )以生成依赖关系报告。您应该看到r7来自哪里,例如:
gradle -q dependencies
gradle -q :projectName:dependencies
r7
compile - Classpath for compiling the main sources. +--- com.commonsware.cwac:camera-v9:0.5.4 | +--- com.actionbarsherlock:actionbarsherlock:4.4.0 | | \--- com.google.android:support-v4:r7 | +--- com.commonsware.cwac:camera:0.5.4 | \--- com.android.support:support-v4:18.0.+ -> 18.0.0 \--- com.android.support:support-v4:18.0.+ -> 18.0.0
然后,使用该exclude指令阻止该依赖项。就我而言,它来自我的 CWAC-Camera 库,因此我使用:
exclude
dependencies { compile('com.commonsware.cwac:camera-v9:0.5.4') { exclude module: 'support-v4' } compile 'com.android.support:support-v4:18.0.+' }
(第二个compile语句表示您实际想要的版本)
compile
这应该可以解决问题,因为您将看到是否再次运行依赖关系报告:
compile - Classpath for compiling the main sources. +--- com.commonsware.cwac:camera-v9:0.5.4 | +--- com.actionbarsherlock:actionbarsherlock:4.4.0 | \--- com.commonsware.cwac:camera:0.5.4 \--- com.android.support:support-v4:18.0.+ -> 18.0.0