Failed to resolve:com.android.support:appcompat-v7第一次运行安卓程序报错

1.当你在用别的电脑上的android studio编写一个项目时,然后copy下来,又在自己电脑上的android studio 上导入该项目时会报错(两台电脑上安装的android studio版本不一样)

2.自己的android studio SDK平台工具的版本太低,然后在不了解项目构建文件(build.gradle文件)的前提下,点开了SDK Manger更新了项目构建工具(SDK Build-Tools)的版本

看完上面两种情况,其实都可以发现它们的共同点,那就是版本问题,所以上述的两种情况就用同一种解决办法就可以了,多简单

具体解决方案:

1.既然是版本问题,那就的先去了解自己的电脑安装的SDK工具版本,点开SDK Manager图标,然后选中Updates就可以看到了

这里我的 sdk 工具版本就是26.1.1了

2.清楚了自己的sdk 工具版本后,接下来我们继续查看版本,接下来是看sdk 构建工具(sdk Build-Tools)的版本,还是在sdk manager上操作,这次选中 Android SDK后,再在右边选中SDK Tools(只看打勾选项就行)

看了这张图,似乎就能明白些什么了对吧,你会发现,我这里是的 Android SDK Build-Tools (就是我前面一直提到的sdk 构建工具)版本是27,而我的SDK Tools才是26,

很明显版本就低了,但这些并不会直接造成项目报错,看完这些数据,我们接下来再看一张截图

3.点开项目构建文件Gradle Scripts,再继续点击build.gradle(Module:app)ps:有两个名字相同的,选第二个,看下面代码的注释行就可以了

  1. apply plugin: 'com.android.application'
  2.  
  3. android {
  4. compileSdkVersion 27 // 使用版本27的SDK编译
  5. buildToolsVersion "27.0.0" // 构建工具版本号为27.0.0 对应上图的SDK Build-Tools27
  6. defaultConfig {
  7. applicationId "com.example.haha.myapplication"
  8. minSdkVersion 19
  9. targetSdkVersion 27
  10. versionCode 1
  11. versionName "1.0"
  12. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  13. }
  14. buildTypes {
  15. release {
  16. minifyEnabled false
  17. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  18. }
  19. }
  20. }
  21.  
  22. dependencies {
  23. compile fileTree(dir: 'libs', include: ['*.jar'])
  24. androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  25. exclude group: 'com.android.support', module: 'support-annotations'
  26. })
  27. compile 'com.android.support:appcompat-v7:27.+'
  1. //上面一行是远程依赖声明,这里我们着重来看appcompat-v7:27.+
  2. testCompile 'junit:junit:4.12'
  3. }

我们来分析下appcompat-v7:27.+

上面我们查到我们的sdk工具版本是26,这里的远程依赖包的版本是27,那么我们只需要将把版本减低到和sdk工具版本相同就ok啦!
  1. apply plugin: 'com.android.application'
  2.  
  3. android {
  4. compileSdkVersion 26         //修改
  5. buildToolsVersion "27.0.0"
  6. defaultConfig {
  7. applicationId "com.example.haha.myapplication"
  8. minSdkVersion 19
  9. targetSdkVersion 26        //修改
  10. versionCode 1
  11. versionName "1.0"
  12. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  13. }
  14. buildTypes {
  15. release {
  16. minifyEnabled false
  17. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  18. }
  19. }
  20. }
  21.  
  22. dependencies {
  23. compile fileTree(dir: 'libs', include: ['*.jar'])
  24. androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  25. exclude group: 'com.android.support', module: 'support-annotations'
  26. })
  27. compile 'com.android.support:appcompat-v7:26+'  //修改
  28. testCompile 'junit:junit:4.12'
  29. }
问题解决,因为当你创建一个新的项目时,有gradle去帮你完成构建项目工作,那它自然是按你的最高sdk构建工具版本去构建项目,所以就会出现这样的问题,到这里你可能会觉得,卧槽,就只有最后那一段是有用的,还这么简单,其实前面给出的铺垫看完加自己的理解,会收获更多!如有错,请多指教哈!
这里重新补充一下:改完了!一定要点击 SYNC进行同步!SYNC !SYNC!重要事情那就说3遍,
或者点击 图下所示 的  Try Again
 
 
若更改后出现了如下 Failed to find target with hash string 'android-26' in 'xxx'的问题时,就直接点击Install missing platform and sync project在下载下就好了

猜你喜欢

转载自www.cnblogs.com/837634902why/p/10301659.html