diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java index 9cc5cb3df0..2a60e5750d 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java @@ -471,14 +471,6 @@ private static void readDefaultOptionValues( options.addInAppInclude(packageName); } } - - if (options.getDistinctId() == null) { - try { - options.setDistinctId(Installation.id(context)); - } catch (RuntimeException e) { - options.getLogger().log(SentryLevel.ERROR, "Could not generate distinct Id.", e); - } - } } /** diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java index f27259fd63..150569dcec 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java @@ -149,6 +149,14 @@ public static void init( "Error in the 'OptionsConfiguration.configure' callback.", t); } + if (options.getDistinctId() == null + && options.getDataCollectionResolver().isUserInfoWithLegacyAlways()) { + try { + options.setDistinctId(Installation.id(context)); + } catch (RuntimeException e) { + options.getLogger().log(SentryLevel.ERROR, "Could not generate distinct Id.", e); + } + } // if SentryPerformanceProvider was disabled or removed, // we set the app start / sdk init time here instead diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt index f8724d286f..068b0964bc 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt @@ -111,6 +111,12 @@ class AndroidOptionsInitializerTest { ) sentryOptions.configureOptions() + if ( + sentryOptions.distinctId == null && + sentryOptions.dataCollectionResolver.isUserInfoWithLegacyAlways + ) { + sentryOptions.distinctId = Installation.id(if (useRealContext) context else mockContext) + } AndroidOptionsInitializer.initializeIntegrationsAndProcessors( sentryOptions, if (useRealContext) context else mockContext, @@ -349,6 +355,44 @@ class AndroidOptionsInitializerTest { installation.deleteOnExit() } + @Test + fun `init should not set generated distinct id when user info is disabled`() { + fixture.initSut(configureOptions = { dataCollection.setUserInfo(false) }) + + assertNull(fixture.sentryOptions.distinctId) + } + + @Test + fun `init should set generated distinct id when user info is enabled`() { + fixture.initSut(configureOptions = { dataCollection.setUserInfo(true) }) + + assertNotNull(fixture.sentryOptions.distinctId) + } + + @Test + fun `init should preserve explicit distinct id when user info is disabled`() { + fixture.initSut( + configureOptions = { + dataCollection.setUserInfo(false) + distinctId = "custom-id" + } + ) + + assertEquals("custom-id", fixture.sentryOptions.distinctId) + } + + @Test + fun `init should set generated distinct id when explicit value is null`() { + fixture.initSut( + configureOptions = { + dataCollection.setUserInfo(true) + distinctId = null + } + ) + + assertNotNull(fixture.sentryOptions.distinctId) + } + @Test fun `init should set proguard uuid id on start`() { fixture.initSut(