Skip to content

Need Help with Enabling New architecture in brownfield React Native with android multi module app #2594

Open
@sanjeevyadavIT

Description

@sanjeevyadavIT

Description

I have a brownfield React Native setup where I have enabled New Architecture and am now trying to write a Turbo Native Module as per the official documentation. However, I am encountering the following error when launching the React Fragment:

Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'NativeLocalStorage' could not be found. 
Verify that a module by this name is registered in the native binary.

Project Structure

I have a multi-module Android project structured as follows:

react-native-brownfield-example/
│── app/                 # Native Android app module
│   ├── MainApplication.kt
│   ├── MainActivity.kt
│   ├── build.gradle.kts
│
│── feature/             # Modularized feature components
│   ├── react/           # Contains all React integration code
│   │   ├── NativeLocalStorageModule.kt
│   │   ├── ReactNativeHost
│   │   ├── build.gradle.kts
│
│── reactNative/         # React Native project
│   ├── specs/NativeLocalStorage.ts
│   ├── index.android.js
│   ├── package.json

Reproducible Example

A minimal, reproducible example is available in this repository:

🔗 GitHub Repo

Steps to Reproduce

  1. Clone the repository:
git clone https://github.com/sanjeevyadavIT/react-native-brownfield-example.git
cd react-native-brownfield-example
git checkout feat/add-native-module
  1. Install dependencies:
cd reactNative
npm install
cd ..
  1. Generate codegen artifacts:
./gradlew generateCodegenArtifactsFromSchema
  1. Open the project in Android Studio and run the app.
Image
  1. Click "Open React Fragment" in the UI.
Image
  1. Observe the Invariant Violation error.

Solution provided by Facebook team @cortinico

I raised this same issue facebook/react-native#49246 and @cortinico provided the solution here -> facebook/react-native#49246 (comment)

But the problem with the solution provided by Nicola Corti is (although it works) it adds a @private/react-native-feature in node_modules and move all the Android integration code written in feature/react into $rootProject/reactNative/@node_modules/@private/react-native-feature but my Actual project has tons of feature module which depends on feature/react module and some of the class are auto generated using dependency injection that relies on code written in feature/react, using this solution doesn't work

Help Needed

✅ Is there any changes needed in my brownfield setup for android multi-module project?
✅ How can I correctly register my Turbo Native Module within a multi-module Android project?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions