JaneaSystems / nodejs-mobile

Full-fledged Node.js on Android and iOS
https://code.janeasystems.com/nodejs-mobile
Other
2.57k stars 182 forks source link

Not working on react-native (in android emu.) throws bad_cast error #297

Closed emirhankolver closed 3 years ago

emirhankolver commented 3 years ago

I did everything one by one in Official NodeJS-Mobile Guide but when I execute the nodejs.start('main.js'); code my app closes without any react-native error or warning. Only throws few lines from logcat. My React-Native version: 0.63.4 My Not working project on Github

My App.js file

import React from "react";
import { Button, Text, ToastAndroid, View } from "react-native";
import nodejs from 'nodejs-mobile-react-native';

class App extends React.Component {

  startTheServer() {
    ToastAndroid.show('Started!',ToastAndroid.SHORT);
    nodejs.start('main.js');
    nodejs.channel.addListener(
      'message',
      (msg) => {
        alert('From node: ' + msg);
      },
      this
    );
  }

  render() {
    return (
      <View
        style={{
          flex: 1,
          justifyContent: "center",
          alignItems: "center",
        }}>

        <Text
          style={{
            fontWeight: "bold",
            fontSize: 32,
          }}>Node.JS Test</Text>

        <Button
          title={"Start NodeJS Server"}
          onPress={() => {this.startTheServer();}}/>

      </View>
    );
  }

}

export default App;

Logcat output by searching NODEJS-RN (logcat from the time that I executed the nodejs.start(main.js) line):

2020-12-27 14:01:23.263 16549-16609/com.untitled D/NODEJS-RN: Node assets copy using pre-built lists
2020-12-27 14:01:23.270 16549-16609/com.untitled D/NODEJS-RN: File not found: nodejs-native-assets-x86/dir.list
2020-12-27 14:01:23.270 16549-16609/com.untitled D/NODEJS-RN: File not found: nodejs-native-assets-x86/file.list
2020-12-27 14:01:23.271 16549-16609/com.untitled V/NODEJS-RN: No assets to copy from nodejs-native-assets-x86
2020-12-27 14:01:23.289 16549-16609/com.untitled D/NODEJS-RN: Node assets copy completed successfully
2020-12-27 14:01:29.142 16549-16648/com.untitled E/NODEJS-MOBILE: terminating with uncaught exception of type std::bad_cast: std::bad_cast
    /usr/local/google/buildbot/src/android/ndk-release-r20/external/libcxx/../../external/libcxxabi/src/abort_message.cpp:73: abort_message: assertion "terminating with uncaught exception of type std::bad_cast: std::bad_cast" failed

Full Logcat (logcat from the time that I executed the nodejs.start(main.js) line):

