Android Studio。无法正确包含外部库

时间:2014-05-20 17:02:32

标签: android jar compiler-errors android-studio

我试图添加到classpath外部库(this one)。我的方式:File -> Project Structure -> Dependencies tab -> Add file dependency。那是对的吗? Maven的图书馆列表没有Xstream。如果我点击add module dependency,则列表为空。

我只需要在classpath中添加3个jar文件 - xstream1.4.7.jarxmlpull-1.1.3.1.jarxpp3_min-1.1.4c。所以我按照以前写的那样做了。事情是,在此之后,我可以包括这个库,一切似乎都好。但在运行项目后出现错误:

    Error:Execution failed for task ':gridview:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    D:\adt-bundle-windows-x86_64-20140321\sdk\build-tools\android-4.4.2\dx.bat --dex --output D:\Mobile devices\Course Work\android studio\gridview\gridview\build\dex\debug D:\Mobile devices\Course Work\android studio\gridview\gridview\build\classes\debug D:\Mobile devices\Course Work\android studio\gridview\gridview\build\dependency-cache\debug D:\Mobile devices\Course Work\android studio\gridview\gridview\build\pre-dexed\debug\classes-80f8711ee653afcf7efe6b620bb026841cb6946a.jar D:\Mobile devices\Course Work\android studio\gridview\gridview\build\pre-dexed\debug\support-v4-19.1.0-269b660d78e582ad08c396ce1a171205a775b0a0.jar D:\Mobile devices\Course Work\android studio\gridview\gridview\build\pre-dexed\debug\xmlpull-1.1.3.1-e45616f42a2838037b23e662d2381fe8ba29f039.jar D:\Mobile devices\Course Work\android studio\gridview\gridview\build\pre-dexed\debug\xpp3_min-1.1.4c-26abc774a3b821df830ea21d4b6b0daf1ce5acb6.jar D:\Mobile devices\Course Work\android studio\gridview\gridview\build\pre-dexed\debug\xstream-1.4.7-12d1ec007d50d7a5412e6ce3abef54fbab1046d4.jar D:\Mobile devices\Course Work\android studio\gridview\gridview\build\pre-dexed\debug\xstream-benchmark-1.4.7-574037033c1552eddf3ff5624f95bc8d3d954d28.jar
Error Code:
    2
Output:
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lorg/xmlpull/v1/XmlPullParser;
        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)

此外,我在Intelij中使用常见的java测试了这个库,而不是android。这非常有效。所以我只是将工作代码从Intelij复制到Android Studio中,并且发生了错误。

以下是发生此错误的代码(如果需要):

FileReader reader = new FileReader("boards.xml");  // load file
XStream xstream = new XStream();
xstream.processAnnotations(Boards.class);
xstream.processAnnotations(IdBoards.class);
Boards boards = (Boards) xstream.fromXML(reader); // parse xml into array list

所以问题是如何正确地将外部库添加到Android Studio?或者还有其他一些问题?谢谢你的建议。

3 个答案:

答案 0 :(得分:5)

xml pull parser是android框架的一部分,删除xmlpull-1.1.3.1.jar并尝试它可能有用。

答案 1 :(得分:3)

我使用exclude中的app/build.gradle选项修复了它,现在工作正常。

dependencies {
    .....
    compile ('com.thoughtworks.xstream:xstream:1.4.7'){
        exclude group: 'xmlpull'
    }
}

答案 2 :(得分:0)

如何将外部库添加到Android Studio

  • 项目视图 Android 更改为项目
  • 将外部库添加到" libs "的目录中。的 你想把jar文件放进的项目。
  • 选择" libs / xxx.jar"然后右键单击,选择" 添加为 文库"

然后检查 build.gradle 依赖关系,您会发现jar文件已成功添加。