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

Unable to get the hash of the binary's bundled resources - "codepush.gradle" may have not been added to the build definition. #1981

Closed
SeveHo opened this issue Nov 13, 2020 · 22 comments

Comments

@SeveHo
Copy link

SeveHo commented Nov 13, 2020

Hi my RN Application is crashing for release builds with the error: [CodePush] Unable to get the hash of the binary's bundled resources - "codepush.gradle" may have not been added to the build definition. Below you can find my description. Please help!!

Steps to Reproduce

  1. Follow instructions for RN > 60 https://docs.microsoft.com/en-us/appcenter/distribution/codepush/rn-get-started#android-setup
  2. Follow instructions for https://docs.microsoft.com/en-us/appcenter/distribution/codepush/rn-deployment#android
  3. Run gradlew bundleRelease
  4. Upload .aab file to the internal test track on Google Play Store
  5. Upload bundle to CodePush: appcenter codepush release-react -b app.bundle -a <REDACTED>/<REDACTED> -d Staging

Resulting Setup

settings.gradle

include ':app', ':react-native-code-push'
project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')

app/build.gradle

...
project.ext.react = [
    // custom for dep react-native-jitsi-meet
    bundleAssetName: "app.bundle",
    enableHermes: true,
]

apply from: "../../node_modules/react-native/react.gradle"
// Custom for CodePush
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"
// Added by sentry cli
apply from: "../../node_modules/@sentry/react-native/sentry.gradle"
...
android {
    buildTypes {
        debug {
        ...
        }
        release {
            signingConfig signingConfigs.release
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"

            // Important Detox-specific additions to pro-guard
            proguardFile "${rootProject.projectDir}/../node_modules/detox/android/detox/proguard-rules-app.pro"

            // Custom for CodePush
            resValue "string", "CodePushDeploymentKey", '"<Redacted>"'
        }

        // The naming convention for releaseStaging is significant due to this line https://github.com/facebook/react-native/blob/e083f9a139b3f8c5552528f8f8018529ef3193b9/react.gradle#L79
        releaseStaging {
            signingConfig signingConfigs.release
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"

            // Important Detox-specific additions to pro-guard
            proguardFile "${rootProject.projectDir}/../node_modules/detox/android/detox/proguard-rules-app.pro"

            // Custom for CodePush
            resValue "string", "CodePushDeploymentKey", '"<Redacted>"'
            // Note: It is a good idea to provide matchingFallbacks for the new buildType you create to prevent build issues
            // Add the following line if not already there
            matchingFallbacks = ['release']
        }
    }
}

MainApplication.java

...

// custom for deb react-native-jitsi-meet
import androidx.annotation.Nullable;

// custom for CodePush
import com.microsoft.codepush.react.CodePush;

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost =
      new ReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
          return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
          return packages;
        }

        // Custom for CodePush
        // Override the getJSBundleFile method in order to let
        // the CodePush runtime determine where to get the JS
        // bundle location from on each app start. Specify bundle name due to change for rn-jitsi-meet
        @Override
        protected String getJSBundleFile() {
           return CodePush.getJSBundleFile("app.bundle");
        }

        @Override
        protected String getJSMainModuleName() {
          return "index";
        }

        // custom for deb react-native-jitsi-meet
        @Override
        protected @Nullable String getBundleAssetName() {
          return "app.bundle";
        }
      };

...
  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
    initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
  }
}
...

Expected Behavior

The app should load the bundle from CodePush and start properly like when testet with npx react-native run-android --variant release

Actual Behavior

The app crashes with the following error

[CodePush] Loading JS bundle from "assets://app.bundle"
[CodePush] Unable to get the hash of the binary's bundled resources - "codepush.gradle" may have not been added to the build definition.
AndroidRuntime: FATAL EXCEPTION: mqt_js

Environment

  • react-native-code-push version: 6.4.0
  • react-native version: 63.3
  • iOS/Android/Windows version: Android 10
  • Does this reproduce on a debug build or release build? Release build bundle with bundleRelease command
  • Does this reproduce on a simulator, or only on a physical device? Only on a physical device
