簡體   English   中英

警告:與依賴項“com.android.support:support-annotations”沖突

[英]Warning:Conflict with dependency 'com.android.support:support-annotations'

我幾乎嘗試了書中的每一個技巧。

  • ResolutionStrategy.force
  • 不包括模塊

但似乎沒有任何效果,下面是我的build.gradle。 我正在使用Gradle版本1.2.3。 有人可以說清楚我的代碼還有什么問題。

我唯一沒試過的是改變Gradle的版本。 這是一個非常基本的Espresso測試案例。 謝謝!

apply plugin: 'com.android.application'
android {
    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
    }
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.rasika.job"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
repositories {
    mavenCentral()
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'

    testCompile 'junit:junit:4.12'

    androidTestCompile 'com.android.support.test:runner:0.3'
    androidTestCompile 'com.android.support.test:rules:0.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}

我將android-topeka google示例和更新的appcompat版本分解為23.1.0,同樣的消息:

警告:與依賴項“com.android.support:support-annotations”沖突。 app(23.1.0)和測試app(23.0.1)的已解決版本有所不同。

我補充說:

androidTestCompile 'com.android.support:support-annotations:23.1.0'

現在兩個都解析為23.1.0,警告消失,應用程序和測試仍然有效。

我不確定這是更好的解決方案,所以我正在尋找另一個但是找到了你的問題。

更新:閱讀PaulR的這個好解釋

Update2:確認, android測試谷歌樣本做到了。

// Testing-only dependencies
// Force usage of support annotations in the test app, since it is internally used by the runner module.
androidTestCompile 'com.android.support:support-annotations:23.0.1'

Update3CommonsWare的另一個很好的回應

使用以下方法檢查特定版本/沖突/解決方案

./gradlew -q yourmodule:dependencies

在此輸入圖像描述

在你的情況下,Appcompat是22.1.1,但你強迫22.1.0。

Update4: Android構建系統(Android Dev Summit 2015)中解釋的依賴沖突。

在此輸入圖像描述

解決主要和測試APK之間的沖突

運行檢測測試時,主APK和測試APK共享相同的類路徑。 如果主APK和測試APK使用相同的庫(例如Guava)但是在不同版本中,Gradle構建將失敗。 如果gradle沒有捕獲到,那么您的應用程序在測試期間和正常運行期間可能會有不同的行為(包括其中一個案例中的崩潰)。

要使構建成功,只需確保兩個APK使用相同的版本。 如果錯誤是關於間接依賴項 (您在build.gradle中未提及的庫),則只需將新版本的依賴項添加 到需要它 的配置 (“compile”或“androidTestCompile”) 中。 您還可以使用Gradle的解決策略機制。 您可以通過運行./gradlew:app:dependencies和./gradlew:app:androidDependencies來檢查依賴關系樹。

我通過添加依賴來解決沖突:

androidTestCompile 'com.android.support:support-annotations:23.2.0'

我遇到了同樣的問題,由此解決了:

// build.gradle
...
android {
    ...
    defaultConfig {
        ...
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    ...
    androidTestCompile('com.android.support.test.espresso:espresso-core: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:runner:0.3') {
        // Necessary if your app targets Marshmallow (since the test runner
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'
    }
}

解決方案在這里找到: https//github.com/codepath/android_guides/wiki/UI-Testing-with-Espresso

更新:我的build.gradle中的依賴塊最終看起來像這樣:

dependencies {
    ...        
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:support-v4:23.2.1'
    compile 'com.android.support:design:23.2.1'
    ...     

    // Necessary if your app targets Marshmallow (since Espresso
    // hasn't moved to Marshmallow yet)
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
        exclude group: 'com.android.support'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2.2') {
        exclude group: 'com.android.support'
    }
    androidTestCompile('com.android.support.test:runner:0.5') {
        exclude group: 'com.android.support'
    }
    androidTestCompile('com.android.support.test:rules:0.5') {
        exclude group: 'com.android.support'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') {
        exclude group: 'com.android.support'
    }
    androidTestCompile('com.android.support:support-annotations:23.2.1') {
        exclude group: 'com.android.support'
    }
    androidTestCompile('com.android.support.test.uiautomator:uiautomator-v18:2.1.2') {
        exclude group: 'com.android.support'
    }
}

最近添加uiautomator時發生了這種情況。 要解決此問題,您需要確定使用過時模塊的依賴項或依賴項。 您可以通過將每個androidTestCompile依賴項包裝到一個塊中來完成此操作,如下所示:

androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2') {
    transitive = false;
}

可能會破壞其他一些東西,所以你需要小心。 我能夠確切地確定哪兩個依賴項對我造成了這個問題,並且只是將這種阻塞機制添加到那些。

我通過從runner和espresso-core依賴項中排除support-annotation庫來解決沖突:

androidTestCompile 'com.android.support.test:runner:0.5',{
        exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.2'){
        exclude group: 'com.android.support', module: 'support-annotations'
}

將其添加到主build.gradle:

allprojects {
    ...
    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:23.1.1'
    }
    ...
}

將以下代碼添加到build.gradle文件中的依賴關系塊

compile 'com.android.support:support-annotations:23.2.1'
testCompile 'com.android.support:support-annotations:23.2.1'
androidTestCompile 'com.android.support:support-annotations:23.2.1'

androidTestCompile更改為測試編譯。 並提醒不要將其更改為編譯,只需將此依賴項編譯到我們的調試APK或測試APK。

對我來說這很好

dependencies {

    androidTestCompile 'com.android.support:support-annotations:23.1.1'
}

我通過添加依賴來解決沖突:

compile 'com.android.support:appcompat-v7:23.2.1'
 compile 'com.android.support:design:23.2.1'
...
 androidTestCompile 'com.android.support:support-annotations:23.2.1'

在進入問題說

無法解決

com.android.support:support-annotations:23.1.0

並試圖在其他服務器中找到,

但解決我問題的是添加:

google-service.json 

來自的文件

https://developers.google.com/mobile/add

並將其復制並粘貼到

YourAndroidProject/app 

然后重新編譯它,我希望你的代碼會飛

用它來解決沖突

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

我通過添加依賴來解決沖突:

androidTestCompile "com.android.support:support-annotations:26.0.0-beta1"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM