react-native-vision-camera: πŸ› Android unknown crash

device id of undefined

Camera.tsx

  static getDerivedStateFromProps(props: CameraProps, state: CameraState): CameraState | null {
    const newCameraId = props.device.id;
    if (state.cameraId !== newCameraId) return { ...state, cameraId: newCameraId };
    return null;
  }

my code check

if(device && device.id) {
    console.log(device.id)
  }

crash app

android 10 xiaomi note 7

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 37 (14 by maintainers)

Most upvoted comments

Hey! I ran into the same issue when testing out the module in my own application.

Only one of my devices actually printed any meaningful output when crashing. This was logged on a Samsung Galaxy S8+

--------- beginning of crash
2021-03-09 12:03:42.381 17026-17026/com.test.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.test.app, PID: 17026
    java.lang.IllegalStateException: Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android' and ensure it has the same version as 'kotlinx-coroutines-core'
        at kotlinx.coroutines.internal.MainDispatchersKt.throwMissingMainDispatcherException(MainDispatchers.kt:76)
        at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:107)
        at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:91)
        at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:302)
        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:27)
        at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109)
        at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:158)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
        at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:49)
        at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source:1)
        at com.mrousavy.camera.CameraView.update(CameraView.kt:195)
        at com.mrousavy.camera.CameraViewManager.onAfterUpdateTransaction(CameraViewManager.kt:124)
        at com.mrousavy.camera.CameraViewManager.onAfterUpdateTransaction(CameraViewManager.kt:11)
        at com.facebook.react.uimanager.ViewManager.updateProperties(ViewManager.java:51)
        at com.facebook.react.uimanager.NativeViewHierarchyManager.createView(NativeViewHierarchyManager.java:270)
        at com.facebook.react.uimanager.UIViewOperationQueue$CreateViewOperation.execute(UIViewOperationQueue.java:186)
        at com.facebook.react.uimanager.UIViewOperationQueue$DispatchUIFrameCallback.dispatchPendingNonBatchedOperations(UIViewOperationQueue.java:1112)
        at com.facebook.react.uimanager.UIViewOperationQueue$DispatchUIFrameCallback.doFrameGuarded(UIViewOperationQueue.java:1083)
        at com.facebook.react.uimanager.GuardedFrameCallback.doFrame(GuardedFrameCallback.java:29)
        at com.facebook.react.modules.core.ReactChoreographer$ReactChoreographerDispatcher.doFrame(ReactChoreographer.java:175)
        at com.facebook.react.modules.core.ChoreographerCompat$FrameCallback$1.doFrame(ChoreographerCompat.java:85)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:947)
        at android.view.Choreographer.doCallbacks(Choreographer.java:761)
        at android.view.Choreographer.doFrame(Choreographer.java:693)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7050)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

The only difference I could see between the example app and my app, is that I use react-navigation instead of react-native-navigation

Looking into the react-native-navigation module I see that it has the following dependencies:

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesCore"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinCoroutinesCore"

where kotlinCoroutinesCore = 1.3.7

Also looking up the error message brought me here: https://stackoverflow.com/questions/52677638/module-with-main-dispatcher-is-missing which has some more information.

My current workaround for now is to add the following to my applications build.gradle

// Workaround for react-native-vision-camera
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"

I haven’t dug much further or read too much into this, but maybe replacing implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2" with implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"

in your modules build.gradle would solve the dependancy issue? This is just based on a quick look at the comments on the StackOverflow link, without actually going into details πŸ˜‰

I removed my workaround and did a fresh install. I can confirm that the issue is gone my part at least!

@vinkim thank you for the details!! Helps a lot. I’ve just published 1.0.1 on npm with coroutines-android instead of core, let me know if that works for you guys.

Awesome. I wasn’t sure if kotlinx-coroutines-core was included in the kotlinx-coroutines-android package, but if it works for you I assume it is. πŸš€

Thanks again for your awesome research, took me about 20 seconds to see what’s going on πŸ‘