2020-12-27 14:01:22.630 1963-1988/system_process I/ActivityTaskManager: Displayed com.untitled/.MainActivity: +1s57ms
2020-12-27 14:01:22.678 16549-16591/com.untitled D/EGL_emulation: eglMakeCurrent: 0xebb9bd40: ver 3 1 (tinfo 0xdcfff380)
2020-12-27 14:01:22.721 16549-16591/com.untitled I/chatty: uid=10152(com.untitled) RenderThread identical 1 line
2020-12-27 14:01:22.739 16549-16591/com.untitled D/EGL_emulation: eglMakeCurrent: 0xebb9bd40: ver 3 1 (tinfo 0xdcfff380)
2020-12-27 14:01:23.208 16549-16591/com.untitled D/EGL_emulation: eglMakeCurrent: 0xebb9bd40: ver 3 1 (tinfo 0xdcfff380)
2020-12-27 14:01:23.215 16549-16608/com.untitled D/SoLoader: About to load: libreactnativejni.so
2020-12-27 14:01:23.216 16549-16608/com.untitled D/SoLoader: libreactnativejni.so not found on /data/data/com.untitled/lib-main
2020-12-27 14:01:23.216 16549-16608/com.untitled D/SoLoader: libreactnativejni.so found on /data/app/com.untitled-HptDfjKjdL_rwKzJI9WsdQ==/lib/x86
2020-12-27 14:01:23.216 16549-16608/com.untitled D/SoLoader: Not resolving dependencies for libreactnativejni.so
2020-12-27 14:01:23.218 16549-16608/com.untitled D/SoLoader: Loaded: libreactnativejni.so
2020-12-27 14:01:23.263 16549-16609/com.untitled D/NODEJS-RN: Node assets copy using pre-built lists
2020-12-27 14:01:23.270 16549-16609/com.untitled D/NODEJS-RN: File not found: nodejs-native-assets-x86/dir.list
2020-12-27 14:01:23.270 16549-16609/com.untitled D/NODEJS-RN: File not found: nodejs-native-assets-x86/file.list
2020-12-27 14:01:23.271 16549-16609/com.untitled V/NODEJS-RN: No assets to copy from nodejs-native-assets-x86
2020-12-27 14:01:23.284 16549-16608/com.untitled E/unknown:ReactNative: ReactInstanceManager.createReactContext: mJSIModulePackage null
2020-12-27 14:01:23.289 16549-16609/com.untitled D/NODEJS-RN: Node assets copy completed successfully
2020-12-27 14:01:23.289 16549-16610/com.untitled E/ReactNativeJNI: logMarker CREATE_REACT_CONTEXT_END
2020-12-27 14:01:23.292 16549-16610/com.untitled E/ReactNativeJNI: logMarker RUN_JS_BUNDLE_START
2020-12-27 14:01:23.295 16549-16611/com.untitled E/unknown:ReactNative: ReactInstanceManager.attachRootViewToInstance()
2020-12-27 14:01:23.296 16549-16549/com.untitled W/unknown:ReactNative: Packager connection already open, nooping.
2020-12-27 14:01:23.538 16549-16611/com.untitled D/SoLoader: About to load: libyoga.so
2020-12-27 14:01:23.539 16549-16611/com.untitled D/SoLoader: libyoga.so not found on /data/data/com.untitled/lib-main
2020-12-27 14:01:23.539 16549-16611/com.untitled D/SoLoader: libyoga.so found on /data/app/com.untitled-HptDfjKjdL_rwKzJI9WsdQ==/lib/x86
2020-12-27 14:01:23.539 16549-16611/com.untitled D/SoLoader: Not resolving dependencies for libyoga.so
2020-12-27 14:01:23.555 16549-16611/com.untitled D/SoLoader: Loaded: libyoga.so
2020-12-27 14:01:23.575 16549-16611/com.untitled E/unknown:ReactRootView: runApplication: call AppRegistry.runApplication
2020-12-27 14:01:23.612 16549-16549/com.untitled E/unknown:ReactNative: ReactInstanceManager.attachRootViewToInstance()
2020-12-27 14:01:23.614 16549-16549/com.untitled E/unknown:ReactRootView: runApplication: call AppRegistry.runApplication
2020-12-27 14:01:23.697 16549-16611/com.untitled I/flipper: flipper: FlipperClient::addPlugin Fresco
2020-12-27 14:01:23.710 16549-16610/com.untitled D/SoLoader: About to load: libreactnativeblob.so
2020-12-27 14:01:23.710 16549-16610/com.untitled D/SoLoader: libreactnativeblob.so not found on /data/data/com.untitled/lib-main
2020-12-27 14:01:23.711 16549-16610/com.untitled D/SoLoader: libreactnativeblob.so found on /data/app/com.untitled-HptDfjKjdL_rwKzJI9WsdQ==/lib/x86
2020-12-27 14:01:23.711 16549-16610/com.untitled D/SoLoader: Not resolving dependencies for libreactnativeblob.so
2020-12-27 14:01:23.715 16549-16610/com.untitled D/SoLoader: Loaded: libreactnativeblob.so
2020-12-27 14:01:23.744 16549-16560/com.untitled I/com.untitled: Background young concurrent copying GC freed 38977(8617KB) AllocSpace objects, 14(360KB) LOS objects, 72% free, 2317KB/8461KB, paused 7.380ms total 80.724ms
2020-12-27 14:01:23.758 16549-16611/com.untitled I/WebViewFactory: Loading com.google.android.webview version 74.0.3729.185 (code 373018518)
2020-12-27 14:01:23.763 16549-16611/com.untitled I/com.untitled: The ClassLoaderContext is a special shared library.
2020-12-27 14:01:23.781 16549-16611/com.untitled I/com.untitled: The ClassLoaderContext is a special shared library.
2020-12-27 14:01:23.862 16549-16611/com.untitled I/cr_LibraryLoader: Time to load native libraries: 12 ms (timestamps 9658-9670)
2020-12-27 14:01:23.880 16549-16611/com.untitled I/chromium: [INFO:library_loader_hooks.cc(50)] Chromium logging enabled: level = 0, default verbosity = 0
2020-12-27 14:01:23.881 16549-16611/com.untitled I/cr_LibraryLoader: Expected native library version number "74.0.3729.185", actual native library version number "74.0.3729.185"
2020-12-27 14:01:24.087 16549-16610/com.untitled E/ReactNativeJNI: logMarker RUN_JS_BUNDLE_END
2020-12-27 14:01:24.117 16549-16610/com.untitled I/ReactNativeJS: Running "untitled" with {"rootTag":1}
2020-12-27 14:01:24.169 16549-16549/com.untitled W/com.untitled: Accessing hidden field Landroid/view/View;->mAccessibilityDelegate:Landroid/view/View$AccessibilityDelegate; (greylist, reflection, allowed)
2020-12-27 14:01:24.218 16549-16591/com.untitled D/EGL_emulation: eglMakeCurrent: 0xebb9bd40: ver 3 1 (tinfo 0xdcfff380)
2020-12-27 14:01:29.142 16549-16646/com.untitled A/libc: /usr/local/google/buildbot/src/android/ndk-release-r20/external/libcxx/../../external/libcxxabi/src/abort_message.cpp:73: abort_message: assertion "terminating with uncaught exception of type std::bad_cast: std::bad_cast" failed
2020-12-27 14:01:29.142 16549-16646/com.untitled A/libc: Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 16646 (Thread-5), pid 16549 (com.untitled)
2020-12-27 14:01:29.142 16549-16648/com.untitled E/NODEJS-MOBILE: terminating with uncaught exception of type std::bad_cast: std::bad_cast
    /usr/local/google/buildbot/src/android/ndk-release-r20/external/libcxx/../../external/libcxxabi/src/abort_message.cpp:73: abort_message: assertion "terminating with uncaught exception of type std::bad_cast: std::bad_cast" failed
