2014-10-09 68 views
2

我的build.gradle:不能编译测试与Robolectric

buildscript { 
    repositories { 
     mavenCentral() 
    } 

    dependencies { 
     classpath 'com.stanfy.spoon:spoon-gradle-plugin:0.10.0' 
    } 
} 


apply plugin: 'android-sdk-manager' 
apply plugin: 'android' 
apply plugin: 'spoon' 
apply plugin: 'robolectric' 

android { 
    compileSdkVersion 19 
    buildToolsVersion '20.0.0' 

    defaultConfig { 
     minSdkVersion 14 
     targetSdkVersion 19 
     testInstrumentationRunner 'com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner' 
    } 


    buildTypes { 
     debug { 
     } 
     release { 
     } 
    } 

    productFlavors { 
     first { 
     //just version codes and packages 
     } 
     second { 
     //just version codes and packages 
     } 
     third { 
     //just version codes and packages 
     } 
    } 
} 

spoon { 
    debug = true 
} 

dependencies { 
    compile project(':app:libs:facebookSDK') 
    compile 'com.google.android.gms:play-services:5.0.89' 
    compile 'com.android.support:support-v4:20.0.0+' 
    compile 'com.google.code.gson:gson:2.2.4' 
    compile fileTree(dir: 'libs', include: '*.jar') 
    androidTestCompile fileTree(dir: 'libsTest', include: '*.jar') 
    androidTestCompile 'com.squareup.spoon:spoon-client:1.1.0' 
    androidTestCompile 'junit:junit:4.+' 
    androidTestCompile 'org.robolectric:robolectric:2.3' 
} 

libsTest:浓咖啡的contrib-1.1-bundled.jar现在

,我的错误是以下几点:

Error Code: 
    2 
    Output: 
    UNEXPECTED TOP-LEVEL EXCEPTION: 
    com.android.dex.DexException: Multiple dex files define Lorg/hamcrest/SelfDescribing; 
     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) 

此处更详细的日志:

http://pastebin.com/Yye3cd1c

如何解决此问题?

UPD:我现在的问题基本上是如何狩猎这些重复的部分?

+0

一般来说,这意味着这个类出现在构建路径的多个位置(尽管它在junit的dependency子句中被明确排除)。我无法在我的设置中重现此问题;你可以在你的libsTest和libs文件夹中包含更多关于所有库的信息吗?这可能是罪魁祸首。 – 2014-10-09 17:52:47

+0

@ScottBarta我已经更新了我的问题。 – 2014-10-10 09:26:28

+0

仍然不为我重现。你将不得不四处寻找,找到重复该类的地方。 – 2014-10-10 15:51:32

回答

0

我有一个类似的问题,前一段时间,如果我没有记错,我通过添加排除语句如下固定它:

androidTestCompile('junit:junit:4.+') { 
    exclude module: 'hamcrest-core' 
} 
0

deckard-gradle sample project启发,我建议尝试这样的事:

androidTestCompile 'org.hamcrest:hamcrest-integration:1.1' 
androidTestCompile 'org.hamcrest:hamcrest-core:1.1' 
androidTestCompile 'org.hamcrest:hamcrest-library:1.1' 

androidTestCompile('junit:junit:4.11') { 
    exclude module: 'hamcrest-core' 
} 
androidTestCompile('org.robolectric:robolectric:2.3') { 
    exclude module: 'classworlds' 
    exclude module: 'commons-logging' 
    exclude module: 'httpclient' 
    exclude module: 'maven-artifact' 
    exclude module: 'maven-artifact-manager' 
    exclude module: 'maven-error-diagnostics' 
    exclude module: 'maven-model' 
    exclude module: 'maven-project' 
    exclude module: 'maven-settings' 
    exclude module: 'plexus-container-default' 
    exclude module: 'plexus-interpolation' 
    exclude module: 'plexus-utils' 
    exclude module: 'wagon-file' 
    exclude module: 'wagon-http-lightweight' 
    exclude module: 'wagon-provider-api' 
} 
1

My question now is basically how do I hunt those duplicated parts?

您使用日志来做到这一点。您粘贴的日志说:

com.android.dex.DexException: Multiple dex files define

这是最有可能是由于冲突的库。日志继续:

Lorg/hamcrest/SelfDescribing;

这里是矛盾的图书馆,hamcrest

在向项目添加依赖项后,如果存在库(或库)使用的公共子库,则会发生此错误。所以它看起来像hamcrest不仅被你的一个库使用。

我们将通过检查依赖性来了解这个冲突。当然,有效的检测同时需要既直观又合理。

让我们从hamcrest本身开始。 (我假设你以前没有听说过Hamcrest。)

让我们来看看Hamcrest project page。 Hamcrest将自己定义为用于构建测试表达式的匹配器库。定义说典型场景包括测试框架,嘲讽库 ...这是照亮,因为你有像JUnit,Espresso和Robolectric这样的测试的一些依赖关系。

现在我们应该继续JUnit dependencies。看起来像JUnit使用Hamcrest核心Here是我们的第一个作为子依赖的hamcrest。

让我们继续使用Espresso,因为您在libsTest文件夹中有espresso-contrib-1.1-bundled.jar。 当我们检查espresso-contrib项目dependencies时,我们可以看到Espresso大量使用了hamcrest。

我们可能会在您的项目中找到冲突的库,最后一步是从我们的某个依赖项中排除hamcrest-core,同时添加此依赖项。你可以做到这一点:

androidTestCompile('junit:junit:4.+') { 
    exclude module: 'hamcrest-core' 
} 
0

一招追捕这些错误是使用Android Studio的“导航>类...”选项。

鉴于你有这样的错误:

Lorg/hamcrest/SelfDescribing; 

,那么你可以搜索“org.hamcrest.SelfDescribing”这样你就可以使用它的.jar文件的想法。