Skip to content

[Android] Error: Duplicate resources #22234

Closed
@jeffreyrajanofficial

Description

@jeffreyrajanofficial

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

Activity

ZeroCool00

ZeroCool00 commented on Nov 11, 2018

@ZeroCool00

check this #19239 (comment)

You need to remove drawable folder image if there is any?

jeffreyrajanofficial

jeffreyrajanofficial commented on Nov 12, 2018

@jeffreyrajanofficial
Author

@ZeroCool00 wont that affect the images in Android?

echaritonidis

echaritonidis commented on Nov 12, 2018

@echaritonidis

Mapsy's answer should help https://stackoverflow.com/a/52750886
So basically you edit the /node_modules/react-native/react.gradle file
and add the doLast right after the doFirst block, manually.

doFirst { ... }
doLast {
    def moveFunc = { resSuffix ->
        File originalDir = file("$buildDir/generated/res/react/release/drawable-${resSuffix}");
        if (originalDir.exists()) {
            File destDir = file("$buildDir/../src/main/res/drawable-${resSuffix}");
            ant.move(file: originalDir, tofile: destDir);
        }
    }
    moveFunc.curry("ldpi").call()
    moveFunc.curry("mdpi").call()
    moveFunc.curry("hdpi").call()
    moveFunc.curry("xhdpi").call()
    moveFunc.curry("xxhdpi").call()
    moveFunc.curry("xxxhdpi").call()
}
jeffreyrajanofficial

jeffreyrajanofficial commented on Nov 12, 2018

@jeffreyrajanofficial
Author

@ZeroCool00 @mkchx I checked with both of your answer, its working. Thanks a lot guys :)

vivek-walecha-657-zz

vivek-walecha-657-zz commented on Dec 21, 2018

@vivek-walecha-657-zz

Hi all how will we able to get this done with jenkins job. As it will do npm install always which override this change in react.gradle file. We can create build on android studio for android but not possible on jenkins.

jeffreyrajanofficial

jeffreyrajanofficial commented on Dec 21, 2018

@jeffreyrajanofficial
Author

Hi @vivek-walecha-657 I haven't tried this but you can try this command for creating offline bundling

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle

vivek-walecha-657-zz

vivek-walecha-657-zz commented on Dec 24, 2018

@vivek-walecha-657-zz

@jeffreyrajanofficial Thanx for writing, the solution you have provided will help if we go and change react.gradle file. But I don't want to make the release by changing the react.gradle file every time i do npm install everywhere.

vivek-walecha-657-zz

vivek-walecha-657-zz commented on Dec 24, 2018

@vivek-walecha-657-zz

@jeffreyrajanofficial Can you please tell which version(latest lower than this or higher than this) is working fine, without this issue. Because release notes don't tell anything that this issue is resolved.

vivek-walecha-657-zz

vivek-walecha-657-zz commented on Jan 3, 2019

@vivek-walecha-657-zz

Things are sorted now in RN > 57 react.gradle file automatically creates the bundle.
for creating a release build you dont need to run the npm run build:android:release

abhishekgargx

abhishekgargx commented on Jan 14, 2019

@abhishekgargx

Using 55.4 react native version here is my sample project gist for build.gradle package.json with fixes.

https://gist.github.com/Abhishekgarg727/daf031fb9f94fdfd985e84db57dedbe1

mikehardy

mikehardy commented on Jan 24, 2019

@mikehardy
Contributor

I was still seeing this, using macOS 10.14.3 + RN 0.57.8 + Android Studio 3.3 + Gradle 4.10.3. Maybe I'm not the only one? Or maybe someone here can confirm it works so I'll dig more and fix it for myself for real.

I'm currently working around it with the "patch-package" package in combination with the attached patch based on the above comment from @mkchx (with .txt suffix appended so github would accept the attachment) in order to automagically fix it on 'npm install' after adding postinstall: patch-package to my package.json scripts.

Maybe this is useful to someone...
react-native+0.57.8.patch.txt

rahulkumar1409

rahulkumar1409 commented on Mar 1, 2019

@rahulkumar1409

Remove the files you might have on:

android/app/src/main/res/drawable-mdpi/
android/app/src/main/res/drawable-xhdpi/
android/app/src/main/res/drawable-xxhdpi/
Run Build again, This fixed the issue for me.

132 remaining items

andreiciceu

andreiciceu commented on Feb 17, 2023

@andreiciceu

The problem for my setup was this line in app/build.gradle leftover from older react-native versions:

apply from: "../../node_modules/react-native/react.gradle"

removed it and build works now

varadarajan-videoyantra

varadarajan-videoyantra commented on Mar 12, 2023

@varadarajan-videoyantra

Thank you @andreiciceu. This worked for me too.

jcmunoz200

jcmunoz200 commented on Apr 24, 2023

@jcmunoz200

Sorry for my english.

This works for me.

Add new scripts in package.json

  1. Generate the new bundle with generate:bundle
  2. Clean up resources in drawable with clean:drawable
  3. To generate an apk in release mode run yarn android:release
{
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint . --ext .js,.jsx,.ts,.tsx",
    "clean:drawable": "rm -rf ./android/app/src/main/res/drawable-* && rm -rf ./android/app/src/main/res/raw",
    "generate:bundle": "npx 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 && yarn clean:drawable",
    "android:release": "yarn generate:bundle && npx react-native run-android --variant=release"
  }
}
yourerrorbro

