Skip to content

Setup fails with 'writeKey must not be null or empty' #290

@danborthwick

Description

@danborthwick

Using @segment/analytics-react-native v1.4.3 and @segment/analytics-react-native-intercom v1.4.3.

We recently updated a seemingly unrelated library, react-native-purchases, to v4.1.0. With the update, our app shows a React Native red box on startup with the message 'writeKey must not be null or empty'.

CatalystInstanceImpl caught native exception
 java.lang.IllegalArgumentException: writeKey
 	at com.segment.analytics.Analytics$Builder.<init>(Analytics.java:108
 	at com.segment.analytics.Analytics.with(Analytics.java:175)
 	at com.segment.analytics.reactnative.core.RNAnalyticsModule
 	at com.segment.analytics.reactnative.core.RNAnalyticsModule.setup(RNAnalyticsModule.kt:207)
 	at java.lang.reflect.Method.invoke(Native Method)
 	at com.facebook.react.bridge.JavaMethodWrapper.in
 	at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:151)
 	at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
 	at android.os.Handler.handleCallback(Handler.java:873)
 	at android.os.Handler.dispatchMessage(Handler.java:99)
 	at com.facebook.react.bridge.queue.MessageQueueThreadH
 	at android.os.Looper.loop(Looper.java:193)
 	at com.facebook.react.bridge.queue.Message
 	at java.lang.Thread.run(Thread.java:764)

Debugging RNAnalyticsModule.setup() confirms that writeKey is being passed with a valid value. It also reveals an IllegalStateException being caught and ignored (line 190). While the comment in that catch block reads pass if the error is due to calling setSingletonInstance multiple times. However, the exception has the message java.lang.IllegalStateException: Method addObserver must be called on the main thread_. So it would seem that setup()'s first attempt to initialise a client is silently failing, then when analytics.track() is called from trackApplicationLifecycleEvents(), a second attempt is made to initialise a client which fails, causing the red box error.

Noting that the addObserver() call was surrounded by if (useNewLifecycleMethods) { I tried adding

    android: {
      experimentalUseNewLifecycleMethods: false,

to the call to segment.setup() in RN. This has no effect as experimentalUseNewLifecycleMethods is not passed through by configuration.ts. Modifying the Java code to force this to true causes setup() to appear to complete successfully, though I've not verified if there are any other side-effects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions