Skip to content

Commit

Permalink
Set default Release and Distribution for iOS and Android (#1856)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint authored Aug 17, 2022
1 parent ef06390 commit 7ff846f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 51 additions & 0 deletions src/Sentry/Platforms/Android/SentrySdk.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Android.Content.PM;
using Android.OS;
using Sentry.Android;
using Sentry.Android.Callbacks;
using Sentry.Android.Extensions;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
}
}
17 changes: 17 additions & 0 deletions src/Sentry/Platforms/iOS/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

0 comments on commit 7ff846f

Please sign in to comment.