个人问题出现场景:在编写ListView列表时,主类中初始化数据时,R报错,找不到该资源文件
个人问题出现原因:是res资源下drawable文件夹中放置的图片有问题。(Goole了一下,发现每个人问题可能不一样)
drawable官方文档说明:
Drawable Resources
SEE ALSO
A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int)
or apply to another XML resource with attributes such as android:drawable
and android:icon
. There are several different types of drawables:
- Bitmap: the simplest Drawable, a PNG or JPEG image.
- Nine Patch: an extension to the PNG format allows it to specify information about how to stretch it and place things inside of it.
- Shape: contains simple drawing commands instead of a raw bitmap, allowing it to resize better in some cases.
- Layers: a compound drawable, which draws multiple underlying drawables on top of each other.
- States: a compound drawable that selects one of a set of drawables based on its state.
- Levels: a compound drawable that selects one of a set of drawables based on its level.
- Scale: a compound drawable with a single child drawable, whose overall size is modified based on the current level.
①个人当时编写代码时比较匆忙,没有提前准备好相应的图片,就找了电脑里几张喜欢的图片,复制粘贴在drawable下,在代码中R资源文件爆红,显示找不到,在引入资源时R文件发生报错,报错查看locat之后发现原因:drawable图片尺寸过大;
②紧接着,删除了过大的图片,又试着找了几张图标图,结果依然R文件报错,locat中显示Error:Execution failed for task ':app:processDebugResources'。带着问题查找了官方文档,最终发现是drawable文件夹下面的图片资源文件错误。删除之后,程序正常运行,列表也显示成功。
Error:Execution failed for task ':app:processDebugResources'——每个人出现问题的原因可能不同,针对locat给出的提示,在结合官方API文档,去查找对应的解决办法。
附录 对于构建项目中出现的问题的解决方式汇总 (长期更新)
1)第三方依赖冲突 | 依赖库版本与SDK版本不一致
解决方式 — app的build.gradle下添加:
android{
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (requested.name.startsWith("appcompat-v7")) {
details.useVersion '25.3.0'
}
if (requested.name.startsWith("appcompat-v4")) {
details.useVersion '25.3.0'
}
if (requested.name.startsWith("recyclerview-v7")) {
details.useVersion '25.3.0'
}
}
}
}
}
强制所有依赖库使用同一个版本,不用去一个个查找了。
2)强制版本号
有时候第三方Lib用到了高版本的support包,而其可能存在一个问题,肯定不想因为这个Lib而引入问题,更不想因为它而自动升级当前的support包。
可是Gradle依赖的默认机制是有高版本则用高版本,这就陷入了一种进退两难的境地:用这个Lib就会升级supprot包,而不用则无法实现某个功能。幸运的是,configations提供了强制指定版本的功能。
先在根build.gradle中配置一个Task查看当前的support库的版本信息。
subprojects {
task allDeps(type: DependencyReportTask) { }
}
现在强制指定support库的版本号:
configurations.all {
// 指定某个库的版本
resolutionStrategy.force "com.android.support:appcompat-v7:26.1.0"
}
3)更改输出的apk的名字
static def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
android {
... ...
buildTypes {
... ...
}
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = outputFile.name.replace("app",
"${defaultConfig.applicationId}_${defaultConfig.versionName}_${releaseTime()}" )
outputFileName = fileName
}
}
}
}
上面的Task可以将APK以“包名 + 版本名 + 生产时间”的方式进行命名。