Java Android Studio - 已定义资源条目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24501149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:37:08  来源:igfitidea点击:

Android Studio - Resource Entry already defined

javaandroidandroid-studioandroid-gradle-plugin

提问by Yahya Uddin

After compiling my Android application using the Android Studio (Beta), I have the following error:

使用 Android Studio (Beta) 编译我的 Android 应用程序后,出现以下错误:

Error:error: Resource entry background is already defined.
res\drawable-xhdpi-v4\background.9.png:0: Originally defined here.

This occurred when I had a image file called "background" in my res folder.

当我的 res 文件夹中有一个名为“背景”的图像文件时,就会发生这种情况。

I then made a 9 patch version of it and pasted it in the same folder. This meant that there was 2 files with the same name. So I unsurprisingly I had this error.

然后我制作了它的 9 个补丁版本并将其粘贴到同一个文件夹中。这意味着有 2 个文件同名。所以不出所料,我遇到了这个错误。

However I now deleted all instances of the background image file (including the 9 patch version and original image) in my code and layout file and I still have this error.

但是,我现在删除了代码和布局文件中背景图像文件的所有实例(包括 9 补丁版本和原始图像),但仍然出现此错误。

采纳答案by deubaka

You could try to do one of the following:

您可以尝试执行以下操作之一:

  • Clean and Build or gradle clean build assemble
  • Invalidate and restart Android Studio
  • 清洁和构建或 gradle clean build assemble
  • 使 Android Studio 无效并重新启动

回答by zaidorx

I am using Android studio and I run exactly into the same issue and none of the answers I found on the web helped me solve the problem, including the previous one. So reading through all details of the error messages and found this lines:

我正在使用 Android Studio 并且遇到了完全相同的问题,我在网上找到的答案都没有帮助我解决问题,包括前一个问题。因此,通读了错误消息的所有详细信息并找到了以下几行:

Error:Execution failed for task ':app:processDebugResources'.

错误:任务“:app:processDebugResources”的执行失败。

com.android.ide.common.internal.LoggedErrorException: Failed to run command: C:\Users\user\AppData\Local\Android\android-studio\sdk\build-tools\android-4.4W\aapt.exe package -f --no-crunch -I C:\Users\user\AppData\Local\Android\android-studio\sdk\platforms\android-20\android.jar -M C:\Users\user\AndroidStudioProjects\DartsApp\app\build\intermediates\manifests\debug\AndroidManifest.xml -S C:\Users\user\AndroidStudioProjects\DartsApp\app\build\intermediates\res\debug -A C:\Users\user\AndroidStudioProjects\DartsApp\app\build\intermediates\assets\debug -m -J C:\Users\user\AndroidStudioProjects\DartsApp\app\build\generated\source\r\debug -F C:\Users\user\AndroidStudioProjects\DartsApp\app\build\intermediates\libs\app-debug.ap_ --debug-mode --custom-package com.dartstransit.dartsapp -0 apk --output-text-symbols C:\Users\user\AndroidStudioProjects\DartsApp\app\build\intermediates\symbols\debug

com.android.ide.common.internal.LoggedErrorException:无法运行命令:C:\Users\user\AppData\Local\Android\android-studio\sdk\build-tools\android-4.4W\aapt.exe 包 - f --no-crunch -IC:\Users\user\AppData\Local\Android\android-studio\sdk\platforms\android-20\android.jar -MC:\Users\user\AndroidStudioProjects\DartsApp\app\build \intermediates\manifests\debug\AndroidManifest.xml -SC:\Users\user\AndroidStudioProjects\DartsApp\app\build\intermediates\res\debug -AC:\Users\user\AndroidStudioProjects\DartsApp\app\build\intermediates\assets \debug -m -JC:\Users\user\AndroidStudioProjects\DartsApp\app\build\generated\source\r\debug -FC:\Users\user\AndroidStudioProjects\DartsApp\app\build\intermediates\libs\app-debug .ap_ --debug-mode --custom-package com.dartstransit.dartsapp -0 apk --output-text-symbols C:\Users\user\AndroidStudioProjects\DartsApp\app\build\intermediates\symbols\debug

Error Code:

错误代码:

1

Output:

输出:

res\drawable-hdpi-v4\bus_w.png:0: error: Resource entry bus_w is already defined.
res\drawable-hdpi-v4\bus_w.jpg:0: Originally defined here.

As you can see there are two files of name bus_w with different extensions on the same folder. I had already removed those files from the project and still getting the error, so I decided to check every single folder of the project that was reference in the compiler line in the error message. Finally found that there was a copy of the file bus_w.png in the folder app\build\intermediates\res\debug\drawable-mdpi-v4.

如您所见,在同一个文件夹中有两个名称为 bus_w 且扩展名不同的文件。我已经从项目中删除了这些文件,但仍然收到错误消息,因此我决定检查错误消息中编译器行中引用的项目的每个文件夹。最后发现文件夹中有一个文件bus_w.png的副本app\build\intermediates\res\debug\drawable-mdpi-v4

I just deleted it and voila, problem solved.

我刚刚删除了它,瞧,问题解决了。

回答by penduDev

A simple Build> Rebuild projecthelped me

一个简单的Build> Rebuild 项目帮助了我

回答by Enaoi

1 Build > Clean Project
2 Build > Rebuild Project

1构建 > 清理项目
2构建 > 重建项目

回答by Jeffin Bao

