diff --git a/CHANGELOG.md b/CHANGELOG.md index 99beca8409..5956af1b8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Add Mac Catalyst target ([#1848](https://github.com/getsentry/sentry-dotnet/pull/1848)) - Add `Distribution` properties ([#1851](https://github.com/getsentry/sentry-dotnet/pull/1851)) - Add and configure options for the iOS SDK ([#1849](https://github.com/getsentry/sentry-dotnet/pull/1849)) +- Set default `Release` and `Distribution` for iOS and Android ([#1856](https://github.com/getsentry/sentry-dotnet/pull/1856)) ### Fixes diff --git a/src/Sentry/Platforms/Android/SentrySdk.cs b/src/Sentry/Platforms/Android/SentrySdk.cs index f7343b73cd..1fcbf91652 100644 --- a/src/Sentry/Platforms/Android/SentrySdk.cs +++ b/src/Sentry/Platforms/Android/SentrySdk.cs @@ -1,3 +1,5 @@ +using Android.Content.PM; +using Android.OS; using Sentry.Android; using Sentry.Android.Callbacks; using Sentry.Android.Extensions; @@ -42,6 +44,10 @@ private static void InitSentryAndroidSdk(SentryOptions options) options.AutoSessionTracking = true; options.IsGlobalModeEnabled = true; + // Set default release and distribution + options.Release ??= GetDefaultReleaseString(); + options.Distribution ??= GetDefaultDistributionString(); + // "Best" mode throws permission exception on Android options.DetectStartupTime = StartupTimeDetectionMode.Fast; @@ -192,4 +198,49 @@ private static void AndroidEnvironment_UnhandledExceptionRaiser(object? _, Raise Close(); } } + + private static string? GetDefaultReleaseString() + { + var context = AndroidContext ?? Application.Context; + var packageName = context.PackageName; + if (packageName == null) + { + return null; + } + + var packageInfo = context.PackageManager?.GetPackageInfo(packageName, PackageInfoFlags.Permissions); + return packageInfo == null ? null : $"{packageName}@{packageInfo.VersionName}+{packageInfo.GetVersionCode()}"; + } + + private static string? GetDefaultDistributionString() => GetAndroidPackageVersionCode()?.ToString(); + + private static long? GetAndroidPackageVersionCode() + { + var context = AndroidContext ?? Application.Context; + var packageName = context.PackageName; + if (packageName == null) + { + return null; + } + + var packageInfo = context.PackageManager?.GetPackageInfo(packageName, PackageInfoFlags.Permissions); + return packageInfo?.GetVersionCode(); + } + + private static long? GetVersionCode(this PackageInfo packageInfo) + { + // The value comes from different property depending on Android version + if (AndroidBuild.VERSION.SdkInt >= BuildVersionCodes.P) + { +#pragma warning disable CA1416 + // callsite only reachable on Android >= P (28) + return packageInfo.LongVersionCode; +#pragma warning restore CA1416 + } + +#pragma warning disable CS0618 + // obsolete on Android >= P (28) + return packageInfo.VersionCode; +#pragma warning restore CS0618 + } } diff --git a/src/Sentry/Platforms/iOS/SentrySdk.cs b/src/Sentry/Platforms/iOS/SentrySdk.cs index 097b1dadb2..f1ba0e5f47 100644 --- a/src/Sentry/Platforms/iOS/SentrySdk.cs +++ b/src/Sentry/Platforms/iOS/SentrySdk.cs @@ -18,6 +18,10 @@ private static void InitSentryCocoaSdk(SentryOptions options) options.AutoSessionTracking = true; options.IsGlobalModeEnabled = true; + // Set default release and distribution + options.Release ??= GetDefaultReleaseString(); + options.Distribution ??= GetDefaultDistributionString(); + // "Best" mode throws platform not supported exception. Use "Fast" mode instead. options.DetectStartupTime = StartupTimeDetectionMode.Fast; @@ -174,4 +178,17 @@ private static void InitSentryCocoaSdk(SentryOptions options) // TODO: Pause/Resume } + + private static string GetDefaultReleaseString() + { + var packageName = GetBundleValue("CFBundleIdentifier"); + var packageVersion = GetBundleValue("CFBundleShortVersionString"); + var buildVersion = GetBundleValue("CFBundleVersion"); + + return $"{packageName}@{packageVersion}+{buildVersion}"; + } + + private static string GetDefaultDistributionString() => GetBundleValue("CFBundleVersion"); + + private static string GetBundleValue(string key) => NSBundle.MainBundle.ObjectForInfoDictionary(key).ToString(); }