Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Fix error: CodePushHash: Resource and asset merger: Duplicate resources #2574

Closed
wants to merge 2 commits into from

Conversation

sanduluca
Copy link

Delete file before create & write

Solves #2497

@sanduluca sanduluca requested a review from a team as a code owner August 25, 2023 11:06
@XantreDev
Copy link

When it will be reviewed?

@XantreDev
Copy link

In my case adding this patch is not actually fixes subsequent builds. This workaround fixes the issue

./gradlew bundleRelease
rm -rf app/build/generated
gradlew assembleRelease

@brianlenz
Copy link

Yeah, I have found the same thing. The Duplicate resources error still happens with this patch, but simply re-running the build will fix it when it happens.

@DmitriyKirakosyan
Copy link
Contributor

@XantreGodlike , am I right that the fix in this PR doesn't work for you?
@brianlenz , can you also verify these changes?

@XantreDev
Copy link

@DmitriyKirakosyan yep
We are using this in our script as workaround

./gradlew bundleRelease
rm -rf app/build/generated
./gradlew assembleRelease

@brianlenz
Copy link

@DmitriyKirakosyan the changes here seem to improve things, but it doesn't fix the problem completely. To test, I just cleaned the Android project completely and rebuilt. It worked successfully. Then I did a subsequent build, which is incremental, and it failed with the usual CodePushHash: Resource and asset merger: Duplicate resources error. After the error happened, I can re-run it successfully many times without issue. So it seems there's something weird about the second build after a clean rebuild that still has issues? I can reproduce this reliably.

So, I do think this change may improve things, but it definitely doesn't solve it definitively.

@sanduluca
Copy link
Author

So, I do think this change may improve things, but it definitely doesn't solve it definitively.

What things does it improve ? It either fixes the problem or not.

Tested once again on my: MacBook Pro M2 (macos Ventura 13.5)

Guys how did you test this PR ? How do you know that it is now working ?
@brianlenz
@XantreGodlike

@XantreDev
Copy link

I'm just applying this patch, but susequent builds is not working

diff --git a/scripts/generateBundledResourcesHash.js b/scripts/generateBundledResourcesHash.js
index 01ab70ca51e274eeedc22a03d0b1416280d9b0ce..8bd21491020eae7c4d772925ee08cbc6098fb09f 100644
--- a/scripts/generateBundledResourcesHash.js
+++ b/scripts/generateBundledResourcesHash.js
@@ -78,6 +78,9 @@ function addJsBundleAndMetaToManifest() {
             console.log(finalHash);
 
             var savedResourcesManifestPath = assetsDir + "/" + CODE_PUSH_HASH_FILE_NAME;
+            if (fs.existsSync(savedResourcesManifestPath)) {
+                fs.unlinkSync(savedResourcesManifestPath);
+            }
             fs.writeFileSync(savedResourcesManifestPath, finalHash);
 
             // "CodePushHash.json" file name breaks flow type checking

@sanduluca
Copy link
Author

@XantreGodlike
Can you post the sequence of commands you execute and shows you the error ? Can you also post the error you get ?

Also what version of react-native-code-push you are using ?

@DmitriyKirakosyan yep We are using this in our script as workaround

./gradlew bundleRelease
rm -rf app/build/generated
./gradlew assembleRelease

What are you doing with this script ? Its generates you a bundle ( .aab file ). After you delete the app/build/generated folder, and then you generate an apk ( .apk file ).

I executed ./gradlew bundleRelease 5 times in raw and didn't get the error.
I executed ./gradlew assembleRelease 5 times in a raw and didn't get the error
I even executed this sequence of commands and none of this failed

./gradlew bundleRelease
./gradlew assembleRelease
./gradlew bundleRelease
./gradlew assembleRelease

@brianlenz
Copy link

@sanduluca I typically run a variation of yarn react-native run-android. I was able to reproduce the issue as I described above after first running ./gradlew clean and then consecutively running yarn react-native run-android.

I'm able to reproduce the exact same pattern if I run ./gradlew clean and then consecutively running ./gradlew bundleDevRelease: the first time succeeds, the second time fails with the "Duplicate resources" error, and the third time onward succeeds. We are using flavors, so the Dev is necessary to get a specific flavor to build.

This is the error:

> Task :app:generateBundledResourcesHashDevRelease
2d3f0a44e0809b840d12a1734cf4f99a2d259eae7fe7209e980d9755a40c147a

> Task :app:mergeDevReleaseAssets FAILED
ERROR:/Users/brian/x/app/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/CodePushHash /Users/brian/x/app/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/CodePushHash: Resource and asset merger: Duplicate resources

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDevReleaseAssets'.
> /Users/brian/x/app/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/CodePushHash    /Users/brian/x/app/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/CodePushHash: Error: Duplicate resources

I tried removing this patch and confirmed the exact same behavior, so it does appear the patch here makes no difference for our case.

@mitaxe
Copy link

mitaxe commented Oct 12, 2023

Still an issue on 0.72.5

@sanduluca
Copy link
Author

Can you guys paste here your error as you may have other Duplicate resources error.

Also anyone confirmed that this patch fixed the Duplicate resources error for android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/CodePushHash ?

@brianlenz
Copy link

@sanduluca I did some more testing here. The issue seems to be caused purely by react-native-code-push. I removed the package from our project altogether and ran the same sequence of ./gradlew clean followed by multiple ./gradlew bundleDevRelease, and the issue doesn't occur without react-native-code-push in our project. As soon as I add back in react-native-code-push, the error presents itself again, exactly as I described above. The behavior is identical with and without the patch here. The second invocation of ./gradlew bundleDevRelease fails with the error:

> Task :app:generateBundledResourcesHashDevRelease
04ae4ccd01749d8fe58ce15eee46ad28f7c4d4a663e6065b6798ed80ef3b9aa3

> Task :app:mergeDevReleaseAssets FAILED
ERROR:/Users/brian/x/app/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/CodePushHash /Users/brian/x/app/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/CodePushHash: Resource and asset merger: Duplicate resources

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDevReleaseAssets'.
> /Users/brian/x/app/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/CodePushHash    /Users/brian/x/app/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/CodePushHash: Error: Duplicate resources

I've also tested it with different flavors (./gradlew bundleStagingRelease and ./gradlew bundleProdRelease), and they exhibit identical behavior:

> Task :app:generateBundledResourcesHashStagingRelease
04ae4ccd01749d8fe58ce15eee46ad28f7c4d4a663e6065b6798ed80ef3b9aa3

> Task :app:mergeStagingReleaseAssets FAILED
ERROR:/Users/brian/x/app/android/app/build/generated/assets/createBundleStagingReleaseJsAndAssets/CodePushHash /Users/brian/x/app/android/app/build/generated/assets/createBundleStagingReleaseJsAndAssets/CodePushHash: Resource and asset merger: Duplicate resources

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeStagingReleaseAssets'.
> /Users/brian/x/app/android/app/build/generated/assets/createBundleStagingReleaseJsAndAssets/CodePushHash        /Users/brian/x/app/android/app/build/generated/assets/createBundleStagingReleaseJsAndAssets/CodePushHash: Error: Duplicate resources
> Task :app:generateBundledResourcesHashProdRelease
04ae4ccd01749d8fe58ce15eee46ad28f7c4d4a663e6065b6798ed80ef3b9aa3

> Task :app:mergeProdReleaseAssets FAILED
ERROR:/Users/brian/x/app/android/app/build/generated/assets/createBundleProdReleaseJsAndAssets/CodePushHash /Users/brian/x/app/android/app/build/generated/assets/createBundleProdReleaseJsAndAssets/CodePushHash: Resource and asset merger: Duplicate resources

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeProdReleaseAssets'.
> /Users/brian/x/app/android/app/build/generated/assets/createBundleProdReleaseJsAndAssets/CodePushHash   /Users/brian/x/app/android/app/build/generated/assets/createBundleProdReleaseJsAndAssets/CodePushHash: Error: Duplicate resources

@DmitriyKirakosyan
Copy link
Contributor

@brianlenz, thank you for delving into this issue. To ensure I'm following correctly:

  1. The change in this PR doesn't seem to entirely resolve the problem, correct?
  2. You've tested it within your project and determined the issue arises when react-native-code-push is introduced as a dependency.

For further clarity, have you tried replicating this issue in a freshly created project? This step could help pinpoint the root cause more precisely.

@brianlenz
Copy link

@DmitriyKirakosyan

  1. This PR doesn't seem to alter the behavior at all: the behavior remains the same with and without the patch provided here.
  2. Yep, the issue only occurs with react-native-code-push.

I just test in a freshly created RN project, and I could not reproduce the "Duplicate resources" issue at all. This was a test without the changes from this PR. I simply used this to test:

./gradlew clean
./gradlew bundleDebug
./gradlew bundleDebug
./gradlew bundleDebug

So then I decided to enable Android flavors (which is what we use in our project). To do this, I simply did:

  1. Create new project with npx react-native@latest init CodePushTest.
  2. yarn add react-native-code-push.
  3. Follow Android setup steps.
  4. Remove app_name from android/app/src/main/res/strings.xml (will be configured per flavor).
  5. Introduce flavors into android/app/build.gradle by adding the following config into the android block:
    flavorDimensions "environment"

    productFlavors {
        Dev {
            dimension "environment"
            applicationIdSuffix ".dev"
            versionNameSuffix ""
            resValue "string", "app_name", "CodePushTest Dev"
            resValue "string", "CodePushDeploymentKey", "xxx"
        }
        Staging {
            dimension "environment"
            applicationIdSuffix ".staging"
            versionNameSuffix ""
            resValue "string", "app_name", "CodePushTest Staging"
            resValue "string", "CodePushDeploymentKey", "yyy"
        }
        Prod {
            dimension "environment"
            applicationIdSuffix ""
            versionNameSuffix ""
            resValue "string", "app_name", "CodePushTest"
            resValue "string", "CodePushDeploymentKey", "zzz"
        }
    }

Once I did that, the behavior reproduces exactly as I've described above. Run ./gradlew bundleDevRelease twice, and you will get the exact issue.

Then, to test this patch, I ran:

yarn remove react-native-code-push
yarn add https://github.com/sanduluca/react-native-code-push#master

Then, simply run:

./gradlew clean
./gradlew bundleDevRelease
./gradlew bundleDevRelease

And you'll get the exact same behavior and error. Here is the full log output:

brian:android brian$ ./gradlew clean

BUILD SUCCESSFUL in 4s
7 actionable tasks: 6 executed, 1 up-to-date
brian:android brian$ ./gradlew bundleDevRelease

> Task :app:createBundleDevReleaseJsAndAssets
debug Reading Metro config from /Users/brian/CodePushTest/metro.config.js
warning: the transform cache was reset.
                Welcome to Metro v0.76.8
              Fast - Scalable - Integrated


info Writing bundle output to:, /Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle
info Writing sourcemap output to:, /Users/brian/CodePushTest/android/app/build/intermediates/sourcemaps/react/DevRelease/index.android.bundle.packager.map
info Done writing bundle output
info Done writing sourcemap output
info Copying 1 asset files
info Done copying assets
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:2305:18: warning: the variable "DebuggerInternal" was not declared in function "__shouldPauseOnThrow"
          typeof DebuggerInternal !== 'undefined' &&
                 ^~~~~~~~~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:5996:7: warning: the variable "setTimeout" was not declared in function "logCapturedError"
      setTimeout(function () {
      ^~~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:3965:108: warning: the variable "nativeFabricUIManager" was not declared in function "onChange"
...lInstanceHandle ? (from && nativeFabricUIManager.setIsJSResponder(from.sta...
                              ^~~~~~~~~~~~~~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:4577:21: warning: the variable "clearTimeout" was not declared in anonymous function " 101#"
    cancelTimeout = clearTimeout;
                    ^~~~~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:8327:30: warning: the variable "__REACT_DEVTOOLS_GLOBAL_HOOK__" was not declared in anonymous function " 101#"
  if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:10701:5: warning: the variable "setImmediate" was not declared in function "handleResolved"
    setImmediate(function () {
    ^~~~~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:14972:12: warning: the variable "fetch" was not declared in anonymous function " 345#"
    fetch: fetch,
           ^~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:14973:14: warning: the variable "Headers" was not declared in anonymous function " 345#"
    Headers: Headers,
             ^~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:14974:14: warning: the variable "Request" was not declared in anonymous function " 345#"
    Request: Request,
             ^~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:14975:15: warning: the variable "Response" was not declared in anonymous function " 345#"
    Response: Response
              ^~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:15132:24: warning: the variable "FileReader" was not declared in function "readBlobAsArrayBuffer"
      var reader = new FileReader();
                       ^~~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:15183:36: warning: the variable "Blob" was not declared in anonymous function " 356#"
        } else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
                                   ^~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:15185:40: warning: the variable "FormData" was not declared in anonymous function " 356#"
        } else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
                                       ^~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:15187:44: warning: the variable "URLSearchParams" was not declared in anonymous function " 356#"
...e if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body...
                                 ^~~~~~~~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:15306:26: warning: the variable "AbortController" was not declared in anonymous function " 362#"
          var ctrl = new AbortController();
                         ^~~~~~~~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:15439:23: warning: the variable "XMLHttpRequest" was not declared in anonymous function " 366#"
        var xhr = new XMLHttpRequest();
                      ^~~~~~~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:14985:71: warning: the variable "self" was not declared in anonymous function " 348#"
...undefined' && globalThis || typeof self !== 'undefined' && self ||
                                      ^~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:23797:27: warning: the variable "performance" was not declared in anonymous function " 641#"
  if ("object" === typeof performance && "function" === typeof performance.no...
                          ^~~~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:23820:26: warning: the variable "navigator" was not declared in anonymous function " 641#"
  "undefined" !== typeof navigator && undefined !== navigator.scheduling && u...
                         ^~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:23930:37: warning: the variable "MessageChannel" was not declared in anonymous function " 641#"
  };else if ("undefined" !== typeof MessageChannel) {
                                    ^~~~~~~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:23945:34: warning: the variable "nativeRuntimeScheduler" was not declared in anonymous function " 641#"
... = "undefined" !== typeof nativeRuntimeScheduler ? nativeRuntimeScheduler....
                             ^~~~~~~~~~~~~~~~~~~~~~
/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/index.android.bundle:33697:34: warning: the variable "requestAnimationFrame" was not declared in function "start 9#"
...    this._animationFrame = requestAnimationFrame(this.onUpdate.bind(this));
                              ^~~~~~~~~~~~~~~~~~~~~

> Task :app:generateBundledResourcesHashDevRelease
37a65beeb35ec35b1b250d2f04b3deffbb0492b788570bee7dc06540d072f946

> Task :react-native-code-push:processReleaseManifest
package="com.microsoft.codepush.react" found in source AndroidManifest.xml: /Users/brian/CodePushTest/node_modules/react-native-code-push/android/app/src/main/AndroidManifest.xml.
Setting the namespace via a source AndroidManifest.xml's package attribute is deprecated.
Please instead set the namespace (or testNamespace) in the module's build.gradle file, as described here: https://developer.android.com/studio/build/configure-app-module#set-namespace
This migration can be done automatically using the AGP Upgrade Assistant, please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information.

> Task :react-native-code-push:compileReleaseJavaWithJavac
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/brian/CodePushTest/node_modules/react-native-code-push/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

> Task :app:stripDevReleaseDebugSymbols
Unable to strip the following libraries, packaging them as they are: libc++_shared.so, libfabricjni.so, libfb.so, libfbjni.so, libfolly_runtime.so, libglog.so, libglog_init.so, libhermes.so, libhermes_executor.so, libimagepipeline.so, libjsi.so, libjsijniprofiler.so, libjsinspector.so, liblogger.so, libmapbufferjni.so, libnative-filters.so, libnative-imagetranscoder.so, libreact_codegen_rncore.so, libreact_config.so, libreact_debug.so, libreact_nativemodule_core.so, libreact_newarchdefaults.so, libreact_render_animations.so, libreact_render_attributedstring.so, libreact_render_componentregistry.so, libreact_render_core.so, libreact_render_debug.so, libreact_render_graphics.so, libreact_render_imagemanager.so, libreact_render_leakchecker.so, libreact_render_mapbuffer.so, libreact_render_mounting.so, libreact_render_runtimescheduler.so, libreact_render_scheduler.so, libreact_render_telemetry.so, libreact_render_templateprocessor.so, libreact_render_textlayoutmanager.so, libreact_render_uimanager.so, libreact_utils.so, libreactnativeblob.so, libreactnativejni.so, libreactperfloggerjni.so, librrc_image.so, librrc_legacyviewmanagerinterop.so, librrc_root.so, librrc_scrollview.so, librrc_text.so, librrc_textinput.so, librrc_unimplementedview.so, librrc_view.so, libruntimeexecutor.so, libturbomodulejsijni.so, libyoga.so.

BUILD SUCCESSFUL in 13s
65 actionable tasks: 56 executed, 9 up-to-date
brian:android brian$ ./gradlew bundleDevRelease

> Task :app:generateBundledResourcesHashDevRelease
37a65beeb35ec35b1b250d2f04b3deffbb0492b788570bee7dc06540d072f946

> Task :app:mergeDevReleaseAssets FAILED
ERROR:/Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/CodePushHash /Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/CodePushHash: Resource and asset merger: Duplicate resources

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDevReleaseAssets'.
> /Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/CodePushHash       /Users/brian/CodePushTest/android/app/build/generated/assets/createBundleDevReleaseJsAndAssets/CodePushHash: 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

BUILD FAILED in 2s
25 actionable tasks: 5 executed, 20 up-to-date
brian:android brian$ ./gradlew bundleDevRelease

> Task :app:generateBundledResourcesHashDevRelease
37a65beeb35ec35b1b250d2f04b3deffbb0492b788570bee7dc06540d072f946

BUILD SUCCESSFUL in 3s
65 actionable tasks: 11 executed, 54 up-to-date
brian:android brian$ ./gradlew bundleDevRelease

> Task :app:generateBundledResourcesHashDevRelease
37a65beeb35ec35b1b250d2f04b3deffbb0492b788570bee7dc06540d072f946

BUILD SUCCESSFUL in 3s
65 actionable tasks: 10 executed, 55 up-to-date
brian:android brian$ ./gradlew bundleDevRelease

> Task :app:generateBundledResourcesHashDevRelease
37a65beeb35ec35b1b250d2f04b3deffbb0492b788570bee7dc06540d072f946

BUILD SUCCESSFUL in 3s
65 actionable tasks: 10 executed, 55 up-to-date
brian:android brian$

Run ./gradlew bundleDevRelease again multiple times, and it won't ever fail again until you do another ./gradlew clean or have sufficient dependency changes to trigger whatever is causing the error.

@DmitriyKirakosyan
Copy link
Contributor

@brianlenz, thanks for confirming and providing more details.

@sanduluca, can you please review @brianlenz's case and see if it can be addressed in your PR?

@himanshuzeptonow
Copy link

when are we planning to merge this

@tai8292
Copy link

tai8292 commented Nov 9, 2023

I'm just applying this patch and it's working.

@velimir-jankovic
Copy link
Contributor

Let's wait for @sanduluca to address all issues before reviewing/merging.

@eliasg52
Copy link

eliasg52 commented Dec 5, 2023

I am currently working on the "react-native": "0.71.2" version of react native and I am having this same bug. It hasn't been solved yet, right?

@ThomasGuenard
Copy link

This patch doesn't work for me neither.

My fix for now is to run a script before each android build that remove the concerned hash file

I can't wait to get this fixed

@sanduluca
Copy link
Author

Ok, so I finally got some time for debuging. I will say first, I dont have a solution for now, but I will write what I noticed.

As @brianlenz says, the strangest thing is that we get the error after the second run after a ./gradlew clean, but next runs it won't fail

./gradlew clean
./gradlew bundleDevRelease
./gradlew bundleDevRelease # <-- this will fail
./gradlew bundleDevRelease # <-- this and next runs will succeed 
...

Analyzing the commit history and the codepush.gradle file found out this PR #2204. I tried to revert the changes it introduced and found out that I fixed the error (at least for me 😅).

If you wonder doesn't reverting this PR will reintroduce the issues it aimed to solve, the answer is : I don't know. But analyzing the issues activity (from that PR), it seems there are still developers that encounter problems

Here is the patch-package for react-native-code-push+8.1.0.patch. I would like to ask you to try it out and return with feedback.

diff --git a/node_modules/react-native-code-push/android/codepush.gradle b/node_modules/react-native-code-push/android/codepush.gradle
index a0f9d27..f234206 100644
--- a/node_modules/react-native-code-push/android/codepush.gradle
+++ b/node_modules/react-native-code-push/android/codepush.gradle
@@ -68,9 +68,6 @@ gradle.projectsEvaluated {
 
                 enabled !debuggableVariants.contains(variant.name) ?: targetName.toLowerCase().contains("release")
             }
-
-            runBefore("merge${targetName}Resources", generateBundledResourcesHash)
-            runBefore("merge${targetName}Assets", generateBundledResourcesHash)
         } else {
             def jsBundleDirConfigName = "jsBundleDir${targetName}"
             jsBundleDir = elvisFile(config."$jsBundleDirConfigName").get() ?:

@brianlenz
Copy link

@sanduluca sorry for the delay! I finally got around to testing this. The issue still exists (unsurprisingly) in 8.2.1. I've applied your patch for 8.2.1, as follows:

diff --git a/android/codepush.gradle b/android/codepush.gradle
index 4faf49f2f8233097fbb90b4e9dbccff6c98ef0f4..eef007baa78c5487c939e9b820d4509dda815dd6 100644
--- a/android/codepush.gradle
+++ b/android/codepush.gradle
@@ -72,9 +72,6 @@ gradle.projectsEvaluated {
 
                 enabled !debuggableVariants.contains(variant.name) ?: targetName.toLowerCase().contains("release")
             }
-
-            runBefore("merge${targetName}Resources", generateBundledResourcesHash)
-            runBefore("merge${targetName}Assets", generateBundledResourcesHash)
         } else {
             def jsBundleDirConfigName = "jsBundleDir${targetName}"
             jsBundleDir = elvisFile(config."$jsBundleDirConfigName").get() ?:

This change does seem to have affected the behavior, but it doesn't look like it fully fixes it. Now, running the bundle gradlew commands doesn't reproduce it anymore:

./gradlew clean
./gradlew bundleDevDebug
./gradlew bundleDevDebug # <-- this breaks in 8.2.1, but NOT with the above patch
./gradlew bundleDevDebug # <-- still works. run multiple more times without issue.

The scenario that doesn't work for me still is when running react-native run-android 🤔

./gradlew clean
yarn react-native run-android --mode=DevDebug --appIdSuffix dev
yarn react-native run-android --mode=DevDebug --appIdSuffix dev # <-- this fails still!

Here's the error when run a second time in that way:

ERROR: /Users/brian/TTW/ttw/app/android/app/build/generated/assets/createBundleDevDebugJsAndAssets/CodePushHash /Users/brian/TTW/ttw/app/android/app/build/generated/assets/createBundleDevDebugJsAndAssets/CodePushHash: Resource and asset merger: Duplicate resources

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDevDebugAssets'.
> /Users/brian/TTW/ttw/app/android/app/build/generated/assets/createBundleDevDebugJsAndAssets/CodePushHash	/Users/brian/TTW/ttw/app/android/app/build/generated/assets/createBundleDevDebugJsAndAssets/CodePushHash: 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.

BUILD FAILED in 12s
error Failed to install the app. Command failed with exit code 1: ./gradlew app:installDevDebug -PreactNativeDevServerPort=8081
ERROR: /Users/brian/TTW/ttw/app/android/app/build/generated/assets/createBundleDevDebugJsAndAssets/CodePushHash /Users/brian/TTW/ttw/app/android/app/build/generated/assets/createBundleDevDebugJsAndAssets/CodePushHash: Resource and asset merger: Duplicate resources FAILURE: Build failed with an exception. * What went wrong:
Execution failed for task ':app:mergeDevDebugAssets'.
> /Users/brian/TTW/ttw/app/android/app/build/generated/assets/createBundleDevDebugJsAndAssets/CodePushHash	/Users/brian/TTW/ttw/app/android/app/build/generated/assets/createBundleDevDebugJsAndAssets/CodePushHash: 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. BUILD FAILED in 12s.
info Run CLI with --verbose flag for more details.

The difference is that it's running installDevDebug instead of bundleDevDebug. So, here's a simpler way to reproduce the issue:

./gradlew clean
./gradlew installDevDebug
./gradlew installDevDebug # <-- fails!
./gradlew installDevDebug # <-- succeeds!

So, it seems that reverting #2204 doesn't solve the problem; it just modifies the behavior a bit.

@garmoshka-mo
Copy link

garmoshka-mo commented Mar 1, 2024

for hotfix: full cleaning will increase next build time significanlty.
instead, it's better to only clean codepush bug files before build, in my case they are here:
rm -rf android/app/build/ASSETS

@viLeeKorn
Copy link

Does this issue affect not debug build?

@ziga-hvalec
Copy link

Any update on this?

@DmitriyKirakosyan
Copy link
Contributor

#2697 is merged. So closing this PR.
@bestander thank you for the contribution!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.