2020-12-27 14:01:29.126 16549-16549/com.untitled W/Thread-5: type=1400 audit(0.0:493): avc: denied { ioctl } for path="/sys/kernel/debug/tracing/trace_marker" dev="tracefs" ino=29 ioctlcmd=0x5451 scontext=u:r:untrusted_app:s0:c152,c256,c512,c768 tcontext=u:object_r:debugfs_trace_marker:s0 tclass=file permissive=0 app=com.untitled
2020-12-27 14:01:29.126 16549-16549/com.untitled W/Thread-5: type=1400 audit(0.0:494): avc: denied { ioctl } for path="/apex/com.android.runtime/javalib/core-oj.jar" dev="dm-2" ino=247 ioctlcmd=0x5451 scontext=u:r:untrusted_app:s0:c152,c256,c512,c768 tcontext=u:object_r:system_file:s0 tclass=file permissive=0 app=com.untitled
2020-12-27 14:01:29.241 16656-16656/? A/DEBUG: pid: 16549, tid: 16646, name: Thread-5  >>> com.untitled <<<
2020-12-27 14:01:29.308 16656-16656/? A/DEBUG:       #04 pc 000bad94  /data/app/com.untitled-HptDfjKjdL_rwKzJI9WsdQ==/lib/x86/libfbjni.so (BuildId: 3546c1351f83366cb88b3802c4c63ee907e2db98)
2020-12-27 14:01:29.308 16656-16656/? A/DEBUG:       #05 pc 0009fc2e  /data/app/com.untitled-HptDfjKjdL_rwKzJI9WsdQ==/lib/x86/libfbjni.so (BuildId: 3546c1351f83366cb88b3802c4c63ee907e2db98)
2020-12-27 14:01:29.308 16656-16656/? A/DEBUG:       #06 pc 000b78e9  /data/app/com.untitled-HptDfjKjdL_rwKzJI9WsdQ==/lib/x86/libfbjni.so (BuildId: 3546c1351f83366cb88b3802c4c63ee907e2db98)
2020-12-27 14:01:29.308 16656-16656/? A/DEBUG:       #07 pc 000b6e1e  /data/app/com.untitled-HptDfjKjdL_rwKzJI9WsdQ==/lib/x86/libfbjni.so (BuildId: 3546c1351f83366cb88b3802c4c63ee907e2db98)
2020-12-27 14:01:29.308 16656-16656/? A/DEBUG:       #08 pc 000b6d73  /data/app/com.untitled-HptDfjKjdL_rwKzJI9WsdQ==/lib/x86/libfbjni.so (__cxa_throw+115) (BuildId: 3546c1351f83366cb88b3802c4c63ee907e2db98)
2020-12-27 14:01:29.308 16656-16656/? A/DEBUG:       #09 pc 0005c7a4  /data/app/com.untitled-HptDfjKjdL_rwKzJI9WsdQ==/lib/x86/libc++_shared.so (std::__ndk1::locale::use_facet(std::__ndk1::locale::id&) const+212) (BuildId: 8771d1ad0fbd042529aa00a1937ddbc7b237a107)
2020-12-27 14:01:29.308 16656-16656/? A/DEBUG:       #10 pc 006da709  /data/app/com.untitled-HptDfjKjdL_rwKzJI9WsdQ==/lib/x86/libnode.so (_ZNSt6__ndk124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_j+153)
2020-12-27 14:01:29.336 16549-16591/com.untitled D/EGL_emulation: eglMakeCurrent: 0xebb9bd40: ver 3 1 (tinfo 0xdcfff380)
2020-12-27 14:01:29.344 16549-16591/com.untitled D/EGL_emulation: eglMakeCurrent: 0xebb9bd40: ver 3 1 (tinfo 0xdcfff380)
2020-12-27 14:01:29.710 1963-16663/system_process W/ActivityTaskManager:   Force finishing activity com.untitled/.MainActivity
2020-12-27 14:01:29.838 1963-2052/system_process W/InputDispatcher: channel 'a9e2379 com.untitled/com.untitled.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2020-12-27 14:01:29.839 1963-2052/system_process E/InputDispatcher: channel 'a9e2379 com.untitled/com.untitled.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2020-12-27 14:01:29.871 1963-2033/system_process I/WindowManager: WIN DEATH: Window{a9e2379 u0 com.untitled/com.untitled.MainActivity}
2020-12-27 14:01:29.871 1963-2033/system_process W/InputDispatcher: Attempted to unregister already unregistered input channel 'a9e2379 com.untitled/com.untitled.MainActivity (server)'
2020-12-27 14:01:29.873 1963-2748/system_process I/ActivityManager: Process com.untitled (pid 16549) has died: vis+99 TOP 
2020-12-27 14:01:31.238 1963-1963/system_process W/NotificationService: Object died trying to hide notification android.app.ITransientNotification$Stub$Proxy@f2745d4 in package com.untitled

