DexException:多个dex文件

时间:2015-02-05 02:28:45

标签: android android-studio google-drive-api android-gradle dex

好的,目前我正在开发一款Android应用,该应用利用 Google表格API 3.0版 适用于Java的Drive API客户端库 ,我需要该应用程序从用户驱动器帐户读取spreadhsheet并进行编辑。根据文档,我在我的/ lib文件夹中包含了以下jar(Android Studio 1.1)。
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.google.android.gms:play-services:6.5.87' compile files('libs/google-api-client-1.19.1.jar') compile files('libs/google-api-client-android-1.19.1.jar') compile files('libs/google-api-services-drive-v2-rev158-1.19.1.jar') compile files('libs/google-http-client-1.19.0.jar') compile files('libs/google-http-client-android-1.19.0.jar') compile files('libs/google-http-client-gson-1.19.0.jar') compile files('libs/google-oauth-client-1.19.0.jar') compile files('libs/gson-2.1.jar') compile files('libs/jackson-core-2.1.3.jar') compile files('libs/jackson-core-asl-1.9.11.jar') compile files('libs/jsr305-1.3.9.jar') compile files('libs/protobuf-java-2.4.1.jar') compile files('libs/mail.jar') compile files('libs/jsr305.jar') compile files('libs/guava-11.0.2.jar') compile files('libs/gdata-spreadsheet-meta-3.0.jar') compile files('libs/gdata-spreadsheet-3.0.jar') compile files('libs/gdata-docs-meta-3.0.jar') compile files('libs/gdata-docs-3.0.jar') compile files('libs/gdata-core-1.0.jar') compile files('libs/gdata-client-meta-1.0.jar') compile files('libs/gdata-client-1.0.jar') compile files('libs/additionnal.jar') compile files('libs/activation.jar') }

当我输入我的代码时一切正常,一切都是导入的适当但当我运行我的应用程序时,我得到了这个结果。

`What went wrong:
Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    C:\Users\Ricardo\AppData\Local\Android\sdk\build-tools\21.1.2\dx.bat --dex --output C:\Users\Ricardo\Google Drive\Development\Android\Updated\Private\Google_Fiasco\MyApplication\app\build\intermediates\dex\debug --input-list=C:\Users\Ricardo\Google Drive\Development\Android\Updated\Private\Google_Fiasco\MyApplication\app\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
    2
Output:

    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Ljavax/annotation/CheckForNull;
        at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
        at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
        at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
        at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
        at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
        at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
        at com.android.dx.command.dexer.Main.run(Main.java:246)
        at com.android.dx.command.dexer.Main.main(Main.java:215)
        at com.android.dx.command.Main.main(Main.java:106)
e`<br>

现在需要注意的是,当我创建两个单独的项目时,一个实现Spreadsheet API而另一个实现Drive API没有错误发生。只有当我在同一个项目中实现两个API时才会出现错误,即组合两个库中的jar。在修补罐子后,我发现错误只发生在jsr305.jar&amp; jsr305-1.3.9.jar包括在内。看着这两个罐子,我看到它们都有CheckForNull.java个文件,错误消息Multiple dex files define Ljavax/annotation/CheckForNull中引用了该文件。
所以我的问题是: 如何解决此错误? 我想我必须在依赖项中排除它?可以理解精确的说明,也许可以解释解决方案的作用。还有最后一件事在将jar文件放入my / lib文件夹并将其添加到build.gradle中的依赖项之间有什么区别?它似乎不会影响任何事情。 删除任一jsr.jat文件的结果如下:
UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536 at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:502) at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:277) at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:491) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:168) at com.android.dx.merge.DexMerger.merge(DexMerger.java:189) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303) at com.android.dx.command.dexer.Main.run(Main.java:246) at com.android.dx.command.dexer.Main.main(Main.java:215) at com.android.dx.command.Main.main(Main.java:106)

3 个答案:

答案 0 :(得分:2)

只需清理项目并重新构建......只要我看到“dex”这个词,我就知道我的代码与bug无关。在错误文本中

答案 1 :(得分:1)

此错误是由项目中引用的方法太多引起的。 You can read more here.

通常,这意味着您需要删除未使用的库,或者甚至在调试版本中使用proguard来减少引用的方法数量。您也可以使用文章中提到的multi-dex,但它会使您的项目更复杂。

答案 2 :(得分:0)

我与应用react-native进行了build.gradle项目:

def enableProguardInReleaseBuilds = false

...

buildTypes {
    release {
        minifyEnabled enableProguardInReleaseBuilds
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
    }
}

enableProguardInReleaseBuilds设为true对我有用:

def enableProguardInReleaseBuilds = true