2016-05-18 122 views
7

我想學習android espresso ..我遵循一些基本教程,它工作正常。但現在我想對android導航抽屜做一些測試。爲此,我需要使用gradle dependency androidTestCompile'c​​om.android.support.test.espresso:espresso-contrib:2.2.2'但它與其他依賴項產生衝突。我gradle這個文件:安卓espresso-cotrib gradle構建失敗

apply plugin: 'com.android.application' 

android { 
compileSdkVersion 23 
buildToolsVersion "23.0.3" 

defaultConfig { 
    applicationId "my.com.myapp_android" 
    minSdkVersion 18 
    targetSdkVersion 23 
    versionCode 1 
    versionName "1.0" 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
} 
repositories { 
jcenter() 
} 
dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
testCompile 'junit:junit:4.12' 
//material design 
compile 'com.android.support:appcompat-v7:23.3.0' 
compile 'com.android.support:support-v4:23.3.0' 

//zxing 
compile 'com.journeyapps:zxing-android-embedded:[email protected]' 
compile 'com.google.zxing:core:3.2.1' 

//Testing 
// Optional -- Mockito framework 
testCompile 'org.mockito:mockito-core:1.10.19' 
androidTestCompile 'com.android.support:support-annotations:23.3.0' 
androidTestCompile 'com.android.support.test:runner:0.5' 
androidTestCompile 'com.android.support.test:rules:0.4.1' 
// Optional -- Hamcrest library 
androidTestCompile 'org.hamcrest:hamcrest-library:1.3' 
// Optional -- UI testing with Espresso 
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' 
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2' 
// Optional -- UI testing with UI Automator 
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1' 




//inMarketSDK 
//compile group: 'com.inmarket', name: 'm2msdk', version: '2.29', ext: 'aar' 

} 

錯誤是這樣的:

Error:Conflict with dependency 'com.android.support:support-v4'. Resolved versions for app (23.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details. 
Error:Conflict with dependency 'com.android.support:appcompat-v7'. Resolved versions for app (23.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details. 

遵循這樣的:link的咖啡安裝

我也試圖排除註釋依賴性:

androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.2') { 
    // Necessary if your app targets Marshmallow (since Espresso 
    // hasn't moved to Marshmallow yet) 
    exclude group: 'com.android.support', module: 'support-annotations' 
} 

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2') 
     { 
      // Necessary if your app targets Marshmallow (since Espresso 
      // hasn't moved to Marshmallow yet) 
      exclude group: 'com.android.support', module: 'support-annotations' 
     } 
+0

錯誤消息說與'com.android.support:support-v4'和'com.android.support:appcompat-v7'衝突。所以儘量排除它們。 – nenick

回答

27

TL; DR;

espresso-contrib 2.2.2庫的新版本現在com.android.support:appcompat-v7:23.1.1依賴於我們的compile時間依賴性當使用不同版本的appcompat-v7像下面導致發生衝突:

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.4.0' 

    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2' 
} 

爲了避免衝突,當我們排除appcompat-v7依賴espresso-contrib像下面一樣,由於design support庫存在一些值依賴關係,所以再次中斷。

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){ 
    exclude module: 'support-annotations' 
    exclude module: 'support-v4' 
    exclude module: 'support-v13' 
    exclude module: 'recyclerview-v7' 
    exclude module: 'appcompat-v7' 
} 

錯誤:

Error:(69) Error retrieving parent for item: No resource found that matches the given name 'TextAppearance.AppCompat.Display1'.

根源:

This is because the design support lib has dependency on appcompat-v7 .
So,when we exclude 'appcompat-v7' module from espresso-contrib dependencies(like above) , the design support lib downloaded as part of transitive dependency of espresso-contrib lib couldn't find the compatible version of appcompat-v7 lib(23.1.1) it is using internally in its resources files and thus gives out the above error.