Metro.Config File:

const blacklist = require('metro-config/src/defaults/blacklist');

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
  resolver: {
    blacklistRE: blacklist([
      /\/nodejs-assets\/.*/,
      /\/android\/.*/,
      /\/ios\/.*/
    ])
  },
};

project\nodejs-assets\nodejs-project\main.js File:

// Rename this sample file to main.js to use on your project.
// The main.js file will be overwritten in updates/reinstalls.

const rn_bridge = require("rn-bridge");

// Echo every message received from react-native.
rn_bridge.channel.on('message', (msg) => {
  rn_bridge.channel.send(msg);
} );

// Inform react-native node is initialized.
rn_bridge.channel.send("Node was initialized.");

project\nodejs-assets\nodejs-project\package.json File

{
  "//": 
  ["Rename this sample file to package.json to use on your project."
  , "The sample-package.json file will be overwritten in updates/reinstalls."
  ],
  "name": "sample-node-project",
  "version": "0.0.1",
  "description": "node part of the project",
  "main": "main.js",
  "author": "janeasystems",
  "license": ""
}
emirhankolver commented 3 years ago

I fixed this issue by re-creating the project with downgrading react-native version to 0.61.5. it worked well! nodejs-mobile-react-native Doesn't working at latest version of react-native.

gengshenghong commented 3 years ago

