Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Error: Duplicate resources #22234

Closed
3 tasks done
jeffreyrajanofficial opened this issue Nov 10, 2018 · 130 comments
Closed
3 tasks done

[Android] Error: Duplicate resources #22234

jeffreyrajanofficial opened this issue Nov 10, 2018 · 130 comments
Labels
Bug Platform: Android Android applications.

Comments

@jeffreyrajanofficial
Copy link

jeffreyrajanofficial commented Nov 10, 2018

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
@react-native-bot react-native-bot added the Platform: Android Android applications. label Nov 10, 2018
@ZeroCool00
Copy link

check this #19239 (comment)

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

@jeffreyrajanofficial
Copy link
Author

@ZeroCool00 wont that affect the images in Android?

@echaritonidis
Copy link

echaritonidis commented Nov 12, 2018

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
Copy link
Author

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

@vivek-walecha-657-zz
Copy link

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
Copy link
Author

jeffreyrajanofficial commented Dec 21, 2018

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
Copy link

@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
Copy link

@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
Copy link

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
Copy link

abhishekgargx commented Jan 14, 2019

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
Copy link
Contributor

mikehardy commented Jan 24, 2019

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
Copy link

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.

@mikehardy
Copy link
Contributor

I was still seeing this in RN0.58.x and it continues in RN0.59.x - are we doing something wrong here or is this really a bug?

I continue to have success with the workaround from @mkchx encoded in patch form in the patches directory for use with the patch-package module and this patch (updated for RN0.59.1)

react-native+0.59.1.patch.txt

@dragosroua
Copy link

dragosroua commented Mar 26, 2019

If you have extra resources added in custom folders, you might want to try something like this:

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

But if you have dependencies that are packing their own assets, it's not working, still getting this error (edited for clarity):

Execution failed for task ':app:mergeReleaseResources'.

> [drawable-xxxhdpi-v4/node_modules_reactnavigationstack_dist_views_assets_backicon] 
/[...]/android/app/src/main/res/drawable-xxxhdpi/node_modules_reactnavigationstack_dist_views_assets_backicon.png

[drawable-xxxhdpi-v4/node_modules_reactnavigationstack_dist_views_assets_backicon] 
/[...]/android/app/build/generated/res/react/release/drawable-xxxhdpi/node_modules_reactnavigationstack_dist_views_assets_backicon.png: 

Error: Duplicate resources

Is this actively assessed, or should we move forward with our own patches?

@mikehardy
Copy link
Contributor

@dragosroua I see a missing hyphen in your xxxhdpi curry. Coincidentally the same leading paths with problems for you?

@dragosroua
Copy link

You beat me by 2 minutes, I was about to edit that part. Yes, everything bundles ok now, but the bit with "raw" path for custom resources might be useful for somebody.

@mikehardy
Copy link
Contributor

@dragosroua glad you're compiling now - I remember how frustrating this one was for me, and I'm still amazed it's not fixed in master though I haven't proposed a PR either so I guess I get out what I put in...

@zakabluk
Copy link

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

Pls explain me why for my react-native 0.57.5 it's doesn't work?
I created pacth file. Added to package.json. Run npm install and has as result

        def currentBundleTask = tasks.create(
            name: "bundle${targetName}JsAndAssets",
            type: Exec) {
            group = "react"
            description = "bundle JS and assets for ${targetName}."

            // Create dirs if they are not there (e.g. the "clean" task just ran)
            doFirst {
                jsBundleDir.deleteDir()
                jsBundleDir.mkdirs()
                resourcesDir.deleteDir()
                resourcesDir.mkdirs()
            }

            // Set up inputs and outputs so gradle can cache the result
            inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)
            outputs.dir jsBundleDir
            outputs.dir resourcesDir

without necessary changes.

@mikehardy
Copy link
Contributor

@zakabluk you'd need to post the output of your npm install, but as a guess it is because the patch-package package is very careful about version numbers. you are trying on 57.5, but the patch is against 57.8?

@furedal
Copy link

furedal commented Apr 8, 2019

I usually make a python scripts for patching node_modules.
Add this as postinstall.py and add it to your postinstall script or run it with ./postinstall.py

#!/usr/bin/env python3

import os
import textwrap

def file_dir():
  return os.path.dirname(os.path.realpath(__file__))

def read_file(filename):
    '''
    Reads the specified file.

    :param filename: The file to read
    :return: The content of the specified file
    '''
    if os.path.exists(filename):
        with open(filename, "r") as file:
            return file.read()
    else:
        raise IOError("file {} not found.".format(filename))

def write_file(filename, text):
    '''
    Writes the specified text to the specified file.

    :param filename: The file to write to
    :param text: The text to write
    '''
    with open(filename, "w") as file:
        file.write(text)

def fix_android_assets():
  print("Fixing android error with duplicate assets: https://github.com/facebook/react-native/issues/22234")

  gradle_file_path = "{}/node_modules/react-native/react.gradle".format(file_dir())

  code_snippet = textwrap.indent("""\
            // Added by post_install
            // Fix for: https://github.com/facebook/react-native/issues/22234
            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()
            }
  """, "")

  text = read_file(gradle_file_path)

  start = text.find("doFirst", 0)
  end = text.find("}", start)
  end = text.find("\n", end) + 1
  
  text = text[:end] + code_snippet + text[end:]

  write_file(gradle_file_path, text)

def main():
    fix_android_assets()

if __name__ == "__main__":
    main()

Here you are able to add your own scripts if required

@mikehardy
Copy link
Contributor

mikehardy commented Apr 8, 2019

