question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react.gradle file not bundling Js code

See original GitHub issue

i uncommented app.gradle code to following

/**
 * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
 * and bundleReleaseJsAndAssets).
 * These basically call `react-native bundle` with the correct arguments during the Android build
 * cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
 * bundle directly from the development server. Below you can see all the possible configurations
 * and their defaults. If you decide to add a configuration block, make sure to add it before the
 * `apply from: "../../node_modules/react-native/react.gradle"` line.
 */
  project.ext.react = [
    // the name of the generated asset file containing your JS bundle
    bundleAssetName: "index.android.bundle",

    // the entry file for bundle generation
    entryFile: "index.android.js",

    // whether to bundle JS and assets in debug mode
    bundleInDebug: true,

    // whether to bundle JS and assets in release mode
    bundleInRelease: true,

    // whether to bundle JS and assets in another build variant (if configured).
    // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
    // The configuration property can be in the following formats
    //         'bundleIn${productFlavor}${buildType}'
    //         'bundleIn${buildType}'
    // bundleInFreeDebug: true,
    // bundleInPaidRelease: true,
    // bundleInBeta: true,

    // the root of your project, i.e. where "package.json" lives
    root: "../../",

    // where to put the JS bundle asset in debug mode
    jsBundleDirDebug: "$buildDir/intermediates/assets/debug",

    // where to put the JS bundle asset in release mode
    jsBundleDirRelease: "$buildDir/intermediates/assets/release",

    // where to put drawable resources / React Native assets, e.g. the ones you use via
    // require('./image.png')), in debug mode
    resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",

    // where to put drawable resources / React Native assets, e.g. the ones you use via
    // require('./image.png')), in release mode
    resourcesDirRelease: "$buildDir/intermediates/res/merged/release",

    // by default the gradle tasks are skipped if none of the JS files or assets change; this means
    // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
    // date; if you have any other folders that you want to ignore for performance reasons (gradle
    // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
    // for example, you might want to remove it from here.
    inputExcludes: ["android/**", "ios/**"],

    // override which node gets called and with what additional arguments
    nodeExecutableAndArgs: ["node"],

    // supply additional arguments to the packager
    extraPackagerArgs: []
  ]


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

but no js is bundled in apk file upon building in both debug and release mode and no js files are placed in assets folder.

and following exception is thrown

08-27 03:52:08.736 9630-9630/com.simpfuel E/unknown:React: Exception in native call from JS
                                                           java.lang.RuntimeException: Could not get BatchedBridge, make sure your bundle is packaged correctly
                                                               at com.facebook.react.cxxbridge.CatalystInstanceImpl.loadScriptFromAssets(Native Method)
                                                               at com.facebook.react.cxxbridge.JSBundleLoader$1.loadScript(JSBundleLoader.java:37)
                                                               at com.facebook.react.cxxbridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:177)
                                                               at com.facebook.react.XReactInstanceManagerImpl$4.call(XReactInstanceManagerImpl.java:889)
                                                               at com.facebook.react.XReactInstanceManagerImpl$4.call(XReactInstanceManagerImpl.java:882)
                                                               at com.facebook.react.bridge.queue.MessageQueueThreadImpl$1.run(MessageQueueThreadImpl.java:74)
                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                               at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
                                                               at android.os.Looper.loop(Looper.java:148)
                                                               at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196)
                                                               at java.lang.Thread.run(Thread.java:818)

device-2016-08-27-125224

i am using react-native version 0.32.0 on windows 10 and for android

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
Oblongmanacommented, May 27, 2017

So I think I figured it all out, I definitely now have builds reliably running properly when hitting the “Run” button.

There are two issues (on OSX):

  1. Android Studio skipping the bundle[buildType]JsAndAssets step for release-type builds
  2. Android Studio not finding node

To Solve:

1) Bundling being skipped

Uncheck “Configure on demand” as discussed above

Android Studio > 
Preferences > 
Build, Execution, Deployment > 
Compiler > 
[Untick] Configure on demand

2) Android Studio not finding node

Even after fixing bundling being skipped, the build still fails with A problem occurred starting process 'command 'node''. As noted by @andpor:

The apps [on OSX] do not execute any of the user shell profiles when they are started so if there is some nvm or node setup being done in files such as .bash_profile, they will not get picked app by Studio which will barf at executive react-native shell commands which are node based.

There are workarounds for trying to get OSX apps to use your PATH, but basically - they’re all terrible, relatively difficult, require restarts, and generally break between OSX versions, and for other reasons.

So the solution - when gradle runs, it checks gradle.properties as follows (documented at https://docs.gradle.org/current/userguide/build_environment.html):

  • from gradle.properties in project build dir.
  • from gradle.properties in gradle user home.

What we want to do is tell gradle directly what the node path is. We don’t want to do this in the project’s gradle.properties - you’ll commit that to version control, and the node path may vary between developers.

Instead, you set it in the gradle user home. Users can override this, but generally it’s USER_HOME/.gradle, so on OSX, generally ~/.gradle. So (and this is the bit you want to add to your project README so your developers know how to configure it), we can configure our node path for gradle like this:

$ mkdir -p ~/.gradle    # make the gradle home dir, if it doesn't exist already
$ touch ~/.gradle/gradle.properties     # create a gradle.properties, if it doesn't exist already

Now open ~/.gradle/gradle.properties in your preferred editor, and add the line: NODE_PATH=/usr/local/bin/node Or wherever your node install is located of course - check this with $ which node

Open android/app/build.gradle There should be a line that says apply from: "../../node_modules/react-native/react.gradle" Just before that line, add the following (or add to your existing project.ext.react if you have one):

project.ext.react = [
    nodeExecutableAndArgs: hasProperty('NODE_PATH')?[NODE_PATH]:null
]

The nodeExecutableAndArgs is something you can configure for the react gradle file, which makes it use the path and args you supply, instead of looking in the environment (as discussed above, the environment won’t work). This line checks whether the NODE_PATH has been configured, and uses it if supplied, and if not, sets the property to null (which causes the react gradle file to fall back to using the environment)

9reactions
corbtcommented, Oct 11, 2016

I was able to get this working in Android Studio by going to Preferences > Build, Execution, Deployment > Build Tools > Compiler and unchecking “Configure on demand”.

It was always working for me when running ./gradlew assembleRelease from the command line, which is why I suspected an Android Studio config issue. Anyway, those are two things to try.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bundling React-native android release not picking up code ...
gradlew assembleRelease is not bundling the code changes unless I run a script : ... react.gradle is applied in app/build.gradle file
Read more >
Bundling React Native during Android release builds
Looking into the react-native directory reveals a Gradle file that defines a bundling task. This is the react.gradle file that is mentioned in...
Read more >
RAM Bundles and Inline Requires - React Native
Before react-native can execute JS code, that code must be loaded into memory and parsed. With a standard bundle if you load a...
Read more >
Bundling React Native during Android release builds
Looking into the react-native directory reveals a Gradle file that defines a bundling task. This is the react.gradle file that is mentioned in ......
Read more >
Exploring React Native's new architecture - LogRocket Blog
cd .. yarn add react-native-gradle-plugin code android/settings.gradle ... a React Native application is run, the JavaScript code is bundled ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found