Android Studio新建项目遇support-annotations依赖版本冲突问题
解决Android Studio中support-annotations依赖版本冲突问题
嘿,我来帮你搞定这个烦人的依赖冲突报错!
问题原因
你的主项目依赖的com.android.support:appcompat-v7:26.1.0会自动引入26.1.0版本的support-annotations,但你又在测试依赖里手动指定了27.1.1版本的support-annotations,这就导致主程序和测试程序使用的依赖版本不一致,触发了这个报错。
解决方案
这里有两种靠谱的解决办法,选一种就行:
方法一:统一依赖版本
把测试模块里的support-annotations版本改成和主项目一致的26.1.0,同时建议统一使用androidTestImplementation(代替旧的androidTestCompile,这是现在官方推荐的方式)。修改后的依赖配置如下:
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2' androidTestImplementation 'com.android.support:support-annotations:26.1.0' }
方法二:强制统一所有依赖的版本
如果你的项目依赖比较复杂,不想一个个改版本,可以在build.gradle的android代码块外面添加以下配置,强制所有模块使用指定版本的support-annotations:
configurations.all { resolutionStrategy.force 'com.android.support:support-annotations:26.1.0' }
这样不管哪个依赖引入了support-annotations,都会统一使用26.1.0版本,彻底避免版本冲突。
小提示
记得修改完配置后点击Sync Now按钮同步项目,然后重新构建应该就能解决问题啦!
内容的提问来源于stack exchange,提问作者Lomado
相关产品推荐
相关产品推荐