Looks like a re-implementation of what you get with npm install patch-package but if python is your thing and you want to maintain more code yourself it does seem viable. I'm still using patch-package for what it's worth, with 0.59.3 like so
react-native+0.59.3.patch.txt

@hramos - #19239 was similar (I think) and this is long-standing but appears to have a fix. Does this just need a PR for an ultimate fix or am I missing a reason why the patch used here is not viable? (I might be). If we just need a PR I could send one in...

@wmailn
Copy link

wmailn commented Apr 10, 2019

Looks like a re-implementation of what you get with npm install patch-package but if python is your thing and you want to maintain more code yourself it does seem viable. I'm still using patch-package for what it's worth, with 0.59.3 like so
react-native+0.59.3.patch.txt

@hramos - #19239 was similar (I think) and this is long-standing but appears to have a fix. Does this just need a PR for an ultimate fix or am I missing a reason why the patch used here is not viable? (I might be). If we just need a PR I could send one in...

How to use this path , thank

@mikehardy
Copy link
Contributor

@ZhanRU - https://github.com/ds300/patch-package#set-up - you just want to put that patch (with .patch extension) into the 'patches' directory in your project after installing and setting up patch-package

@wmailn
Copy link

wmailn commented Apr 10, 2019

@ZhanRU - https://github.com/ds300/patch-package#set-up - you just want to put that patch (with .patch extension) into the 'patches' directory in your project after installing and setting up patch-package

Thank you very much

@mikehardy
Copy link
Contributor

For anyone still following along, I recently integrated an external system and needed to separate my testing from production external data, which leads to using "flavors" in gradle so you can have qaDebug, stagingRelease, etc etc pointing to different external system. The patch here did not support that though, so I added flavor support, and my patch looks like this now. It lives in patches/react-native+0.59.5.patch where it is applied during npm i runs after npm install patch-package

diff --git a/node_modules/react-native/react.gradle b/node_modules/react-native/react.gradle
index 4ead2b6..e0f92b7 100644
--- a/node_modules/react-native/react.gradle
+++ b/node_modules/react-native/react.gradle
@@ -48,6 +48,33 @@ afterEvaluate {
                 resourcesDir.mkdirs()
             }
 
+            // From https://stackoverflow.com/questions/53239705/react-native-error-duplicate-resources-android
+            // Currently has no solution?
+
+            // IF you are using flavors, add flavor name to the path you move from
+            def flavorPathSegment = ""
+            android.productFlavors.all { flavor ->
+                if (targetName.toLowerCase().contains(flavor.name)) {
+                    flavorPathSegment = flavor.name + "/"
+                }
+            }
+
+            doLast {
+                def moveFunc = { resSuffix ->
+                    File originalDir = file("$buildDir/generated/res/react/${flavorPathSegment}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()
+            }
+
             // Set up inputs and outputs so gradle can cache the result
             inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)
             outputs.dir(jsBundleDir)

@AliRehman7141
Copy link

My solution:
Delete all files in /your_project/android/app/src/main/raw/res.
It's work for me!

Thanks, Bro It works for me as well.

@maacofficial
Copy link

Change your wrong .png or .jpg file. It is solved... 😇

@reyn-nova
Copy link

reyn-nova commented Mar 19, 2021

Why solving this still using workaround? why there is no fixing approach? I agree with what @safaiyeh says, editing node_modules everytime after npm install is not ideal, because your changes in node_modules are not stored in git

@Milker90
Copy link

I solved the problem by adding code here "node_modules/react-native/react.gradle" in react-native 0.63.4

截屏2021-04-24 上午10 30 03

        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()                
        }

@maheenriaz
Copy link

Renamed the image from assets folder that is duplicate and then goto android/app/src/main/res/drawable-mdpi/
and delete image that were duplicate solved my problem!!

@skjangid
Copy link

skjangid commented Jul 2, 2022

@ZeroCool00 wont that affect the images in Android?

This is working for me. Thanks

@rafaelmaeuer
Copy link

rafaelmaeuer commented Sep 10, 2022

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

@adilexperience
Copy link

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.

Additionally removing raw folder fixed issue for me, Remove these and build again:

android/app/src/main/res/drawable-mdpi/
android/app/src/main/res/drawable-xhdpi/
android/app/src/main/res/drawable-xxhdpi/
android/app/src/main/res/raw/

Thanks

@PreetikaSingh
Copy link

Try this :

Delete build inside android/app folder
Delete build inside android folder
run rm -rf $HOME/.gradle/caches/
Open build.gradle --> android/app/build.gradle
comment this line
//apply from: "../../node_modules/react-native/react.gradle"
Delete index.android.bundle file from assets folder and re-create using 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
run react-native run-android Or run react-native run-android --variant=release

@agunable
Copy link

agunable commented Feb 2, 2023

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()
}

I don't know how, I'don't know why but I've been searching for answers for 3 days and this solution works! thanks. Maybe you can explain what this code do to our project

@cortinico
Copy link
Contributor

I'm closing this issue as it's a 2018 issue and essentially unactionable from us.
The original issue is from React Native 0.57.x

If you're still having this failures on the latest version of the framework, please open a new issue.

@andreiciceu
Copy link

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

Thank you @andreiciceu. This worked for me too.

@jcmunoz200
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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

@sharjeelhaider
Copy link

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
Copy link

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
Copy link

gigby commented Mar 27, 2024

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
Copy link

rafaelmaeuer commented Apr 5, 2024

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
Copy link

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
Labels
Bug Platform: Android Android applications.
Projects
None yet
Development

No branches or pull requests