Michael Ho
Michael Ho

Reputation: 81

Gradle Build Fails with Duplicate Drawable Resources Error in Android Project: Conflicting .png and .jpg Files

When I compile using the command line, the following error occurs,but when I compile in Android Studio, I don't get this error. I know that Android doesn't allow two files with the same name in the same resource directory, even if they have different extensions.

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

How to solve it without changing the file name?

Upvotes: 0

Views: 40

Answers (1)

BenjyTec
BenjyTec

Reputation: 10812

You already gave the answer yourself:

I know that Android doesn't allow two files with the same name in the same resource directory, even if they have different extensions.

The reason for that is indicated in the official documentation:

res/drawable/filename.png (.png, .webp, .jpg, or .gif)
The filename is the resource ID

If you have two files with the same filename, you will have a duplicate resource ID and Android can't resolve it anymore.

You have not specified how you compiled your project in Android Studio, but when I add two files with the same name into the drawable folder and try to run the app, I immediately get the error:

Task :app:mergeDebugResources 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"}

Without further details from your side, it's hard to tell why Android can build the project in your case.
The answer to your question however is, that you can't and shouldn't add two drawable resources with the same filename.

Upvotes: 0

Related Questions