I have also encountered this error recently.Here is my solution that works,hope it will help you.

我最近也遇到了这个错误。这是我的解决方案,希望对您有所帮助。

1.only keep 9 patch version image in the drawable-hdpi folder

1.drawable-hdpi文件夹中只保留9个补丁版本图片

2.make sure the code related to this problem in xml file remains the same: android:background="@drawable/msg_left"

2.确保xml文件中与此问题相关的代码保持不变: android:background="@drawable/msg_left"

instead of using the code:

而不是使用代码:

android:background="@drawable/msg_left.9"

android:background="@drawable/msg_left.9"

PS:My original image name is msg_left,and my 9 patch version image name is msg_left.9

PS:我的原图名字是msg_left,我的9补丁版本图片名字是msg_left.9

回答by khaintt

Did you try rename this file to other name? It seem one of your libraries have same resource.

您是否尝试将此文件重命名为其他名称?您的图书馆之一似乎拥有相同的资源。

I see that many devs(include me) had issue with ic_launchertoo. I think you want to try mipmapinstead drawable. So we will have something like this:

我看到许多开发人员(包括我)也有问题ic_launcher。我想你想尝试mipmap,而不是drawable。所以我们会有这样的事情:

\res\mipmap-hdpi
\res\mipmap-mdpi
\res\mipmap-xhdpi
\res\mipmap-xxhdpi

Hope this help.

希望这有帮助。

回答by Miao1007

I did the trick by moving the icon

我通过移动图标来解决问题

drawable/ic_launcher

to

mipmap/ic_launcher

and it worked fine :)

它工作正常:)

回答by MrSmith42

In my case the problem was that I had more than one '.' in the filename. like:

就我而言,问题是我有多个 '.' 在文件名中。喜欢:

a.png
a.bold.png

Solution was to rename to

解决方案是重命名为

a.png
a_bold.png

回答by cgr

In our case, problem started occurring (error shows 'duplicate value for resource' in a library) when some other library was upgraded to it's latest version.
We just downgraded the version of the library that was upgraded.

在我们的例子中,当其他一些库升级到它的最新版本时,问题开始出现(错误显示库中的“资源重复值”)。
我们只是降级了升级后的库版本。

Real pain was because that we have many dependencies which can be dynamically upgraded when there is a new version available in repositories. So my teammate had to go through all libs that can be dynamically upgraded and see if there is one that is recently upgraded, try to downgrade it and check if build goes fine. He identified many were upgraded on previous day. Downgrading one of them got the build back.

真正的痛苦是因为我们有许多依赖项,当存储库中有新版本可用时,这些依赖项可以动态升级。所以我的队友不得不检查所有可以动态升级的库,看看是否有最近升级的库,尝试降级并检查构建是否正常。他发现许多是在前一天升级的。降级其中之一使构建恢复。

Conclusion we made is not to use Dynamic Versioning. Don't use dynamic versions for your dependencies.

我们得出的结论不是使用动态版本控制。不要为您的依赖项使用动态版本

This was the error by the way:

顺便说一下,这是错误:

Task :app:generateDebugResources UP-TO-DATE AGPBI: {"kind":"error","text":"error: duplicate value for resource \u0027attr/itemBackground\u0027 with config \u0027\u0027.","sources":[{"file":"/Users/unknownuser/.gradle/caches/transforms-1/files-1.1/singledateandtimepicker-1.2.2.aar/86ae3786d70c769e610c8a01d58aa638/res/values/values.xml","position":{"startLine":15,"startColumn":4,"startOffset":2427,"endColumn":48,"endOffset":2471}}],"original":"","tool":"AAPT"} AGPBI: {"kind":"error","text":"error: resource previously defined here.","sources":[{"file":"/Users/unknownuser/.gradle/caches/transforms-1/files-1.1/singledateandtimepicker-1.2.2.aar/86ae3786d70c769e610c8a01d58aa638/res/values/values.xml","position":{"startLine":15,"startColumn":4,"startOffset":2427,"endColumn":48,"endOffset":2471}}],"original":"","tool":"AAPT"}

Task :app:mergeDebugResources FAILED Error: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for details

FAILURE: Build failed with an exception.

任务 :app:generateDebugResources UP-TO-DATE AGPBI: {"kind":"error","text":"error: 资源 \u0027attr/itemBackground\u0027 与配置 \u0027\u0027 的重复值。","sources" :[{"file":"/Users/unknownuser/.gradle/caches/transforms-1/files-1.1/singledateandtimepicker-1.2.2.aar/86ae3786d70c769e610c8a01d58aa638/res/values/values:xml"{ "startLine":15,"startColumn":4,"startOffset":2427,"endColumn":48,"endOffset":2471}}],"original":"","tool":"AAPT"} AGPBI: {"kind":"error","text":"error: 先前在这里定义的资源。","sources":[{"file":"/Users/unknownuser/.gradle/caches/transforms-1/files- 1.1/singledateandtimepicker-1.2.2.aar/86ae3786d70c769e610c8a01d58aa638/res/values/values.xml","position":{"startLine":15,"startColumn":4,"startOffset":2427:2427,"endColumn "endOffset":2471}}],"original":"","tool":"AAPT"}

任务:app:mergeDebugResources FAILED 错误:java.util.concurrent.ExecutionException:com.android.builder.internal.aapt.v2.Aapt2Exception:AAPT2 错误:检查日志以获取详细信息

FAILURE:构建失败,出现异常。