【问题标题】:Warning:Conflict with dependency 'com.android.support:support-annotations'警告:与依赖 'com.android.support:support-annotations' 冲突
【发布时间】:2015-12-12 13:04:08
【问题描述】:

我几乎尝试了书中的所有技巧。

  • 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 testing dependencies android-support-library


    【解决方案1】:

    我最近在添加 uiautomator 时发生了这种情况。要解决此问题,您需要确定哪些依赖项或哪些依赖项正在使用过时的模块。您可以通过将每个 androidTestCompile 依赖包装到一个块中来做到这一点,如下所示:

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

    可能会破坏其他一些东西,所以你需要小心。我能够准确地确定是哪两个依赖项导致了我的这个问题,并且只需将这种阻塞机制添加到这些依赖项中。

    【讨论】:

    • 这与./gradlew -q yourmodule:dependencies 一起使用是最干净的。
    【解决方案2】:

    我 fork android-topeka google sample 并将 appcompat 版本更新到 23.1.0,同样的消息:

    警告:与依赖冲突 'com.android.support:support-annotations'。应用程序的已解决版本 (23.1.0) 和测试应用 (23.0.1) 不同。

    我补充说:

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

    现在都解析为 23.1.0,警告消失了,应用和测试仍然有效。

    我不确定这是更好的解决方案,所以我正在寻找另一个解决方案,但找到了您的问题。

    更新:阅读this good explanation by PaulR

    Update2:确认,android-testing google sample 做到了。

    // 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'
    

    Update3Another good response by CommonsWare.

    使用以下方法检查您的特定版本/冲突/解决方案:

    ./gradlew -q yourmodule:dependencies
    

    在您的情况下,Appcompat 是 22.1.1,但您强制使用 22.1.0。

    更新4: The Android Build System (Android Dev Summit 2015) 解释了依赖冲突。

    Resolving conflicts between main and test APK

    运行仪器测试时,主 APK 和测试 APK 共享相同的类路径。如果主 APK 和 测试 APK 使用相同的库(例如 Guava),但使用不同的库 版本。如果 gradle 没有捕捉到这一点,您的应用程序可能会运行 在测试期间和正常运行期间(包括在 一种情况)。

    要使构建成功,只需确保两个 APK 使用相同的 版本。 如果错误是关于间接依赖的(您需要的库 在你的 build.gradle 中没有提到),只需为 配置的较新版本(“compile”或“androidTestCompile”) 需要它。你也可以使用 Gradle 的解析策略 机制。您可以通过运行 ./gradlew 检查依赖关系树 :app:dependencies 和 ./gradlew :app:androidDependencies。

    【讨论】:

      【解决方案3】:

      androidTestCompile 更改为 testCompile。并提醒不要改成编译,只需要把这个依赖编译到我们的debug APK或者test APK中就可以了。

      【讨论】:

        【解决方案4】:

        将此添加到您的主 bu​​ild.gradle:

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

        【讨论】:

        • 为什么要关闭allprojects
        • 这将强制使用 Android SDK 23 完成所有解析,从而避免因不同 sdk 的存在而导致的任何冲突。
        【解决方案5】:

        我通过添加依赖解决了冲突:

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

        【讨论】:

        • 这对我有用,但不仅是这个库,有时其他支持库也会发生冲突。您也可以使用 androidTestCompile 添加其他人。只是提醒其他有类似问题的人
        【解决方案6】:

        In也陷入了问题的说法

        无法解决

        com.android.support:support-annotations:23.1.0
        

        并尝试在其他服务器中查找,

        但是解决我的问题的是添加:

        google-service.json 
        

        文件来自

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

        复制粘贴到

        YourAndroidProject/app 
        

        然后重新编译它,我希望你的代码能飞起来

        【讨论】:

          【解决方案7】:

          对我来说效果很好

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

          【讨论】:

            【解决方案8】:

            在 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'
            

            【讨论】:

              【解决方案9】:

              我通过添加依赖解决了冲突:

              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'
              

              【讨论】:

                【解决方案10】:

                我遇到了同样的问题,通过这个解决了:

                // 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 中的 finally dependencies 块看起来像这样:

                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'
                    }
                }
                

                【讨论】:

                • 这个应该是最好的解决方案,如果我们只指定带有版本依赖的support-annotation模块,我们可以编译但是干净的项目会失败
                【解决方案11】:

                用它来解决冲突

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

                【讨论】:

                • 请在您的答案中添加一些详细信息,例如解释 OP 方法中的问题以及您的建议解决问题的原因。这个答案是否有效只是因为这个问题已经过去了一年多,现在情况不同了?
                【解决方案12】:

                我通过从 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'
                }
                

                【讨论】:

                  【解决方案13】:

                  我通过添加依赖解决了冲突:

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

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2018-11-20
                    • 1970-01-01
                    • 2018-10-24
                    • 1970-01-01
                    • 2016-08-02
                    • 2016-11-05
                    相关资源
                    最近更新 更多