Error:Execution failed for task ':app:processDebugResources'.个人已解决

 

个人问题出现场景:在编写ListView列表时,主类中初始化数据时,R报错,找不到该资源文件

个人问题出现原因:是res资源下drawable文件夹中放置的图片有问题。(Goole了一下,发现每个人问题可能不一样)

drawable官方文档说明

 

Drawable Resources

SEE ALSO

  1. 2D Graphics

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以“包名 + 版本名 + 生产时间”的方式进行命名。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值