mrousavy / NitroBenchmarks

Benchmarks TurboModules vs ExpoModules vs NitroModules
59 stars 1 forks source link

Not able to run android app #1

Open shubhamguptadream11 opened 6 days ago

shubhamguptadream11 commented 6 days ago

I am trying to benchmark NitroModule on Android but cannot do so using the provided NitroBenchmarks repository.

Steps to Reproduce:

Reproducer App: NitroBenchmarks Repo

Able to run this on iOS simulator

The following error appears on Metro, preventing me from proceeding. Could you please assist in resolving this?

 (NOBRIDGE) LOG  Bridgeless mode is enabled
 (NOBRIDGE) ERROR  Error: Failed to get NitroModules: The native "NitroModules" TurboModule could not be found.
* Make sure react-native-nitro-modules/NitroModules is correctly autolinked (run `npx react-native config` to verify)
* Make sure you enabled the new architecture (TurboModules) and CodeGen properly generated the "NativeNitroModules"/NitroModulesCxx specs. See https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md
* Make sure you are using react-native 0.74.0 or higher, because NitroModules are built with C++ TurboModules.
* Make sure you rebuilt the app.
* Make sure gradle is synced.
 (NOBRIDGE) ERROR  Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.

Attaching screenshot for reference: Screenshot_20240923_152859

mrousavy commented 6 days ago

Hey - yep this benchmark was built in just a few min for iOS only. I haven't implemented Android yet, but feel free to implement the same specs on Android to run it :)

shubhamguptadream11 commented 4 days ago

@mrousavy I tried creating a Nitro module for Android by following the Nitro docs. I created a bare React Native project and tried adding the Nitro module using the template.

However, I got stuck trying to resolve this issue that occurs during runtime:

 (NOBRIDGE) ERROR  Warning: Error: Exception in HostFunction: Cannot create an instance of HybridObject "Calculator" - It has not yet been registered in the Nitro Modules HybridObjectRegistry! Suggestions:
- If you use Nitrogen, make sure your `nitro.json` contains `Calculator` on this platform.
- If you use Nitrogen, make sure your library (*Package.java)/app (MainApplication.java) calls `System.loadLibrary("<<androidCxxLibName>>")` somewhere on app-startup.
- If you use Nitrogen, make sure your cpp-adapter.cpp calls `margelo::nitro::<<cxxNamespace>>::initialize(vm)`.
- If you use Nitrogen, inspect the generated `<<androidCxxLibName>>OnLoad.cpp` file.
- If you don't use Nitrogen, make sure you called `HybridObjectRegistry.registerHybridObject(...)`.
    at App
    at RCTView
    at View (http://10.0.2.2:8081/index.bundle//&platform=android&dev=true&lazy=true&minify=false&app=com.myapp&modulesOnly=false&runModule=true:64713:43)
    at RCTView
    at View (http://10.0.2.2:8081/index.bundle//&platform=android&dev=true&lazy=true&minify=false&app=com.myapp&modulesOnly=false&runModule=true:64713:43)
    at AppContainer (http://10.0.2.2:8081/index.bundle//&platform=android&dev=true&lazy=true&minify=false&app=com.myapp&modulesOnly=false&runModule=true:64586:25)
    at MyApp(RootComponent) (http://10.0.2.2:8081/index.bundle//&platform=android&dev=true&lazy=true&minify=false&app=com.myapp&modulesOnly=false&runModule=true:117226:28)
 (NOBRIDGE) ERROR  Warning: TypeError: Cannot read property 'NitroCalculator' of undefined

This error is located at:
    in App
    in RCTView (created by View)
    in View (created by AppContainer)
    in RCTView (created by View)
    in View (created by AppContainer)
    in AppContainer
    in MyApp(RootComponent)
 (NOBRIDGE) ERROR  Warning: TypeError: Cannot read property 'NitroCalculator' of undefined

This error is located at:
    in App
    in RCTView (created by View)
    in View (created by AppContainer)
    in RCTView (created by View)
    in View (created by AppContainer)
    in AppContainer
    in MyApp(RootComponent)

Repo link: https://github.com/shubham43245/nitro-module-benchmark

Attaching snapshot for reference: Screenshot_20240925_212633

mrousavy commented 4 days ago

hmm, maybe the react-native CLI (the default autolinking tool) doesn't pick up your module properly, and NitroCalculatorPackage is never added?

Might need to add it to your react-native.config.js, or link via yarn/bun, or monorepo, or explicitly add it to your app's gradle - multiple ways of doing that.

shubhamguptadream11 commented 4 days ago

Cool. I tired auto linking my module by creating react-native.config.js file and adding my module path in it. Now it was failing during build time.

> Task :nitro-calculator:compileDebugKotlin FAILED
e: file:///Users/shubhamgupta/D11Repo/nitro-package/nitro-module-benchmark/node_modules/nitro-calculator/android/src/main/java/com/margelo/nitro/calculator/HybridCalculator.kt:4:26 Unresolved reference: HybridObject
e: file:///Users/shubhamgupta/D11Repo/nitro-package/nitro-module-benchmark/node_modules/nitro-calculator/android/src/main/java/com/margelo/nitro/calculator/HybridCalculator.kt:5:26 Unresolved reference: HybridObjectRegistry
e: file:///Users/shubhamgupta/D11Repo/nitro-package/nitro-module-benchmark/node_modules/nitro-calculator/android/src/main/java/com/margelo/nitro/calculator/HybridCalculator.kt:9:26 Unresolved reference: HybridObject

It seems that now it was picking module code at least 🤔

This is the react-native.config.js file I added at root of my project.

module.exports = {
    dependencies: {
      'NitroCalculator': {
        root: './modules/NitroCalculator',
      },
    },
  };