Duplicate resources android studio ошибка

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.

The «Duplicate Resource» error in Android Studio can occur when a resource file or directory is declared more than once in the project, leading to conflicting resources that cause the build process to fail. This error can be caused by a variety of issues, including incorrect resource file naming, overlapping resource directories, or inconsistent resource declarations in multiple manifest files. If you’re encountering this error, there are several methods you can use to resolve the conflict and successfully build your project.

Method 1: Remove duplicated resource files

When you encounter an Android Studio Error Duplicate Resource, you can fix it by removing the duplicated resource files. Here are the steps to do it:

  1. Open your project in Android Studio.
  2. Navigate to the «res» folder in your project.
  3. Look for any duplicated resource files that have the same name and type. For example, you may have two files named «ic_launcher.png» in different folders.
  4. Delete one of the duplicated files. Make sure to keep the one that is being used in your code.
  5. Clean and rebuild your project to ensure that the error is resolved.

Here is an example of how to remove a duplicated image resource file:

<!-- Duplicated resource file in res/drawable-mdpi and res/drawable-hdpi -->
<ImageView
    android:id="@+id/my_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

To fix the error, delete one of the duplicated files (e.g. ic_launcher.png in res/drawable-hdpi), and update the ImageView to use the remaining file:

<!-- Updated ImageView to use the remaining resource file in res/drawable-mdpi -->
<ImageView
    android:id="@+id/my_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

By following these steps, you should be able to fix the Android Studio Error Duplicate Resource by removing duplicated resource files.

Method 2: Rename resource files

If you encounter the Android Studio Error Duplicate Resource, one way to fix it is by renaming resource files. Here’s how to do it:

  1. Open your Android Studio project.
  2. Navigate to the res folder.
  3. Locate the resource files that are causing the duplicate error.
  4. Right-click on the file and select «Refactor» > «Rename».
  5. Rename the file to something unique, such as adding a number or letter to the end of the file name.
  6. Click «Refactor» to apply the changes.
  7. Repeat this process for all duplicate resource files.

Here’s an example of renaming a layout file:

// Original layout file name
activity_main.xml

// Renamed layout file name
activity_main_2.xml

Here’s an example of renaming a drawable file:

// Original drawable file name
ic_launcher.png

// Renamed drawable file name
ic_launcher_2.png

By renaming the duplicate resource files, you are ensuring that each file has a unique name and avoiding the error of having duplicate resources.

That’s it! By following these steps, you should be able to fix the Android Studio Error Duplicate Resource by renaming resource files.

Method 3: Merge resource directories

If you are facing the Android Studio Error Duplicate Resource issue, you can resolve it by merging your resource directories. Follow the below steps to merge your resource directories and fix the error:

  1. Open your project in Android Studio.
  2. Navigate to the project view and expand the app folder.
  3. Right-click on the res folder and select New -> Android resource directory.
  4. In the Resource type dropdown, select the resource type that is causing the error (e.g. drawable).
  5. In the Directory name field, add a suffix to the directory name (e.g. _temp).
  6. Click Ok to create the new resource directory.
  7. Move all the conflicting resources from the original directory to the new directory.
  8. Repeat steps 3-7 for all other resource types causing the error.
  9. Once you have moved all the conflicting resources to the new directories, delete the original directories.
  10. Rename the new directories to remove the suffix you added in step 5.

Here is an example of how to merge the drawable resource directory:

1. Right-click on the `res` folder and select `New` -> `Android resource directory`.
2. Select `drawable` from the `Resource type` dropdown.
3. Add `_temp` to the `Directory name` field and click `Ok`.
4. Move all the conflicting drawable resources from the original `drawable` directory to the new `drawable_temp` directory.
5. Delete the original `drawable` directory.
6. Rename the `drawable_temp` directory to `drawable`.

Repeat these steps for all other resource types causing the error. By merging your resource directories, you should be able to resolve the Android Studio Error Duplicate Resource issue.

Method 4: Check resource declarations in manifest files

If you encounter an Android Studio error that says «Duplicate Resource», it means that there are two or more resources with the same name and type in your project. This can happen if you have imported a library that contains resources with the same name as your project’s resources. To fix this error, you can check resource declarations in manifest files. Here’s how to do it:

Step 1: Identify the Duplicate Resource

The first step is to identify which resource is causing the error. The error message should provide you with the name of the resource that is causing the problem. If not, you can use the Android Studio search function to find the resource.

Step 2: Check Manifest Files

Once you have identified the duplicate resource, you need to check the manifest files in your project. Look for any references to the resource in the manifest files. You can use the search function in Android Studio to find the resource in the manifest files.

Step 3: Resolve the Conflict

If you find that the duplicate resource is declared in a library that you have imported, you can exclude the resource from the library in your Gradle file. Here’s an example:

android {
    // ...
    configurations {
        all*.exclude group: 'com.example.library', module: 'duplicate-resource'
    }
}

In this example, the resource «duplicate-resource» from the library «com.example.library» is excluded from the build.

If the duplicate resource is declared in your own project, you need to rename one of the resources to a unique name.

Step 4: Clean and Rebuild the Project

After resolving the conflict, you need to clean and rebuild the project. This will ensure that the duplicate resource error is resolved.

То, что Android считает, что при сопоставлении изображений в ваших макетах одно и то же:

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 сообщает Android, что это изображение является 9-патч-изображением. В то время как другое изображение, login_bg.png, является обычным образом. Но с точки зрения ссылок на изображения они объявляются одинаковыми, как в следующих примерах.

