Android项目Gradle构建报错:同名不同格式Drawable资源冲突
问题描述
使用命令行执行Gradle编译时出现重复资源错误,但在Android Studio中编译无异常。已知Android不允许同一资源目录下存在同名不同扩展名的文件。
环境信息
- Android SDK Version: 34
- Gradle Version: 8.7
命令行编译错误日志
root@test-2288H-V6:/tmp/workspace/Compile_POC_Android/PQ/PQ# gradle clean build --dependency-verification=off --offline -x test > Configure project :opencv OpenCV: 4.7.0-dev /tmp/workspace/Compile_POC_Android/PQ/PQ/opencv/build.gradle > Task :app:mergeDebugResources FAILED ERROR: [drawable/baohedu] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/baohedu.png [drawable/baohedu] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/baohedu.jpg: Resource and asset merger: Duplicate resources ERROR: [drawable/jpg11] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/jpg11.jpg [drawable/jpg11] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/jpg11.png: Resource and asset merger: Duplicate resources ERROR: [drawable/jpg12] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/jpg12.png [drawable/jpg12] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/jpg12.jpg: Resource and asset merger: Duplicate resources ERROR: [drawable/liangdu] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/liangdu.jpg [drawable/liangdu] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/liangdu.png: Resource and asset merger: Duplicate resources ERROR: [drawable/sewen] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/sewen.png [drawable/sewen] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/sewen.jpg: Resource and asset merger: Duplicate resources ERROR: [drawable/huidu] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/huidu.png [drawable/huidu] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/huidu.jpg: Resource and asset merger: Duplicate resources ERROR: [drawable/expand] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/expand.jpg [drawable/expand] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/expand.png: Resource and asset merger: Duplicate resources ERROR: [drawable/ruidu] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/ruidu.png [drawable/ruidu] /tmp/workspace/Compile_POC_Android/PQ/PQ/app/src/main/res/drawable/ruidu.jpg: Resource and asset merger: Duplicate resources
解决方案(不修改文件名)
方案1:排除其中一类格式的资源
如果只需保留一种格式的资源(如PNG),可在app模块的构建脚本中配置排除另一种格式的文件:
Groovy 语法(build.gradle)
android { tasks.withType(MergeResources) { // 排除所有JPG文件,保留PNG资源;反之则替换为 exclude '**/*.png' exclude '**/*.jpg' } }
Kotlin DSL 语法(build.gradle.kts)
android { tasks.withType<MergeResources> { exclude("**/*.jpg") // 替换为exclude("**/*.png")可保留JPG资源 } }
方案2:将一类文件转为Assets资源
若需同时使用两种格式的文件,可将其中一类(如JPG)转为Assets资源,避免与Drawable资源冲突:
Groovy 语法(build.gradle)
android { sourceSets { main { // 将drawable目录下的JPG文件纳入Assets目录 assets.srcDirs += fileTree(dir: 'src/main/res/drawable', include: ['**/*.jpg']) // 从Drawable资源中排除JPG文件 res.exclude '**/*.jpg' } } }
Kotlin DSL 语法(build.gradle.kts)
android { sourceSets { main { assets.srcDirs += fileTree("src/main/res/drawable") { include("**/*.jpg") } res.exclude("**/*.jpg") } } }
之后需通过AssetManager读取Assets中的文件,而非资源ID。
方案3:拆分资源到不同目录
创建额外的资源目录,通过Gradle配置映射不同格式的文件,无需修改文件名:
- 在
src/main/res下新建drawable-jpg和drawable-png目录; - 将原
drawable目录下的JPG文件移至drawable-jpg,PNG文件移至drawable-png; - 在构建脚本中添加配置:
Groovy 语法(build.gradle)
android { sourceSets { main { res.srcDirs = [ 'src/main/res/drawable-png', 'src/main/res/drawable-jpg', 'src/main/res' ] } } }
Kotlin DSL 语法(build.gradle.kts)
android { sourceSets { main { res.srcDirs( "src/main/res/drawable-png", "src/main/res/drawable-jpg", "src/main/res" ) } } }
内容的提问来源于stack exchange,提问作者Michael Ho
相关产品推荐
相关产品推荐

