Realm-js: [Android] Cannot read property 'debugHosts' of undefined

Created on 11 Jul 2016  路  20Comments  路  Source: realm/realm-js

Hi There, i just did a vanilla installation for the realm specified here. However, i got this exception. Below is a screenshot of the app. I was not able to launch it at all. If i remove the constructor for Realm the app works.

Steps Followed:

  1. react-native init
  2. npm install --save realm
  3. rnpm link realm
  4. react-native run-android

package.json

{
  "name": "Realm14",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start"
  },
  "dependencies": {
    "react": "15.2.1",
    "react-native": "^0.29.0",
    "realm": "^0.14.1"
  }
}

image

Most helpful comment

Fixed by closing react-native packager again, closing simulator and running npm cache clean, npm start and react-native run-ios

All 20 comments

I have the same issue. On iOs it is working. Did you maybe manage to solve this?

I tested with RN version 0.28 and it is working. So I assume that changes in 0.29 breaks compatibility. This is specially mentioned:

Change the application template (49f20f4) - @foghina

Plugins which pass activity reference in the constructor need to be updated to extend ReactContextBaseJavaModule use getCurrentActivity to get the activity reference. This change is backward compatible.
Apps need to be migrated to use the latest template

Migration instructions for apps

  1. Create a new file MainApplication.java in the same folder as MainActivity.java
  2. Update it to match - https://github.com/facebook/react-native/blob/0.29-stable/local-cli/generator-android/templates/package/MainApplication.java and move your customisations from MainActivity.java to this file
  3. Update MainActivity.java to match - https://github.com/facebook/react-native/blob/0.29-stable/local-cli/generator-android/templates/package/MainActivity.java
  4. Add android:name=".MainApplication" in the section of AndroidManifest.xml. It should now look like,

  • android:name=".MainApplication"
    android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:theme="@style/AppTheme">
    Clean gradle cache - ./gradlew clean

@gasperz - Did the fix work for you?

Migration instructions for apps

  1. Create a new file MainApplication.java in the same folder as MainActivity.java
  2. Update it to match - https://github.com/facebook/react-native/blob/0.29-stable/local-cli/generator-android/templates/package/MainApplication.java and move your customisations from MainActivity.java to this file
  3. Update MainActivity.java to match - https://github.com/facebook/react-native/blob/0.29-stable/local-cli/generator-android/templates/package/MainActivity.java
  4. Add android:name=".MainApplication" in the section of AndroidManifest.xml. It should now look like,

android:name=".MainApplication" android:allowBackup="true" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme"> Clean gradle cache - ./gradlew clean

I don't know how to apply this because I think this needs to be applied to module itself, not just android settings in my app.

Steps to fix:

rnpm link realm

Add the module in your app's /android/.../MainApplication.Java file, should look like this

 @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new RealmReactPackage() // this, also import it
      );
    }
  };

rebuild/clean should fix it

I tried that with 0.29.2 of react native, but my application failed to run. I get "Unfortunately, app has stopped."

Post the stack trace from Android Studio

I added

android:name=".MainApplication"

in AndroidManifest.xml in "application" section. And now it works. This is probably needed until react-native fixes template files.

Hello guys, I have same issue here.
I still get this error after doing the steps mentioned by @octohedron.
Any idea ? appreciate your help!

RN 0.29.2
react 15.1.0
realm ^0.14.1

Post the stack trace from Android Studio

Also, try adding android:name=".MainApplication" in the manifest as gasperz added.

07-21 09:50:41.043 24299-25546/com.kark E/unknown:JSDebuggerWebSocketClient: Error occurred, shutting down websocket connection: Websocket exception
                                                                                java.net.ConnectException: Failed to connect to /xxx.xxx.x.xx:8081
                                                                                    at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:142)
                                                                                    at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
                                                                                    at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:188)
                                                                                    at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127)
                                                                                    at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
                                                                                    at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289)
                                                                                    at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
                                                                                    at okhttp3.RealCall.getResponse(RealCall.java:240)
                                                                                    at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
                                                                                    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
                                                                                    at okhttp3.RealCall.access$100(RealCall.java:30)
                                                                                    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
                                                                                    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
                                                                                    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)
07-21 09:50:41.056 24299-25548/com.kark E/unknown:JSDebuggerWebSocketClient: Error occurred, shutting down websocket connection: Websocket exception
                                                                                java.net.ConnectException: Failed to connect to /xxx.xxx.x.xx:8081
                                                                                    at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:142)
                                                                                    at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
                                                                                    at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:188)
                                                                                    at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127)
                                                                                    at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
                                                                                    at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289)
                                                                                    at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
                                                                                    at okhttp3.RealCall.getResponse(RealCall.java:240)
                                                                                    at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
                                                                                    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
                                                                                    at okhttp3.RealCall.access$100(RealCall.java:30)
                                                                                    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
                                                                                    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
                                                                                    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)
07-21 09:50:41.070 24299-25552/com.kark E/unknown:JSDebuggerWebSocketClient: Error occurred, shutting down websocket connection: Websocket exception
                                                                                java.net.ConnectException: Failed to connect to /xxx.xxx.x.xx:8081
                                                                                    at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:142)
                                                                                    at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
                                                                                    at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:188)
                                                                                    at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127)
                                                                                    at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
                                                                                    at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289)
                                                                                    at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
                                                                                    at okhttp3.RealCall.getResponse(RealCall.java:240)
                                                                                    at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
                                                                                    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
                                                                                    at okhttp3.RealCall.access$100(RealCall.java:30)
                                                                                    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
                                                                                    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
                                                                                    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)
