analytics-react-native: Android "Duplicate analytics client created"

This is a common issue on segment libraries for Android. The issue is that when you have already initialized the segment module on Android you cannot initialize it again without the app crashing.

This happens a lot when doing React Native development on Android, as running the Reload function (shake your device and press Reload) will cause this crash.

One solution is to guard the initialization on Android. While the doc of this project says you can catch errors when setting up, it doesn’t seem to be working right now.

Here are some related issues in other repos with the same error: https://github.com/leoilab/react-native-analytics-segment-io/issues/3 https://github.com/segmentio/analytics-android/issues/538

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 32
  • Comments: 31 (2 by maintainers)

Commits related to this issue

Most upvoted comments

@fathyb Hate to bother, but we’re excited about this fix too. Let us know if you have a timeline?

The problem is annoying 😦

@fathyb Any updates?

I have contacted the support directly to explain the problem as this repository does not seems to have activity. I waited 10 days for an answer that is something like “It’s in the ticket system, I am waiting for an ETA”

I suggest every concerned people to contact support to make these issue more visible and make the Segment devs to do more active support.

Thank you @anandwahed ! I already forked this repo to have a fixed version.

Thanks to crashlytics I detected this issue in production. They are raised because I use code push to deploy OTA. When the app is reloaded with the new version, the analytics.setup is called again, and my clients enjoy a new crash. I am not totally sure if this is the only scenario where this library crashes in production.

It would be great if you handle this native error and then raise it to JS to handle it there.

Thanks !

👍 to having this error with CodePush. Unpleasant choosing between our delivery mechanism, and our analytics tracking mechanism.

@f2prateek trying that, yarn gives the error error Can't add "@internal/analytics-react-native": invalid package version undefined. @snackvideo-dkeith’s fork works (thank you!), though I’d rather not ship a production version of our app based on an unpublished third-party fork! Any word on this fix being published?

@fathyb will the fix be merged anytime soon or is there any other workaround for this issue during the app development?

Me too comment. And also feels in some strange situation we get this in production app

Should I be worried about using this in production?

In my experience I haven’t had any issues in production, only when reloading during development.

One very quick solution is to do something like this:

--- a/android/src/main/java/com/segment/analytics/reactnative/core/RNAnalyticsModule.kt
+++ b/android/src/main/java/com/segment/analytics/reactnative/core/RNAnalyticsModule.kt
@@ -69,9 +69,13 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
             builder.logLevel(Analytics.LogLevel.VERBOSE)
         }
 
-        Analytics.setSingletonInstance(
-            RNAnalytics.buildWithIntegrations(builder)
-        )
+        try {
+            Analytics.setSingletonInstance(
+                RNAnalytics.buildWithIntegrations(builder)
+            )
+        } catch(e: Exception) {
+            // Ignore, most likely error during development
+        }
     }
 
     @ReactMethod

With this I haven’t had any issues in our builds, neither during development or production. I can’t 100 % be sure this won’t ever break in production, but our numbers seems to match that this is fine to do.

You can of course also expand this by adding some more error logging if you want to.

When using appcenter codepush : this causes the app crash 😦

Same here if you could please let us know when the fix has been merged.

I came across this bug for iOS, using React Native. Error triggered when trying to use recordScreenViews: true or trackAppLifecycleEvents: true.

Solution: If you are encountering this error for React Native, and you are using CocoaPods, make sure you are not installing both Analytics and RNAnalytics in your podfile.

Having both declared seems to cause the duplicate analytics client bug.

@sejas the solution posted by @cristeahub https://github.com/segmentio/analytics-react-native/issues/16#issuecomment-439326062 works fine for us. Till not we haven’t seen any issues in the production as well.

We are seeing another crash in our production system.

This is what we do https://github.com/segmentio/analytics-react-native/issues/16#issuecomment-439326062 . Decide for yourselves if you only want to apply this patch in dev or not.