当我使用命令行编译时,出现以下错误,但是当我在Android Studio中编译时,我没有收到此错误。我知道Android不允许在同一个资源目录中存在两个同名的文件,即使它们具有不同的扩展名。
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
不改文件名如何解决?
你自己已经给出答案了:
我知道Android不允许在同一个资源目录中存在两个同名的文件,即使它们具有不同的扩展名。
原因在官方文档中有说明:
res/drawable/filename.png(.png、.webp、.jpg 或 .gif)
文件名就是资源ID
如果您有两个具有相同
filename
的文件,您将拥有重复的资源 ID,并且 Android 无法再解析它。
您没有指定如何在 Android Studio 中编译项目,但是当我将两个同名文件添加到可绘制文件夹中并尝试运行应用程序时,我立即收到错误:
Task :app:mergeReleaseResources FAILED
AGPBI: {"kind":"error","text":"Duplicate resources","sources":[{"file":{"description":"drawable/sample","path":"C:\\Users\\User\\Projects\\EdgeApplication\\app\\src\\main\\res\\drawable\\sample.jpg"}},{"file":{"description":"drawable/sample","path":"C:\\Users\\User\\Projects\\EdgeApplication\\app\\src\\main\\res\\drawable\\sample.png"}}],"tool":"Resource and asset merger"}
如果您没有提供更多详细信息,很难说出为什么 Android 可以在您的案例中构建该项目。
然而,你的问题的答案是,你不能也不应该添加两个具有相同
filename
的可绘制资源。