I am using monorepo created using Yarn Workspaces with Typescript which has a react-native (0.68.2) project (mobile folder) and a common folder which contains the common files to be shared across projects. Here the mobile project is dependent on common files and I have configured it referring to this doc.
Starting with Android, the app runs fine in debug mode. When I create a release build, the apk is generated successfully. After installing the app, on opening, it crashes as soon as it opens. On observing the error, it looks like there is a problem with the bundle. I tried manually running the bundle command for packing it and tried generating the apk, but it still results in the same error.
Error from LogCat:
2022-06-03 22:36:28.457 5699-5723/? E/AndroidRuntime: FATAL EXCEPTION: create_react_context
Process: com.myapp.mobile, PID: 5699
java.lang.RuntimeException: Unable to load script. Make sure you're either running Metro (run 'npx react-native start') or that your bundle 'index.android.bundle' is packaged correctly for release.
at com.facebook.react.bridge.CatalystInstanceImpl.jniLoadScriptFromAssets(Native Method)
at com.facebook.react.bridge.CatalystInstanceImpl.loadScriptFromAssets(CatalystInstanceImpl.java:248)
at com.facebook.react.bridge.JSBundleLoader$1.loadScript(JSBundleLoader.java:29)
at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:277)
at com.facebook.react.ReactInstanceManager.createReactContext(ReactInstanceManager.java:1422)
at com.facebook.react.ReactInstanceManager.access$1200(ReactInstanceManager.java:138)
at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:1111)
at java.lang.Thread.run(Thread.java:923)
Any Suggestions?
The bundle was packed and placed in the wrong path which was causing this error.
Always the bundle should be packed and placed in only in this path android\app\src\main\assets\index.android.bundle
for android. The below command will do it correctly.
npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android\app\src\main\assets\index.android.bundle