@fabricio0915
Copy link

i have the same problem, when i use bundleRelease to generate an Android App Bundle i do have this issue, can anyone help us?

@fabricio0915
Copy link

In my tests the file CodePushHash was generated but, has not been copied to assets folder in the bundle

@jessamp
Copy link

jessamp commented Nov 20, 2020

I wonder if this could be related to this problem here: #1977. I was having problems when setting bundleInRelease: false. One workaround I found was manually creating these directories: android/app/build/generated/res/react/release android/app/build/generated/assets/react/release. During the build, the CodePushHash was then placed in the assets directory.

@fabricio0915
Copy link

@jessamp where you put the script to make this folders?

@fabricio0915
Copy link

in my case i use product Flavors this folder structure maybe it`s not supported by code push

@jessamp
Copy link

jessamp commented Nov 20, 2020

I set it up in my build pipelines. It's in a yaml file. If you don't use pipelines, you could probably add a post-script in the npm install step.

I also use product flavors. So in that case it would be: android/app/build/generated/res/react/<flavor name>/release

@fabricio0915
Copy link

image

Even creating the folder using the flavor the CodePushHash its not copied to my assets folder, i really got no ideia to fix it

@fabricio0915
Copy link

@alexandergoncharov could you help me with this issue?

@DemonWu
Copy link

DemonWu commented Jan 8, 2021

我试了下改codepush.gradle文件后可以一直强制复制CodePushHash后解决了这个问题
react-native-code-push/android/codepush.gradle文件的64行除添加

                config."bundleIn${variant.buildType.name.capitalize()}" ?:
                targetName.toLowerCase().contains("release")
// 以下位置添加进去
>>>         doLast {
                    copy {
                        into("${buildDir}/intermediates")
                        into ("assets/${targetPath}") {
                            from(jsBundleDir)
                        }

                        // Workaround for Android Gradle Plugin 3.2+ new asset directory
                        into ("merged_assets/${variant.name}/merge${targetName}Assets/out") {
                            from(jsBundleDir)
                        }

                        // Workaround for Android Gradle Plugin 3.4+ new asset directory
                        into ("merged_assets/${variant.name}/out") {
                            from(jsBundleDir)
                        }
                    }
                }

可有试一下

jiwansvmx added a commit to jiwansvmx/react-native-code-push that referenced this issue Jan 29, 2021
I ran into microsoft#1981 and upon digging in I noticed that that generateBundledResourcesHash task does not run when org.gradle.configureondemand is set to true. This diff uses  afterEvaluate instead of  gradle.projectsEvaluated to ensure that the generateBundledResourcesHash is run.
@hungtrn75
Copy link

pull request
I followed this and it worked

@rafalzawadzki
Copy link

rafalzawadzki commented Mar 23, 2021

What I found trying to solve this:

  1. I see this problem happening on Android Gradle 4.1.0+ and Gradle wrapper 6.5+. Downgrading to versions 4.0.1 and 6.2 respectively solves it.
  2. Tested CodePush versions 6.3.0 through to 7.0.0, all the same
  3. Clean builds always result in the issue (after ./gradlew clean), but consecutive builds are fine. This is particularly problematic in CI environments.
  4. The solution given by @hungtrn75 does not work (neither changing codepush.gradle or the org.gradle.configureondemand flag)
  5. Suggested change from @DemonWu did not change anything for me

Any ideas? :)

@snkhan120
Copy link

pull request
I followed this and it worked

Is it working???Anyone

@hgbao
Copy link

hgbao commented Jun 16, 2021

+1 on this

@hungtrn75
Copy link

pull request
I followed this and it worked

Is it working???Anyone

I confirm it worked. I must create a patch package with codepush to run post install.

@owjsub
Copy link
Contributor

owjsub commented Aug 7, 2021

I'm seeing the same thing as @rafalzawadzki. I tried to use patch-package to modify react-native-code-push/android/codepush.gradle to run the 'hash' task between the react-native bundle & copy tasks, but ran into an issue where bundle + hash ran, but the copy task was skipped.

// in react-native-code-push/android/codepush.gradle:
Task bundleTask = tasks.findByPath("bundle${targetName}JsAndAssets")
generateBundledResourcesHash.dependsOn(bundleTask)

Task copyTask = tasks.findByPath("copy${targetName}BundledJs")
copyTask.dependsOn(generateBundledResourcesHash)

Any ideas?

@rafalzawadzki
Copy link

After upgrading our project to RN >= 0.64 the issue resolved itself.

Check out this PR: facebook/react-native@53f5500

@owjsub
Copy link
Contributor

owjsub commented Aug 7, 2021

@rafalzawadzki I'm still reproducing the issue in a fresh build after upgrading to RN 0.64. I'm using Gradle 7.1 Alpha 6, so that's probably causing the issue with task ordering between react-native and react-native-code-push gradle files. In testing a fresh build, a ./gradlew clean did not actually put it into a clean state, I had to re-clone the project in order to reproduce the issue locally that I was seeing on our CI pipeline.

@owjsub
Copy link
Contributor

owjsub commented Aug 9, 2021

Update:

  • I was able to reproduce the crash even after removing react-native-code-push from the project.
  • I discovered that downgrading from Gradle 7.1 -> 7.0 resolved the issue for release builds.

Here’s a working environment for release builds (should work for both APK and AAB):

compileSdkVersion 31
targetSdkVersion 31
buildToolsVersion '31.0.0'
Gradle 7.0.2
AGP 7.0.0
react-native: 0.63.4 OR 0.64.2
react-native-code-push: 6.2.0

Just to be on the safe side with Gradle 7+, I have opted to disable certain build optimizations:

// in android/gradle.properties:
# When configured, Gradle will run in parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=false

# Enables configuration on demand, where Gradle will attempt to configure only necessary projects
# This option should only be used with decoupled projects.
# https://docs.gradle.org/7.0/userguide/multi_project_configuration_and_execution.html#sec:configuration_on_demand
org.gradle.configureondemand=false

If your @react-native-community/cli-platform-android is below 6.0, you will need this patch for Gradle 7. To avoid forking that project, use patch-package to apply this change:

diff --git a/node_modules/@react-native-community/cli-platform-android/native_modules.gradle b/node_modules/@react-native-community/cli-platform-android/native_modules.gradle
index 11fe3e6..a279d24 100644
--- a/node_modules/@react-native-community/cli-platform-android/native_modules.gradle
+++ b/node_modules/@react-native-community/cli-platform-android/native_modules.gradle
@@ -118,7 +118,9 @@ class ReactNativeModules {
    * @param generatedFileContentsTemplate
    */
   void generatePackagesFile(File outputDir, String generatedFileName, String generatedFileContentsTemplate) {
-    ArrayList<HashMap<String, String>>[] packages = this.reactNativeModules
+    // Patch for Gradle 7
+    // ref: https://github.com/react-native-community/cli/pull/1396/files
+    ArrayList<HashMap<String, String>> packages = this.reactNativeModules
     String packageName = this.packageName

For anyone that has trouble reproducing the crash with release builds locally, here’s some commands that I found helpful to quickly put a project into a ‘clean’ state without resorting to cloning a fresh copy of the project:
./gradlew clean && ./gradlew --stop && rm -rf .gradle && rm -rf $HOME/.gradle/caches

@ghost ghost added the stale label Dec 13, 2021
@ghost
Copy link

ghost commented Dec 13, 2021

This issue has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs within 15 days of this comment.

@ghost
Copy link

ghost commented Dec 28, 2021

This issue will now be closed because it hasn't had any activity for 15 days after stale. Please feel free to open a new issue if you still have a question/issue or suggestion.

@ghost ghost closed this as completed Dec 28, 2021
@alexnekrashevich
Copy link
Contributor

I hope that this explains everything and helps to solve the issue: #838 (comment)

@chrismcleod
Copy link

#1981 (comment) worked for me

bishalfan added a commit to ds-fancode/react-native-code-push that referenced this issue Oct 17, 2022
bishalfan added a commit to ds-fancode/react-native-code-push that referenced this issue Oct 17, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests