UI測試導入Espresso時的衝突

  • 以下是主要針對android studio導入包引發錯誤解決方法

用Espresso做UI測試時會導入下面一些包

dependencies {
  androidTestCompile 'com.android.support.test:runner:0.4'
  // Set this dependency to use JUnit 4 rules
  androidTestCompile 'com.android.support.test:rules:0.4'
  // Set this dependency to build and run Espresso tests
  androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
}

當導入這些包時,如果support-annotations.jar與Espresso版本不匹配時,

就會報出下面的錯誤:

Error:Conflict with dependency ‘com.android.support:support-annotations’. Resolved versions for app (22.0.0) and test app (23.0.1) differ.

這有兩種解決方法:

方法一:

就按提示把你的support-annotations改成需要的版本。改完後你會發現之前寫的SDK版本號不符和其它包不符時,這時也要連帶着一起去改那些版本號。這時都改完後,你應用中有網絡請求時會發現有些http老版的包不能用,這時在build.gradle裏面加上下面這句話:

android {
    ......
//繼續引用這個包
    useLibrary 'org.apache.http.legacy'
    ......
    }

當代碼寫到後期代碼量多時改動量稍微有點大

方法二:

相對於上面的方法就簡單多了,只需要忽略support-annotations的版本問題就可以了

dependencies {
......
 androidTestCompile ('com.android.support.test:runner:0.4'){
        exclude module: 'support-annotations'
    }
    androidTestCompile ('com.android.support.test:rules:0.4'){
        exclude module: 'support-annotations'
    }
    androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.1'){
        exclude module: 'support-annotations'
    }
    ......
    }

感謝大家的支持

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章