Android error Duplicate Resources

2020-08-09 08:36发布

问题:

I am very new to android development and am developing my first app when I got 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 duplicate here? What am I supposed to do to rectify it?

P.S. The UI was developed by my frnd who is an UI developer and then he mailed me the project on which I am now supposed to add code and functionality so it becomes hard for me to try and figure out what might be the possible error.

回答1:

That's because Android considers the following to be the same when you referenced the images 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

login_bg.9.png image tells Android that this image is a 9-patch image. Whereas, the other image, login_bg.png, is a normal image. But 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 your /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.



回答2:

basically it will occur whenever the xml detects multiple files with same name, regardless of their extension type. e.g : mypicture.jpg can not be in same directory folder with mypicture.png

so does with your case, login_bg.9.png and login.png in the same directory folder is not allowed.

hopefully this may be useful. have a good day



回答3:

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



回答4:

Solved this by going to :

Build ➞ Clean Project

Good luck!!



回答5:

I solved this issue by renaming both files with different names.



回答6:

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.



回答7:

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