-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Comments
check this #19239 (comment) You need to remove drawable folder image if there is any? |
@ZeroCool00 wont that affect the images in Android? |
Mapsy's answer should help https://stackoverflow.com/a/52750886
|
@ZeroCool00 @mkchx I checked with both of your answer, its working. Thanks a lot guys :) |
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. |
Hi @vivek-walecha-657 I haven't tried this but you can try this command for creating offline bundling
|
@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. |
@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. |
Things are sorted now in RN > 57 react.gradle file automatically creates the bundle. |
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 |
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 Maybe this is useful to someone... |
Remove the files you might have on: android/app/src/main/res/drawable-mdpi/ |
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) |
If you have extra resources added in custom folders, you might want to try something like this:
|
@dragosroua I see a missing hyphen in your xxxhdpi curry. Coincidentally the same leading paths with problems for you? |
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. |
@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... |
Pls explain me why for my react-native 0.57.5 it's doesn't work?
without necessary changes. |
@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? |
I usually make a python scripts for patching node_modules. #!/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 |
Looks like a re-implementation of what you get with @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 |
@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 |
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 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) |
Thanks, Bro It works for me as well. |
Change your wrong .png or .jpg file. It is solved... 😇 |
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 |
I solved the problem by adding code here "node_modules/react-native/react.gradle" in react-native 0.63.4
|
Renamed the image from assets folder that is duplicate and then goto android/app/src/main/res/drawable-mdpi/ |
This is working for me. Thanks |
I initially met this problem like 4 years ago. I can hardly believe that today with RN 0.70.0 the problem still exists... |
Additionally removing raw folder fixed issue for me, Remove these and build again: android/app/src/main/res/drawable-mdpi/ Thanks |
Try this : Delete build inside android/app folder |
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 |
I'm closing this issue as it's a 2018 issue and essentially unactionable from us. If you're still having this failures on the latest version of the framework, please open a new issue. |
The problem for my setup was this line in apply from: "../../node_modules/react-native/react.gradle" removed it and build works now |
Thank you @andreiciceu. This worked for me too. |
Sorry for my english. This works for me. Add new scripts in package.json
|
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. |
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. |
Thank you, this is working for me. |
@SteveLai524 i am also facing same issue could you please tell me to which version you downgraded? |
I don't have |
Same problem for me. Not able find solution for this. I am using RN v0.72.7 |
It worked fine in my case |
Which RN version did you have? I have 0.68 and this doesn't work |
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 ( Edit: After some hours of investigation I did found another solution. It seems that patching // 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()
}
}
} |
|
Environment
React Native Environment Info:
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
Reproducible Demo
react-native init demo
assets
folder in the project root folder.image
component with the above PNG image.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/
Generate Signed APK
The text was updated successfully, but these errors were encountered: