Skip to content

Android crash on New Architecture: "You should not use ReactNativeHost directly" in trackDeepLinks #1147

@RomanSytnyk

Description

@RomanSytnyk

The app crashes on Android with New Architecture (Bridgeless mode) enabled. The crash occurs on every cold launch via onHostResume
and on deep links via onNewIntent.

Environment

  • @segment/analytics-react-native: 2.22.0
  • React Native: New Architecture enabled (newArchEnabled=true)
  • Expo SDK: 55

Stack trace

java.lang.RuntimeException: You should not use ReactNativeHost directly in the New Architecture
   com.facebook.react.ReactApplication.getReactNativeHost(ReactApplication.kt:20)
   com.segmentanalyticsreactnative.AnalyticsReactNativeModule.trackDeepLinks(AnalyticsReactNativeModule.kt:221)
   com.segmentanalyticsreactnative.AnalyticsReactNativeModule.onHostResume(AnalyticsReactNativeModule.kt:255)
   com.facebook.react.bridge.ReactContext.onHostResume(ReactContext.java:255)
   com.facebook.react.runtime.ReactLifecycleStateManager.moveToOnHostPause(ReactLifecycleStateManager.kt:50)
   com.facebook.react.runtime.ReactHostImpl.onHostPause(ReactHostImpl.kt:284)
   com.facebook.react.ReactDelegate.onHostPause(ReactDelegate.kt:156)
   com.facebook.react.ReactActivityDelegate.onPause(ReactActivityDelegate.java:196)

expo.modules.ReactActivityDelegateWrapper$onPause$1.invokeSuspend(ReactActivityDelegateWrapper.kt:201)

In React Native's New Architecture, ReactApplication.getReactNativeHost() throws a RuntimeException

Suggested fix

Use reactApplicationContext.getNativeModule() directly: the same pattern already used by setAnonymousId() in the same file:

val sovran = reactApplicationContext.getNativeModule(SovranModule::class.java)

See PR: #1146

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions