eddieowens / react-native-boundary

Native implementation of geofencing/region monitoring
Apache License 2.0
121 stars 72 forks source link

Not working with ExpoKit (MainApplication cannot be cast to ReactApplication) #28

Closed RobertoNegro closed 5 years ago

RobertoNegro commented 5 years ago

Hi, I've successfully linked this library to my Expo-detached project (ExpoKit), but the problem is when I should receive the event (in logcat I can see "RNBoundary: Enter geofence event detected. Sending event.") on Android I'm always getting this fatal exception:

E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start service com.eddieowens.services.BoundaryEventHeadlessTaskService@4734458 with Intent { cmp=/com.eddieowens.services.BoundaryEventHeadlessTaskService (has extras) }: java.lang.ClassCastException: host.exp.exponent.MainApplication cannot be cast to com.facebook.react.ReactApplication at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3904) at android.app.ActivityThread.access$1700(ActivityThread.java:237) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7045) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) Caused by: java.lang.ClassCastException: host.exp.exponent.MainApplication cannot be cast to com.facebook.react.ReactApplication at com.facebook.react.HeadlessJsTaskService.getReactNativeHost(HeadlessJsTaskService.java:169) at com.facebook.react.HeadlessJsTaskService.startTask(HeadlessJsTaskService.java:98) at com.facebook.react.HeadlessJsTaskService.onStartCommand(HeadlessJsTaskService.java:51) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3885) at android.app.ActivityThread.access$1700(ActivityThread.java:237)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:214)  at android.app.ActivityThread.main(ActivityThread.java:7045)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 

Any idea or direction would be appreciated.

Expo 31.0.2 Android 9 react-native-bundary 1.0.8

eddieowens commented 5 years ago

Hi @RobertoNegro, I'm not as familiar with ExpoKit, but did you follow all of the instructions listed here?

RobertoNegro commented 5 years ago

Hi @RobertoNegro, I'm not as familiar with ExpoKit, but did you follow all of the instructions listed here?

Hi, and thank you for the reply. Yes, I've followed all the detaching istructions. For what I've seen, the error with react-native-boundary is caused from the differencies from ExpoKit VS a standard RN project: With ExpoKit (ejected/detached Expo project), the android class MainApplication is not extending ReactApplication. That is causing the error, since this library uses HeadlessJS (the error is caused from HeadlessJsTaskService.startTask()) and, from what I've seen after some researches, ExpoKit is not compatible with that (I'm not 100% sure, but I'm more like 90% sure, I can't find an official resource which sais that is or is not compatible).

At the end I've started again a new project, this time without Expo, and after some changes (I had to change all the imports from the ExpoKit library to other libraries), it worked well.

If someone else is facing the same problem, my suggestion is to start a new RN project without Expo, so you can use all the libraries that use HeadlessJS (for background code execution).