yourerrorbro commented on Jun 6, 2023

@yourerrorbro

I hope this email finds you well. I wanted to share an informative blog post with you that I recently came across. Here i provide proper solution. In above example i check. Initially worked but when build upload that's time create issue.

https://medium.com/@yourerrorbro/handling-duplicate-resource-issue-in-android-released-builds-in-react-native-f63c67176bce

SteveLai524

SteveLai524 commented on Aug 8, 2023

@SteveLai524

I remove it, but it still exist...I upgrade my react-native to "0.72.3", and it have not the file, and command. but it still exist the problem, and no idea to fix it.

Chronostyle

Chronostyle commented on Aug 14, 2023

@Chronostyle

I remove it, but it still exist...I upgrade my react-native to "0.72.3", and it have not the file, and command. but it still exist the problem, and no idea to fix it.

use @jcmunoz200 solution, then after you want to release it to playstore, do: cd android && ./gradlew bundleRelease

SteveLai524

SteveLai524 commented on Aug 21, 2023

@SteveLai524

I remove it, but it still exist...I upgrade my react-native to "0.72.3", and it have not the file, and command. but it still exist the problem, and no idea to fix it.

use @jcmunoz200 solution, then after you want to release it to playstore, do: cd android && ./gradlew bundleRelease

It doesn't work. so i downgrade my react-native version.

linhvovan29546

linhvovan29546 commented on Aug 24, 2023

@linhvovan29546

The problem for my setup was this line in app/build.gradle leftover from older react-native versions:

apply from: "../../node_modules/react-native/react.gradle"

removed it and build works now

Thank you, this is working for me.

Sourav-techlur

Sourav-techlur commented on Oct 4, 2023

@Sourav-techlur

work. so i downgrade my react-native version.

@SteveLai524 i am also facing same issue could you please tell me to which version you downgraded?

yogendrajs

yogendrajs commented on Dec 4, 2023

@yogendrajs

I don't have react.gradle file in RN v0.72.6, what do I have to do for this?

sharjeelhaider

sharjeelhaider commented on Dec 6, 2023

@sharjeelhaider

I don't have react.gradle file in RN v0.72.6, what do I have to do for this?

Same problem for me. Not able find solution for this. I am using RN v0.72.7

kaxi1993

kaxi1993 commented on Dec 12, 2023

@kaxi1993

Sorry for my english.

This works for me.

Add new scripts in package.json

  1. Generate the new bundle with generate:bundle
  2. Clean up resources in drawable with clean:drawable
  3. To generate an apk in release mode run yarn android:release
{
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint . --ext .js,.jsx,.ts,.tsx",
    "clean:drawable": "rm -rf ./android/app/src/main/res/drawable-* && rm -rf ./android/app/src/main/res/raw",
    "generate:bundle": "npx 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 && yarn clean:drawable",
    "android:release": "yarn generate:bundle && npx react-native run-android --variant=release"
  }
}

It worked fine in my case

gigby

gigby commented on Mar 27, 2024

@gigby

The problem for my setup was this line in app/build.gradle leftover from older react-native versions:

apply from: "../../node_modules/react-native/react.gradle"

removed it and build works now

Which RN version did you have?

I have 0.68 and this doesn't work

rafaelmaeuer

rafaelmaeuer commented on Apr 5, 2024

@rafaelmaeuer

I initially met this problem like 4 years ago. I can hardly believe that today with RN 0.70.0 the problem still exists...

After upgrading an old react-native project to v0.71.0 the problem is still valid (6 years!!!). None of the fixes earlier in use seem to work (doLast, clean res, etc.) Anyone else having this issue and found a solution already?

Edit: After some hours of investigation I did found another solution. It seems that patching node_modules/react-native/react.gradle with doLast isn't executed anymore. However if we add the patch in app/build.gradle to a doLast section for task createBundleReleaseJsAndAssets it works again:

// run pre build tasks
tasks.whenTaskAdded { task ->
    if (task.name == 'createBundleReleaseJsAndAssets') {
          doLast {
              def moveFolderFunc = { folderName ->
                  File originalDir = file("$buildDir/generated/res/react/release/${folderName}");
                  if (originalDir.exists()) {
                      File destDir = file("$buildDir/../src/main/res/${folderName}");
                      ant.move(file: originalDir, tofile: destDir);
                  }
              }
  
              moveFolderFunc.curry("drawable-ldpi").call()
              moveFolderFunc.curry("drawable-mdpi").call()
              moveFolderFunc.curry("drawable-hdpi").call()
              moveFolderFunc.curry("drawable-xhdpi").call()
              moveFolderFunc.curry("drawable-xxhdpi").call()
              moveFolderFunc.curry("drawable-xxxhdpi").call()
              moveFolderFunc.curry("raw").call()
          }
    }
}
RohitMau

RohitMau commented on May 16, 2024

@RohitMau

I don't have react.gradle file in RN v0.72.6, what do I have to do for this?

@yogendrajs && @sharjeelhaider did you get solution for that scenario?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @cpojer@hramos@wilsolutions@hapablap21@mikehardy

        Issue actions

          [Android] Error: Duplicate resources · Issue #22234 · facebook/react-native