因此,解決上述問題是從espresso-contrib排除 '設計輔助' LIB依賴性象下面這樣:

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){ 
    exclude module: 'support-annotations' 
    exclude module: 'support-v4' 
    exclude module: 'support-v13' 
    exclude module: 'recyclerview-v7' 
    exclude module: 'design' 
} 

T帽子解決衝突問題!

更長的版本(如果有人感興趣):

要發現的,我們用的時候`意式咖啡的contrib」庫我已經創建樣例應用程序,找出根源面對各種各樣的衝突問題的原因。

Step 1:Using Espresso-Contrib Lib version 2.2.1 

創建應用程序使用'意式咖啡的contrib'庫版本2.2。1app/build.gradle文件中添加以下行:

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 

    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1' 

}

注:在這種情況下,我沒有導入任何其他支持庫組件,如
appcompat-v7,recyclerview-v7,etc

依賴圖對上述設置看起來像下面:
enter image description here

如可以看到的那espresso-contrib 2.2.1 LIB對的
support-v4recyclerview-v7support-annotations等23.0.1版傳遞依賴。

因爲我沒有在我的項目中定義recyclerview-v7support-annotations的依賴項,所以上面的設置可以正常工作。

但是,當我們在我們的項目中將這些定義爲編譯依賴關係[如下所示]時,我們會在您的問題中指出版本衝突問題。

compile 'com.android.support:appcompat-v7:23.3.0' 
compile 'com.android.support:support-v4:23.3.0' 

爲了避免這些,我們下面的行添加到我們的意式咖啡的contrib LIB衝突:

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1'){ 
    exclude module: 'support-annotations' 
    exclude module: 'support-v4' 
    exclude module: 'support-v13' 
    exclude module: 'recyclerview-v7' 
} 

這將確保這些依賴不是下載作爲espresso-contrib傳遞依賴的一部分。
一切運行良好與上面setup.No問題!

Step 2: Using Espresso-Contrib lib version 2.2.2 

更改應用程序的的build.gradle '意式咖啡的contrib'庫版本2.2.2 通過改變了以前的build.gradle文件中使用:

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
compile 'com.android.support:appcompat-v7:23.3.0' 
compile 'com.android.support:support-v4:23.3.0' 
testCompile 'junit:junit:4.12' 

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){ 
    exclude module: 'support-annotations' 
    exclude module: 'support-v4' 
    exclude module: 'support-v13' 
    exclude module: 'recyclerview-v7' 
    } 
} 

但是,當我建立的項目中使用以上設置...建立失敗,併發布有問題的錯誤。

錯誤:

Error:Conflict with dependency 'com.android.support:appcompat-v7'. Resolved versions for app (23.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

所以,看的錯誤,我增加了一個行上面的build.gradle:

exclude module: 'appcompat-v7' (inside androidTestCompile block of espresso-contrib) 

但是,這並不解決衝突的問題,我得到發表的評論價值的依賴性錯誤。
所以我檢查我的應用程序的依賴關係圖再: enter image description here

如現在可以看出,espresso-contrib 2.2.2 LIB對com.android.support:design:23.1.1現在傳遞依賴造成上述衝突。

所以,我們需要以下行裏面添加androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2')塊:

exclude module: 'design' 

這解決衝突問題庫版本2.2.2!

+0

waw ...完美的答案。謝謝。 @Droidwala –

+0

@ D4Developer我很高興我可以幫助你:)你可以點擊綠色的勾號來標記這是接受的答案,以便它可以幫助用戶在將來遇到這個問題。 – Droidwala

+0

完美答案。謝謝! – Travis

0

做下面

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1'){ 
    exclude module: 'support-annotations' 
    exclude module: 'support-v4' 
    exclude module: 'recyclerview-v7' 
} 
+0

嗯,我嘗試排除,但是當我寫這個'排除組:'com.android.support',模塊:'appcompat-v7''它剎住了一些值的依賴關係。 –

+0

雖然我試過這個'transitive = false',但它工作正常。但不知道這是一個很好的解決方案 –