问题
My Android react-native app is crashing with an ExecutionException
.
"Could not connect to development server."
The method that is crashing is ReactInstanceManagerImpl.createReactContext()
.
But this is not the normal error as this screenshot shows:
Logcat shows me this stacktrace:
Exception in native call from JS
java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.RuntimeException:
Could not connect to development server.
at com.facebook.react.ReactInstanceManagerImpl.createReactContext(ReactInstanceManagerImpl.java:860)
at com.facebook.react.ReactInstanceManagerImpl.access$700(ReactInstanceManagerImpl.java:98)
at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:194)
...
Caused by: java.util.concurrent.ExecutionException: java.lang.RuntimeException:
at com.facebook.react.common.futures.SimpleSettableFuture.get(SimpleSettableFuture.java:68)
at com.facebook.react.ReactInstanceManagerImpl.createReactContext(ReactInstanceManagerImpl.java:831)
... 9 more
Caused by: java.lang.RuntimeException:
at com.facebook.react.bridge.ReactBridge.loadScriptFromFile(Native Method)
at com.facebook.react.bridge.JSBundleLoader$2.loadScript(JSBundleLoader.java:58)
at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:148)
at com.facebook.react.ReactInstanceManagerImpl$4.call(ReactInstanceManagerImpl.java:851)
...
This suggests that the server has sent the javascript code back to the device and the ReactBridge.loadScriptFromFile()
somehow can't handle the bundle.
Points to clarify:
- this happens on emulator & device (connected via USB)
- this happens on Linux (Mint) & Windows (8.1)
- I did run
adb reverse tcp:8081 tcp:8081
as per the normal advice - I have run
react-native start
and the packager/server is running - the device DOES connect to the development server, as this is logged in the packager window
- the URL that the device hits does return data, as can be seen if copied into the browser address bar
How is this different from the normal "Could not connect..." message?
Here is a screenshot of what happens when the server is legitimately not running:
The differences include:
- no
ExecutionException
orRuntimeException
before the error message - further clarity providing the URL that was called on the server
- error comes from
DevServerHelper.onFailure()
Logcat shows the stacktrace to be quite different in this normal expected case:
Unable to download JS bundle
com.facebook.react.devsupport.DebugServerException:
Could not connect to development server.
at com.facebook.react.devsupport.DevServerHelper$1.onFailure(DevServerHelper.java:196)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:140)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
回答1:
This seems to have been caused by not having Windows Build Tools correctly installed. This broke the node-gyp
commands required by my project.
Following the steps on the node-gyp page got my machine set up correctly.
After a new build I got this error once, and then it disappeared with the project building and running correctly.
来源:https://stackoverflow.com/questions/42768354/react-native-could-no-connect-to-development-server-in-createreactcontext