在现有Android 应用集成 reactNative 运行 crash



  • 参考 文档http://reactnative.cn/docs/0.30/integration-with-existing-apps.html#content 中步骤,在init 出的AwesomeProject 中添加了MyReactActivity, 后运行 后,应用直接崩溃掉了

    0_1470217155940_bugss.jpg
    下面是错误日志, packager server is running 确定是开启的, 模拟器运行的。
    08-03 09:32:45.215 1216-2098/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
    08-03 09:32:45.697 2699-2699/com.awesomeproject E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.awesomeproject, PID: 2699
    java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.RuntimeException: Could not connect to development server.
    Try the following to fix the issue:
    Ensure that the packager server is running
    Ensure that your device/emulator is connected to your machine and has USB debugging enabled - run 'adb devices' to see a list of connected devices
    If you're on a physical device connected to the same machine, run 'adb reverse tcp:8081 tcp:8081' to forward requests from your device
    If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to y
    at com.facebook.react.ReactInstanceManagerImpl.createReactContext(ReactInstanceManagerImpl.java:912)
    at com.facebook.react.ReactInstanceManagerImpl.access$700(ReactInstanceManagerImpl.java:99)
    at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:196)
    at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:179)
    at android.os.AsyncTask$2.call(AsyncTask.java:295)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)
    Caused by: java.util.concurrent.ExecutionException: java.lang.RuntimeException: Could not connect to development server.
    Try the following to fix the issue:
    Ensure that the packager server is running
    Ensure that your device/emulator is connected to your machine and has USB debugging enabled - run 'adb devices' to see a list of connected devices
    If you're on a physical device connected to the same machine, run 'adb reverse tcp:8081 tcp:8081' to forward requests from your device
    If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to y
    at com.facebook.react.common.futures.SimpleSettableFuture.get(SimpleSettableFuture.java:68)
    at com.facebook.react.ReactInstanceManagerImpl.createReactContext(ReactInstanceManagerImpl.java:883)
    ... 9 more
    Caused by: java.lang.RuntimeException: Could not connect to development server.
    Try the following to fix the issue:
    Ensure that the packager server is running
    Ensure that your device/emulator is connected to your machine and has USB debugging enabled - run 'adb devices' to see a list of connected devices
    If you're on a physical device connected to the same machine, run 'adb reverse tcp:8081 tcp:8081' to forward requests from your device
    If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to y
    at com.facebook.react.bridge.ReactBridge.loadScriptFromAssets(Native Method)
    at com.facebook.react.bridge.JSBundleLoader$1.loadScript(JSBundleLoader.java:32)
    at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:149)
    at com.facebook.react.ReactInstanceManagerImpl$4.call(ReactInstanceManagerImpl.java:903)
    at com.facebook.react.ReactInstanceManagerImpl$4.call(ReactInstanceManagerImpl.java:884)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$1.run(MessageQueueThreadImpl.java:74)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
    at android.os.Looper.loop(Looper.java:148)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196)
    ... 1 more
    08-03 09:32:47.862 1532-1568/system_process E/InputDispatcher: channel 'fe644ba com.awesomeproject/com.awesomeproject.ReactTestActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
    08-03 09:32:47.896 1532-2569/system_process E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f51ff3cfb10

    而且有时候,一运行android工程,server特别容易挂掉如下图
    0_1470221532385_bjgssss.jpg



  • .setUseDeveloperSupport(BuildConfig.DEBUG)
    

    改成

    .setUseOldBridge(true)
    .setUseDeveloperSupport(true)
    

    可能还需要改下混淆



  • 感谢!.setUseDeveloperSupport(true) 后, debug 模式已经能正常跑起来了 。
    但打release包,还是不成功!
    0_1470299530718_dea55606-5a5f-11e6-8ee4-fa258f040177.jpg

    我是参考了https://facebook.github.io/react-native/docs/signed-apk-android.html 文档描述, 在build.gradle 文件中 引入 “apply from: "../node_modules/react-native/react.gradle"” , 然后 在 命令行 运行“gradlew assembleRelease” ,如下是运行环境:
    SO: windows 10 , node: '5.5.0', npm: "'3.3.12'" , react: 15.2.1 react-native: 0.30.0