I got the same issue (bad cast) with 0.63.4, and then download to 0.61.5, the bad cast issue disappear, but still get below crash:

--------- beginning of crash
01-11 13:52:21.464 29638 29900 E AndroidRuntime: FATAL EXCEPTION: FlipperEventBaseThread
01-11 13:52:21.464 29638 29900 E AndroidRuntime: Process: com.newapp, PID: 29638
01-11 13:52:21.464 29638 29900 E AndroidRuntime: java.lang.UnsatisfiedLinkError: couldn't find DSO to load: libflipper.so caused by: dlopen failed: library "libfbjni.so" not found result: 0
01-11 13:52:21.464 29638 29900 E AndroidRuntime:    at com.facebook.soloader.SoLoader.doLoadLibraryBySoName(SoLoader.java:896)
01-11 13:52:21.464 29638 29900 E AndroidRuntime:    at com.facebook.soloader.SoLoader.loadLibraryBySoNameImpl(SoLoader.java:725)
01-11 13:52:21.464 29638 29900 E AndroidRuntime:    at com.facebook.soloader.SoLoader.loadLibraryBySoName(SoLoader.java:649)
01-11 13:52:21.464 29638 29900 E AndroidRuntime:    at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:629)
01-11 13:52:21.464 29638 29900 E AndroidRuntime:    at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:577)
01-11 13:52:21.464 29638 29900 E AndroidRuntime:    at com.facebook.flipper.android.EventBase.<clinit>(EventBase.java:19)
01-11 13:52:21.464 29638 29900 E AndroidRuntime:    at com.facebook.flipper.android.FlipperThread.run(FlipperThread.java:25)

any idea?

harlin22 commented 3 years ago

I am also getting the bad_cast error with exactly the same set as @Alonew0lfxx. Same settings except RN 0.63.3. Everything is working fine in iOS. In logcat I am able to console.log the imported nodejs object as

{ "channel": { "_subscriber": { "_subscriptionsForType": {}, "_currentSubscription": null }, "name": "_EVENTS_" } }

Any help would be awesome :-)

emirhankolver commented 3 years ago

I am also getting the bad_cast error with exactly the same set as @Alonew0lfxx. Same settings except RN 0.63.3. Everything is working fine in iOS. In logcat I am able to console.log the imported nodejs object as

{ "channel": { "_subscriber": { "_subscriptionsForType": {}, "_currentSubscription": null }, "name": "_EVENTS_" } }

Any help would be awesome :-)

Hello I fixed most of problems by using ndk-r21b

For Linux https://dl.google.com/android/repository/android-ndk-r21b-linux-x86_64.zip For Windows https://dl.google.com/android/repository/android-ndk-r21b-windows-x86_64.zip For Mac https://dl.google.com/android/repository/android-ndk-r21b-darwin-x86_64.zip

harlin22 commented 3 years ago

I am also getting the bad_cast error with exactly the same set as @Alonew0lfxx. Same settings except RN 0.63.3. Everything is working fine in iOS. In logcat I am able to console.log the imported nodejs object as { "channel": { "_subscriber": { "_subscriptionsForType": {}, "_currentSubscription": null }, "name": "_EVENTS_" } } Any help would be awesome :-)

Hello I fixed most of problems by using ndk-r21b

For Linux https://dl.google.com/android/repository/android-ndk-r21b-linux-x86_64.zip For Windows https://dl.google.com/android/repository/android-ndk-r21b-windows-x86_64.zip For Mac https://dl.google.com/android/repository/android-ndk-r21b-darwin-x86_64.zip

This worked.....thank you so much 🥇

gengshenghong commented 3 years ago

Works for me too!!!! I downgrade to using ndk-r21e (was using latest ndk-r22)

Thanks so much, hope it to be compatible with latest NDK in future.