在Android项目中使用Gradle解决依赖关系时出错

时间:2015-04-20 11:01:24

标签: android android-studio gradle android-gradle

我在Android Studio中的Gradle中配置了以下依赖项。

compile 'com.google.code.gson:gson:2.2.4'
compile files('libs/android-support-v13.jar')
compile files('libs/apache-mime4j-storage-0.7.2.jar')
compile files('libs/commons-cli-1.1.jar')
compile files('libs/commons-io-1.2.jar')
compile files('libs/httpclient-4.3.4.jar')
compile files('libs/httpcore-4.3.2.jar')
compile files('libs/httpmime-4.3.4.jar')
compile files('libs/rabbitmq-client.jar')
compile files('libs/retrofit-1.7.1.jar')
compile files('libs/universal-image-loader-1.9.3-with-sources.jar')
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
compile 'com.google.android.gms:play-services:7.0.0'

但是Gradle给了我以下错误:

Execution failed for task ':app:dexDebug'.

Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    D:\AS sdk\build-tools\21.1.2\dx.bat --dex --no-optimize --output C:\Users\ttuser\ASProjects\MyProject\app\build\intermediates\dex\debug --input-list=C:\Users\ttuser\ASProjects\MyProject\app\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
    2
Output:
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;
        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)

我该如何解决?

2 个答案:

答案 0 :(得分:1)

app:dexDebug error in general means that you've tried to link in the duplicate class to your project more than once。因此,请删除重复库

答案 1 :(得分:0)

我不知道你是否已经知道它,但是看到所有这些都是公开的库,我建议摆脱JAR并将它们作为JCenter / Maven Central的依赖。如果你不确定如何做到这一点,这里有一个例子。您正在使用的一个库是Retrofit,因此您可以访问Maven Central(http://search.maven.org/)等网站并搜索该库并获取您的版本的Gradle依赖项(http://search.maven.org/#artifactdetails|com.squareup.retrofit|retrofit|1.7.1|jar):

compile 'com.squareup.retrofit:retrofit:1.7.1'

为所有依赖项执行此操作。将它们设置为Gradle依赖项而不是JAR后,您可以执行此处说明的操作:https://stackoverflow.com/a/21100040/2245921

基本上,通过终端进入你的:app文件夹,然后运行以下命令:

../gradlew -q dependencies

Gradle将绘制一个依赖树。在那里,您将能够看到重复的依赖关系,并且更容易处理它们。