这种错误可以分为五种情况,主要是学会看日志,自己分析问题逐一排查
看日志,看日志,看日志,重要的事情说三遍,日志在build>sync里。
1. 报xxx plugin找不到:
- 打开studio设置,找到plugin里面看一下 xxxx 插件是否安装,没有就去安装一下,当然通常都是安装了的。
2. gradle版本不匹配
gradle里面的buildscript字段缺失或者版本过低或过高,找一个能正常启动的项目进行对比就能看出问题:
- 项目里面有两个project下的build.gradle和app>module下面的build.gradle。跟我们常见的项目不同的是有些AS项目是用其他的IDE生成的或者熟悉gradle构建的开发者自行修改的,所以报错的gradle不一定在哪,看日志可以帮助我们找到从什么地方开始修改。
- 如果在导入项目下面没有找到buildscript这个关键字段,没有或者不完整的话,就从能完好运行的项目里面拷贝一段过来,替换掉导入项目中的字段。再点击sync试试。
3. 找不到xxx依赖包或者xxx依赖包无法解析导致构建失败
Could not resolve com.jakewharton:butterknife-compiler:10.1.0
- 多添加一个maven仓库就行,一般情况是Google的仓库被墙了,如下代码:
repositories {
jcenter()
google()
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/google/' }
// maven { url "http://mvnrepo.alibaba-inc.com/mvn/repository" }
}
- 这种情况的话需要调整一下maven仓库的顺序
Could not HEAD 'http://repo1.maven.org/maven2/com/jakewharton/butterknife-compiler/10.1.0/butterknife-compiler-10.1.0.pom'. Received status code 501 from server: HTTPS Required
4. buildtoolversion:找不到的错误
- 回到能正常运行的项目,直接找到compileSdkVersion和buildtoolversion一起拷贝过来,当然可能又会引起Android部分依赖包的版本不对,这个时候就看着改一下版本。
5. 配置输出包打包异常
AS提示`Required com.android.build.api.attributes.BuildTypeAttr 'artificial' and found incompatible value 'debug'.`,详细如下:
> Could not resolve all artifacts for configuration ':app:hsyuniformArtificialCompileClasspath'.
> Could not resolve project :hiappconfig.
Required by:
project :app
> Unable to find a matching variant of project :hiappconfig:
- Variant 'debugApiElements' capability HIAPOSCMP:hiappconfig:unspecified:
- Incompatible attribute:
- Required com.android.build.api.attributes.BuildTypeAttr 'artificial' and found incompatible value 'debug'.
方案一:在变体里面添加下面这行代码
artificial{
...
matchingFallbacks = ['release', 'debug']
}
方案二:在依赖的module的buildTypes里面配置同名的输出包打包
artificial{
...
}
some other
Invoke-customs are only supported starting with Android O (--min-api 26)
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
总结
- Android studio配合gradle兼容性比较差,两者组合起来的版本很多。如果同一个开发小组的版本不对会引起很多的问题,最好在项目开始的时候就统一。要升级IDE版本,有空的时候组织一起升级。
- Android studio 构建的时候有时候下载插件卡住了,重新导入一下。
- 部分开发者用的是其他IDE开发,然后导出的AS项目,无法正常在AS里面跑,需要按照上面流程检查操作一下。
- 学会看日志,多看多想,自己就能解决问题。