Android Studio. Can't include external library

2020-03-18 02:40发布

问题:

I'm trying to add to classpath external library (this one). The way I do it: File -> Project Structure -> Dependencies tab -> Add file dependency. Is that correct? Maven's list of libraries doesn't have Xstream. And if I click add module dependency the list is empty.

All I need is to add 3 jar files to classpath - xstream1.4.7.jar, xmlpull-1.1.3.1.jar and xpp3_min-1.1.4c. So I did it as wrote before. The thing is that after this I can include this library and everything seems Ok. But after running the project there is an error:

    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)

Moreover, I have tested this library in Intelij with common java, not android. And that worked perfectly. So I just copied working code into Android Studio from Intelij and that error occurs.

Here's the code where this error occurs (if needed):

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

So the question is How to add external libraries properly to Android Studio? Or probaby there's some other problem? Thanks for advices.

回答1:

xml pull parser is a part of android framework, remove xmlpull-1.1.3.1.jar and try It might work.



回答2:

I fixed it by using exclude option in app/build.gradle, it works fine now.

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


回答3:

How to add external libraries into Android Studio:

  • change Project View from Android to Project.
  • add the external library into the directory of "libs" of the project you want put the jar file into.
  • select the "libs/xxx.jar" and right-click, select "Add as a library".

Then check dependencies of build.gradle, you will find jar file is added successfully.