使用AndroidStudio工具建立新的app应用时,根据工具提示进行多个next以后,发现编译报错,报错以下:html
Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (26.1.0) and test app (27.1.1) differ. See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.
通过网上查阅,发现提供有不少方法,好比 build->Rebuid-project,该方法只是当前启动有用,可是若是关闭androidstudio工具从新打开以后又会从新报错。android
该问题主要是由于在新建的应用中默认依赖了com.android.support:support-annotations包与app版本冲突了,app里的版本是26.1.0,可是Test app的版本里是27.1.1。可在External Libraries文件夹下面找到该包,发现确实冲突。app
最后通过查阅,发现两种方法能够解决该问题,思路都是同样的,就是将27.1.1版本号强制转换为26.1.0,转换方法以下:工具
(1)在依赖中强制转换,在app模块下的build.gradle中dependencies下,添加依赖转换,主要是下面两行代码gradle
androidTestCompile('com.android.support:support-annotations:26.1.0') { force = true }
所有代码为:ui
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 26 defaultConfig { applicationId "baseapp.li.com.baseapp" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation('com.android.support:support-annotations:26.1.0') { force = true } }
(2)在配置中强制转换,在app模块下的build.gralde中进行配置,其中主要增长了两行代码spa
configurations.all { resolutionStrategy.force 'com.android.support:support-annotations:26.1.0' }
所有代码以下:code
apply plugin: 'com.android.application' configurations.all { resolutionStrategy.force 'com.android.support:support-annotations:26.1.0' } android { compileSdkVersion 26 defaultConfig { applicationId "wzt.mytest" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
以上两种方法都可解决包名冲突问题。下面这个代码code背景不知道怎么去掉了,允许我调皮一下,应该无大碍。htm