Conflict with dependency 'com.android.support:support-annotations'解决方案

Conflict with dependency ‘com.android.support:support-annotations’解决方案

今天同步项目的时候,Android报了Conflict with dependency ‘com.android.support:support-annotations’的错误,于是在这里记录一下:

完整的错误如下:

Error:Execution failed for task ':app:preDebugAndroidTestBuild'.
Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (25.3.1) and test app (25.4.0) differ. See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.

根据错误提示,可以知道com.android.support:support-annotations依赖冲突了,App是25.3.1,而test APP是25.4.0
于是按照网上的教程,添加以下依赖:

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

Sync以后,结果发现并没有什么卵用,依然报上述的错误.

有效的解决方案

最后在StackOverflow上找到了有效的解决方案:
1. 强行指定依赖.Android studio你个抖M,看来需要来点硬♂的.

dependencies {
    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:25.4.0'
    }
}
  1. 如果你不喜欢来硬♂的,也有第二种解决方式,使用以下依赖:
dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
}

替换:

androidTestCompile 'com.android.support.test:runner:1.0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.1'

如果你只是想解决上面个问题,就可以右上角点×了,以下纯属我瞎JB乱扯:

由于之前的项目,由于不懂事,觉得测试用例没什么卵用,就删了!以下是重新为项目引入Android Unit Test和Android Instrument Test支持.

说明

  • Android Unit Test: 可以脱离emulator和device独立运行在jvm的测试方式.(也就是说,如果我们需要写一些java代码,又不想在手机或者模拟器中运行,也不想打开Eclipse或者Intellij idea,就可以在这里面编写运行)
  • Android Instrument Test: 它是运行在emulator和device上的测试方式.

Android studio新建项目的时候,默认会支持两种测试用例包括Android Instrument Test和Android Unit Test,会在模块的build.gradle中默认引入以下依赖:

android{
    defaultConfig {
        //这句必须有,否则在运行测试用例的时候,会报Test running failed: Unable to find instrumentation info for......的错误
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}
dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

并在模块的/src目录下生成androidTest和test文件夹,如下图两个箭头所示:

可能你们注意到了,Android studio也友好的分别为我们在/androidTest文件夹和/test文件夹生成了ExampleInstrumentedTest类和ExampleUnitTest类,作为给我们开发人员一个参考demo,这个两个类的代码如下:

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() throws Exception {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.example.dingx.messagepushdemo", appContext.getPackageName());
    }
}
/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
public class ExampleUnitTest {
    @Test
    public void addition_isCorrect() throws Exception {
        assertEquals(4, 2 + 2);
    }
}

具体集成方式

既然Android studio有现成的,于是按照Android studio新建项目时所依赖的东西,添加到现有项目中,并分别新建test文件夹和androidTest文件夹,注意以下引用不要忘了:

android{
    defaultConfig {
        //这句必须有,否则在运行测试用例的时候,会报Test running failed: Unable to find instrumentation info for......的错误
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

当需要为当前类写测试用例的时候,右键空白处,依次选择以下选项即可:

关于Android studio集成其它第三方测试框架的方式,例如Robolectric,Espresso,Robotium,这里就不介绍了(其实我也没集成过:P).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值