Could not get BatchedBridge, make sure your bundle is packaged correctly
最近在原生Android專案中整合React Native環境時,頻繁出現 com.facebook.react.devsupport.JSException: Could not get BatchedBridge, make sure your bundle is packaged correctly的錯誤,詳細報錯資訊如下:
10-18 09:42:47.280 18014-18014/com.wwy.jichengrnapplication E/MemoryLeakMonitorManager: MemoryLeakMonitor.jar is not exist! 10-18 09:42:47.283 18014-18014/com.wwy.jichengrnapplication E/Minikin: Could not get cmap table size! 10-18 09:42:47.394 18014-18034/com.wwy.jichengrnapplication E/iGraphicsGLIntercept: failed to load file: /product/etc/xml/igraphics_config_cust.xml 10-18 09:42:48.589 18014-18063/com.wwy.jichengrnapplication E/unknown:React: Exception in native call from JS com.facebook.react.devsupport.JSException: Could not get BatchedBridge, make sure your bundle is packaged correctly at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:101) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31) at android.os.Looper.loop(Looper.java:166) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196) at java.lang.Thread.run(Thread.java:784) Caused by: com.facebook.jni.CppException: Could not get BatchedBridge, make sure your bundle is packaged correctly at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:101) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31) at android.os.Looper.loop(Looper.java:166) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196) at java.lang.Thread.run(Thread.java:784) 10-18 09:42:48.602 18014-18063/com.wwy.jichengrnapplication E/unknown:React: Exception in native call java.lang.RuntimeException: Error calling function: RCTDeviceEventEmitter:emit at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:101) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31) at android.os.Looper.loop(Looper.java:166) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196) at java.lang.Thread.run(Thread.java:784) Caused by: java.lang.RuntimeException: Empty Optional cannot be unwrapped at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:101) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31) at android.os.Looper.loop(Looper.java:166) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196) at java.lang.Thread.run(Thread.java:784) 10-18 09:42:48.607 18014-18063/com.wwy.jichengrnapplication E/unknown:React: Exception in native call java.lang.RuntimeException: Error calling function: AppRegistry:runApplication at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:101) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31) at android.os.Looper.loop(Looper.java:166) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196) at java.lang.Thread.run(Thread.java:784) Caused by: java.lang.RuntimeException: Empty Optional cannot be unwrapped at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:101) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31) at android.os.Looper.loop(Looper.java:166) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196) at java.lang.Thread.run(Thread.java:784) 10-18 09:42:48.659 18014-18014/com.wwy.jichengrnapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.wwy.jichengrnapplication, PID: 18014 android.view.WindowManager$BadTokenException: Unable to add window
[email protected] -- permission denied for window type 2003 at android.view.ViewRootImpl.setView(ViewRootImpl.java:908) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:372) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:128) at android.app.Dialog.show(Dialog.java:454) at com.facebook.react.devsupport.DevSupportManagerImpl$4.run(DevSupportManagerImpl.java:332) at android.os.Handler.handleCallback(Handler.java:808) at android.os.Handler.dispatchMessage(Handler.java:101) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:7425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
經檢查我的package.json和index.android.js檔案均沒有語法錯誤,然後也確認我index.android.js、package.json、RNactivity檔案中的getMainComponentName方法返回的那個值三者一致。然而並無卵用,跳轉到RN頁面後仍然是全白頁面或者這樣的紅色頁面,雙擊R鍵也沒用,重新啟動應用也沒用。
最後我進入到專案根目錄下cmd命令執行 npm start 開啟react-native的package,參照下圖執行:
待package如上圖啟動成功後點擊進入RN介面,開啟在APP上層顯示許可權,再回到app中仍然無效。
最後看到一篇文章說需要生成本地bundle檔案,解決辦法是:
在當前工程根目錄下開啟cmd執行:react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/
注意根據各自工程名字可能有不同程式的修改,
注意如果工程沒有/android/app/src/main/assets
檔案,需要先建立。
如上圖資訊則成功生成了如下的兩個bundle檔案
這樣就完美解決了,重新執行應用,就正確進入了RN頁面了。