07-21 09:50:41.070 24299-25552/com.kark E/unknown:React: Unable to connect to remote debugger
                                                            java.net.ConnectException: Failed to connect to /xxx.xxx.x.xx:8081
                                                                at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:142)
                                                                at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
                                                                at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:188)
                                                                at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127)
                                                                at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
                                                                at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289)
                                                                at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
                                                                at okhttp3.RealCall.getResponse(RealCall.java:240)
                                                                at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
                                                                at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
                                                                at okhttp3.RealCall.access$100(RealCall.java:30)
                                                                at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
                                                                at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
                                                                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)
07-21 09:50:41.082 24299-24299/com.kark E/unknown:React: Exception in native call from JS
                                                            java.io.IOException: Unable to connect with remote debugger
                                                                at com.facebook.react.devsupport.DevSupportManagerImpl$16.onFailure(DevSupportManagerImpl.java:621)
                                                                at com.facebook.react.bridge.WebsocketJavaScriptExecutor$1.onFailure(WebsocketJavaScriptExecutor.java:87)
                                                                at com.facebook.react.bridge.WebsocketJavaScriptExecutor$2.onFailure(WebsocketJavaScriptExecutor.java:136)
                                                                at com.facebook.react.bridge.JSDebuggerWebSocketClient.abort(JSDebuggerWebSocketClient.java:257)
                                                                at com.facebook.react.bridge.JSDebuggerWebSocketClient.onFailure(JSDebuggerWebSocketClient.java:231)
                                                                at okhttp3.ws.WebSocketCall$1.onFailure(WebSocketCall.java:104)
                                                                at okhttp3.RealCall$AsyncCall.execute(RealCall.java:140)
                                                                at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
                                                                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.net.ConnectException: Failed to connect to /xxx.xxx.x.xx:8081
                                                                at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:142)
                                                                at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
                                                                at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:188)
                                                                at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127)
                                                                at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
                                                                at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289)
                                                                at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
                                                                at okhttp3.RealCall.getResponse(RealCall.java:240)
                                                                at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
                                                                at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
                                                                at okhttp3.RealCall.access$100(RealCall.java:30)
                                                                at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
                                                                at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)聽
                                                                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)聽

                                                    --------- beginning of system

@octohedron Thanks for your reply.
I already added android:name=".MainApplication" into the manifest as RN 0.29.0 documentation mentioned, but still getting this error.

Missing Realm constructor - please ensure RealmReact framework is included!

@octohedron Another issue about realm, if I stop debugging mode.

If you read the error, it says "Failed to connect to /xxx.xxx.x.xx:8081". I don't know what device you are running this from, if it's an emulator you probably need to ./adb forward tcp:8081 tcp:8081, this is solved by google and not related to realm, same as the other error.

This issue is about the problem posted by OP: "Cannot read property 'debugHosts' of undefined", if that is not your issue, try stack overflow, google, etc and If you don't find answers you might want to open a new issue.

I am facing the same issue after all the steps mentioned above. The stack traces are:

'07-21 08:17:49.648 2817-3675/com.livepay E/ReactNativeJS: Missing Realm constructor - please ensure RealmReact framework is included!
07-21 08:17:49.657 2817-3675/com.livepay W/ReactNativeJS: Unable to symbolicate stack trace: undefined is not an object (evaluating 'RCTNetworkingNative.sendRequest')'

I get into the same issue, here how I solved it:

As @gasperz says, the issue is related to react-native: 0.29+ breaks compatibility, they added support for a new file called MainApplication.java, so,

Event running rnpm link realm using rnpm v1.9.0 do not fix, why? when you run the link rnpm it only updates the file MainActivity.java not the new one, I don't know why, but I have installed the lastest verion of rnpm, run the link and it only changed the old one adding the import line:

import io.realm.react.RealmReactPackage;

So, just move that line to MainApplication.java and add the package to the list returned as @octohedron has said.

Add the module in your app's /android/.../MainApplication.Java file, should look like this

@Override
protected List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
        new MainReactPackage(),
        new RealmReactPackage() // this line
    );
}

Another issue you could have is this:

screen shot 2016-07-21 at 5 18 21 pm copy

Just go to ./android.settings.grandle and add this:

include ':realm'
project(':realm').projectDir = file('../node_modules/realm/android')

Hope that help...

So it sounds like this has been resolved. Thanks for all the help figuring this out.

@frangeris you got it right, cheers mate. Let's just hope things will work as they should since rnpm will be merged to react-native in 0.30 馃槃

I have the same issue on ios.
react-native: 0.38.0
ios: 10.0
osx 10.11.6
realm: 0.15.0

Fixed by closing react-native packager again, closing simulator and running npm cache clean, npm start and react-native run-ios

Was this page helpful?
0 / 5 - 0 ratings

Related issues

texas697 picture texas697  路  3Comments

fever324 picture fever324  路  4Comments

timanglade picture timanglade  路  3Comments

jmartindivmedianet picture jmartindivmedianet  路  3Comments

gwanjar picture gwanjar  路  3Comments