Нормальное изображение:

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

Изображение Nine-patch:

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

Примечание. Нет никакой разницы в ссылках на изображения из вашего каталога /res/drawables вашего проекта Android.

См. здесь для получения дополнительной информации об изображении с девятью патчами, или для правильного термина для него доступно девять патчей. Для справки, 9-патч-чертежи должны быть объявлены как <name>.9.png, как в login_bg.9.png.

  • Review the documentation: https://facebook.github.io/react-native
  • Search for existing issues: https://github.com/facebook/react-native/issues
  • Use the latest React Native release: https://github.com/facebook/react-native/releases

Environment

React Native Environment Info:

System:
  OS: macOS 10.14
  CPU: (4) x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
  Memory: 103.10 MB / 8.00 GB
  Shell: 3.2.57 - /bin/bash
Binaries:
  Node: 8.12.0 - /usr/local/bin/node
  Yarn: 1.0.1 - /usr/local/bin/yarn
  npm: 6.4.1 - /usr/local/bin/npm
  Watchman: 4.7.0 - /usr/local/bin/watchman
SDKs:
  iOS SDK:
    Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
  Android SDK:
    API Levels: 16, 17, 19, 21, 23, 24, 25, 26, 27, 28
    Build Tools: 19.1.0, 20.0.0, 23.0.1, 23.0.2, 23.0.3, 25.0.0, 25.0.1, 25.0.2, 25.0.3, 26.0.0, 26.0.1, 26.0.2, 26.0.3, 27.0.0, 27.0.1, 27.0.3, 28.0.0, 28.0.0, 28.0.2, 28.0.3
    System Images: android-16 | ARM EABI v7a, android-16 | MIPS, android-16 | Intel x86 Atom, android-16 | Google APIs Intel x86 Atom, android-19 | Google APIs Intel x86 Atom, android-24 | Google Play Intel x86 Atom, android-26 | Google APIs Intel x86 Atom, android-26 | Google APIs Intel x86 Atom_64, android-26 | Google Play Intel x86 Atom, android-27 | Google Play Intel x86 Atom, android-28 | Google APIs Intel x86 Atom, android-P | Google APIs Intel x86 Atom, android-P | Google Play Intel x86 Atom
IDEs:
  Android Studio: 3.2 AI-181.5540.7.32.5056338
  Xcode: 10.1/10B61 - /usr/bin/xcodebuild
npmPackages:
  react: 16.6.0-alpha.8af6728 => 16.6.0-alpha.8af6728 
  react-native: 0.57.4 => 0.57.4 
npmGlobalPackages:
  babel-preset-react-native: 4.0.0
  react-native-cli: 2.0.1
  react-native-create-library: 3.1.2
  react-native-git-upgrade: 0.2.7

Description

I’m not able to create a release apk with the PNG image in Android. But can able to create a release apk when there is no PNG image in it. Here is the error I’m getting while generating the release build

[drawable-mdpi-v4/assets_mario] /Users/jeffreyrajan/Tutorials/RN/errorCheck/android/app/src/main/res/drawable-mdpi/assets_mario.png	[drawable-mdpi-v4/assets_mario] /Users/jeffreyrajan/Tutorials/RN/errorCheck/android/app/build/generated/res/react/release/drawable-mdpi-v4/assets_mario.png: Error: Duplicate resources
:app:mergeReleaseResources FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeReleaseResources'.
> [drawable-mdpi-v4/assets_mario] /Users/jeffreyrajan/Tutorials/RN/errorCheck/android/app/src/main/res/drawable-mdpi/assets_mario.png	[drawable-mdpi-v4/assets_mario] /Users/jeffreyrajan/Tutorials/RN/errorCheck/android/app/build/generated/res/react/release/drawable-mdpi-v4/assets_mario.png: Error: Duplicate resources

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Reproducible Demo

  1. Create a app — react-native init demo
  2. Create a assets folder in the project root folder.
  3. Add a PNG image inside the assets folder.
  4. Now implement a image component with the above PNG image.
  5. Now bundle it using the cmd
    react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/
  6. Then generate release apk using Generate Signed APK

Problem Description:

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

Error:Error: Duplicate resources: E:AndroidLEDappsrcmainresdrawable-hdpilogin_bg.png:drawable-hdpi-v4/login_bg, E:AndroidLEDappsrcmainresdrawable-hdpilogin_bg.9.png:drawable-hdpi-v4/login_bg
Error:Execution failed for task ':app:mergeDebugResources'.
> E:AndroidLEDappsrcmainresdrawable-hdpilogin_bg.png: Error: Duplicate resources: E:AndroidLEDappsrcmainresdrawable-hdpilogin_bg.png:drawable-hdpi-v4/login_bg, E:AndroidLEDappsrcmainresdrawable-hdpilogin_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?

Solution – 1

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:AndroidLEDappsrcmainresdrawable-hdpilogin_bg.png
E:AndroidLEDappsrcmainresdrawable-hdpilogin_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.

Solution – 2

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.

Solution – 3

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

Solution – 4

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.

Solution – 5

Solved this by going to

Build ➞ Clean Project

Solution – 6

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

Solution – 7

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

Понравилась статья? Поделить с друзьями:

Интересное по теме:

  • Dxdm exe ошибка приложения
  • Dui70 dll ошибка проводника
  • Duck game ошибка при запуске
  • Dxdiag windows 10 ошибка
  • Dtsc volvo ошибка

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии