Rick Roy
Rick Roy

Reputation: 1728

"Duplicate Resources" error when building Android app

When I build my Android app, I get this error:

Error:Error: Duplicate resources: E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.png:drawable-hdpi-v4/login_bg, E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.9.png:drawable-hdpi-v4/login_bg
Error:Execution failed for task ':app:mergeDebugResources'.
> E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.png: Error: Duplicate resources: E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.png:drawable-hdpi-v4/login_bg, E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.9.png:drawable-hdpi-v4/login_bg

I am not able to properly understand the error. What file is duplicated here? What am I supposed to do to rectify it?

Upvotes: 54

Views: 146917

Answers (7)

Aabi
Aabi

Reputation: 81

Delete the image from drawable directory, because you are using same name within one folder, which is not allowed.

Upvotes: 8

ChuongPham
ChuongPham

Reputation: 4801

The reason you are seeing this error is because Android considers the following images to be the same, based on how they are referenced in your layouts:

E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.png
E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.9.png

The first image, login_bg.png, is a normal image. The second image, login_bg.9.png, is named in such a way to tell Android that it is a 9-patch image. However, in terms of referencing the images, they are declared the same, as in the following examples.

Normal image:

<ImageView
    android:id="@+id/normalImage"
    android:background="@drawable/login_bg"/>

Nine-patch image:

<ImageView
    android:id="@+id/ninePatchImage"
    android:background="@drawable/login_bg"/>

Note: There is no difference in terms of referencing the images from the /res/drawables directory of your Android project.

See here for more info about nine-patch image, or the correct term for it is nine-patch drawable. For reference, nine-patch drawables must be declared as <name>.9.png, as in login_bg.9.png.

Therefore, simply renaming them will not solve the issue. You need to check with whoever developed the UI to see which one should be used: either the normal image (login_bg.png) or the 9-patch image (login_bg.9.png)—not both.

Upvotes: 28

Odhik Susanto
Odhik Susanto

Reputation: 227

This error message will occur whenever the build tools detect multiple files with same name, regardless of their extension type.

For example, a file named mypicture.jpg cannot be in the same folder as mypicture.png.

In your case, login_bg.9.png and login_bg.png cannot be in the same directory.

Therefore, what you need to do is change the name of (or delete) one of the files.

Upvotes: 15

Charles
Charles

Reputation: 255

Solved this by going to

Build ➞ Clean Project

Upvotes: 5

Jonathan Applebaum
Jonathan Applebaum

Reputation: 5986

After I changed the initial/default ic_launcher via
Resource Manager>>Image Asset>>Icon Type>> Launcher Icons
I got this error.
It happens because the initial icon still exists and with the same name inside the mipmap folder.

the solution for me was to delete the initial launcher icon.
Solution: Change your view to Project, find the initial icon, and delete it, then the error should be gone.

how to find and delete the initial icon

Upvotes: 5

jiar wang
jiar wang

Reputation: 360

I find a same string name in my two diff .xml file,

//a.xml
<string name="dialog_subscribe">关注</string>
//b.xml
<string name="dialog_subscribe">subscribe</string>

then i solved it by rename one of then

Upvotes: 0

Mike Weir
Mike Weir

Reputation: 3189

Using Ant instead of Gradle seemed to fix the issue for me. Many of my files have the same filename to start with a different extension (indexed resource files of various types). Ancient project, and the ancient solution worked for me.

Upvotes: 0

Related Questions