ReactNative 常见问题汇总

1. Unable to load script from assets ‘index.android.bundle’

1
2
3
4
5
6
7
8
9
10
java.lang.RuntimeException: Unable to load script from assets 'index.android.bundle'. Make sure your bundle is packaged correctly or you're running a packager server.
at com.facebook.react.bridge.CatalystInstanceImpl.jniLoadScriptFromAssets(Native Method)
at com.facebook.react.bridge.CatalystInstanceImpl.loadScriptFromAssets(CatalystInstanceImpl.java:216)
at com.facebook.react.bridge.JSBundleLoader$1.loadScript(JSBundleLoader.java:32)
at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:243)
at com.facebook.react.ReactInstanceManager.createReactContext(ReactInstanceManager.java:1116)
at com.facebook.react.ReactInstanceManager.access$900(ReactInstanceManager.java:117)
at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:916)
at java.lang.Thread.run(Thread.java:818)

出现此问题的原因是因为没有找到index.android.bundle文件,我是用的是2.0.1,解决方案如下

  1. 创建目录 android\app\src\main\assets
  2. 执行以下命令,将index.android.bundle 文件拷贝到 asset目录
    react-native bundle –platform android –dev false –entry-file index.js –bundle-output android/app/src/main/assets/index.android.bundle –assets-dest android/app/src/main/res/

如此以后,再执行react-native run-android 启动app即可解决