Android 11: Segment.Initialize(...) fails with Unhandled Promise Rejection

Please provide the following:

  1. SDK Version: ~41.0.1
  2. Platforms(Android/iOS/web/all): Android 11 (march 1st 2021 update)
  3. Add the appropriate “Tag” based on what Expo library you have a question on.

Segment.initialize({androidWriteKey, iosWriteKey}) throws Unhandled promise rejection: Error
After that no events are tracked to segment using Segment.track.

Works fine in iOS.

Full expo cli console log error is:

[Unhandled promise rejection: Error: Encountered an exception while calling native method: Exception occurred while executing exported method initialize on module ExponentSegment: Method addObserver must be called on the main thread]
at node_modules/react-native/Libraries/BatchedBridge/NativeModules.js:103:50 in promiseMethodWrapper
at node_modules/@unimodules/react-native-adapter/build/NativeModulesProxy.native.js:15:23 in moduleName.methodInfo.name
at node_modules/expo-analytics-segment/build/Segment.js:13:8 in initialize
at App.tsx:42:6 in useEffect$argument_0
at [native code]:null in performSyncWorkOnRoot
at node_modules/react-native/Libraries/ReactNative/renderApplication.js:54:4 in renderApplication
at node_modules/react-native/Libraries/ReactNative/AppRegistry.js:117:25 in runnables.appKey.run
at node_modules/react-native/Libraries/ReactNative/AppRegistry.js:213:4 in runApplication
at [native code]:null in callFunctionReturnFlushedQueue

Relevant code:

1. $ expo install expo-analytics-segment  -> ( "expo-analytics-segment": "~10.1.1" )
2. Relevant code on App.tsx
import * as Segment from 'expo-analytics-segment';


export default function App() {
 
  // Initialize Segment and track app start

  useEffect(() => {

      Segment.initialize({androidWriteKey, iosWriteKey});
      Segment.track("App.start");
      console.log("segment initialized");

  }, []);

Just called my attention that searching for this issue I found this for the original segment non-expo react-native library: [Android] opt-out of new lifecycle methods by prayansh · Pull Request #256 · segmentio/analytics-react-native · GitHub

3 Likes

Related issue:

https://github.com/expo/expo/issues/13128