笔者近期准备着手做一个有关安卓的项目,兴冲冲地找来Android学习书籍,并下载安装了Android Studio。可是在Android Studio的第一次运行就遇到了很大的问题,各种莫名其妙的报错,以至于编译项目都没能成功。于是在CSDN上各种搜,确实找到了很多大佬分享的解决方案,但都没有直接解决问题。
那天从晚上10点一直尝试到凌晨5点钟,几乎所有方法都已用尽,差点想放弃回去睡觉的时候却误打误撞修正成功。虽然没有明白具体的原因,但是解决了问题,之后的Android Studio使用过程再也没有出现过这种情况。于是决定写一篇博文把这次解决的过程分享出来。
下面是第一次运行后的情况,很多报错,无法编译项目。这时的Java的环境,SDK等等都已经配置好了。
我们将报错列表拉开,发现都是 Failed to resolve 报错
于是在网上各种搜,下面是其中的两篇:
https://blog.csdn.net/waa_studio/article/details/78732460
https://blog.csdn.net/itermeng/article/details/69400075
大部分人根据这两篇文章或是其下面推荐的博文中的做法去操作就可以解决问题,再次感谢各位大佬的分享。
特别的关于报错中:(26,13)无法解决:com.android.support:程序兼容性-V7:28 +
即有关“ com.android.support ”的报错也有一系列解决方案,在下面贴出对我有帮助的一些博文:
https://blog.csdn.net/mhl18820672087/article/details/78385361/
https://blog.csdn.net/yishichangan1/article/details/80309735
https://blog.csdn.net/xiangrikui0109/article/details/50617376
笔者目前能力有限,还是处于尝试博客中的操作以求解决问题的阶段,对其内部的原因不是特别清楚,不过能解决问题就很开心啦!
以上的无法解决问题大部分出现在 build.gradle(模块:app)文件中,解决方法往往是对其内容进行修改。下面分享出我解决前和解决后这一文件的代码,作对照参考。
解决前各种报错时的build.gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion "28.0.2"
defaultConfig {
applicationId "com.example.perce.myapplication"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:28.+'
testCompile 'junit:junit:4.12'
}
解决后的 build.gradle 文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '28.0.2'
defaultConfig {
applicationId "com.example.perce.myapplication3"
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 {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:26.0.0-alpha1'
// testImplementation'junit:junit:4.12'
//androidTestImplementation 'com.android.support.test:runner:0.6-alpha'
//androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.3-alpha'
}
/*dependencies {
// compile fileTree(dir: 'libs', include: ['*.jar'])
//androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
// exclude group: 'com.android.support', module: 'support-annotations'
//})
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
//testCompile 'junit:junit:4.12'
}
*/
allprojects {
repositories {
maven { url "http://repo1.maven.org/maven2" }
maven { url "https://maven.google.com"}
}
}
allprojects { repositories { jcenter() } }
/*repositories {
maven {
name = 'Maven Central Repo' //optional name
url = 'http://repo1.maven.org/maven2/'
}
}
*/
可以看出,改动主要出在这几个方面:
1。
compileSdkVersion 26
buildToolsVersion '28.0.2'
defaultConfig {
applicationId "com.example.perce.myapplication3"
minSdkVersion 15
targetSdkVersion 26
SDK工具的版本配置问题,这个需要大家查明自己安装的是什么版本并作出修改,具体操作在这条博文中写得十分清楚
https://blog.csdn.net/mhl18820672087/article/details/78385361/
2。
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:26.0.0-alpha1'
// testImplementation'junit:junit:4.12'
//androidTestImplementation 'com.android.support.test:runner:0.6-alpha'
//androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.3-alpha'
}
对 dependencies {} 的改动前前后做做了很多,担心改出问题于是把被改的注释掉而没有直接删掉,具体的配置依然依据版本不同而不同。
3。
allprojects {
repositories {
maven { url "http://repo1.maven.org/maven2" }
maven { url "https://maven.google.com"}
}
}
allprojects { repositories { jcenter() } }
这一部分属于网络连接的问题,也可找到相应的博文进行修改。
以上就是我解决Android Studio第一次安装启动无法解决报错的全部过程,自己对Android Studio内部的东西并不了解,因此不能整理出真正有效的解决方案,这篇文章也只是借鉴了各位大佬的经验写出的分享而已。之后创建新项目依然会有相同的报错,但只需要将上面贴出的修正后的的build.gradle文件代码复制粘贴,替换掉报错项目中的的build.gradle代码即可顺利编译。当然这是依据我的情况而确定的,大家不同的配置代码也一定略有不同。
最后希望大家在安卓开发的路上能不断解决问题,自我进步。