Skip to content

Commit e3316a2

Browse files
committed
fix: deliver system breadcrumbs in the main thread on Android
1 parent a739982 commit e3316a2

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

src/Sentry.Bindings.Android/Transforms/Metadata.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
</method>
8686
</add-node>
8787

88+
<!-- Remove problematic deprecated methods in the IScopes interface -->
89+
<remove-node path="/api/package[@name='io.sentry']/interface[@name='IScopes']/method[@deprecated]" />
90+
8891
<!--
8992
TODO: If we need this, figure out how to multi-target or late bind.
9093
This API uses FrameMetrics, which requires Android >= 24.0. We currently target Android >= 21.0 which is the minimum supported by MAUI.

src/Sentry/Platforms/Android/AndroidDiagnosticLogger.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ namespace Sentry.Android;
55

66
internal class AndroidDiagnosticLogger : JavaObject, JavaSdk.ILogger
77
{
8-
private readonly IDiagnosticLogger _logger;
8+
private readonly IDiagnosticLogger? _logger;
99

10-
public AndroidDiagnosticLogger(IDiagnosticLogger logger) => _logger = logger;
10+
public AndroidDiagnosticLogger(IDiagnosticLogger? logger) => _logger = logger;
1111

1212
public void Log(JavaSdk.SentryLevel level, string message, JavaObject[]? args) =>
13-
_logger.Log(level.ToSentryLevel(), "Android: " + FormatJavaString(message, args));
13+
_logger?.Log(level.ToSentryLevel(), "Android: " + FormatJavaString(message, args));
1414

1515
public void Log(JavaSdk.SentryLevel level, string message, Throwable? throwable) =>
16-
_logger.Log(level.ToSentryLevel(), "Android: " + message, throwable);
16+
_logger?.Log(level.ToSentryLevel(), "Android: " + message, throwable);
1717

1818
public void Log(JavaSdk.SentryLevel level, Throwable? throwable, string message, params JavaObject[]? args) =>
19-
_logger.Log(level.ToSentryLevel(), "Android: " + FormatJavaString(message, args), throwable);
19+
_logger?.Log(level.ToSentryLevel(), "Android: " + FormatJavaString(message, args), throwable);
2020

2121
public bool IsEnabled(JavaSdk.SentryLevel? level) =>
22-
level != null && _logger.IsEnabled(level.ToSentryLevel());
22+
level != null && _logger != null && _logger.IsEnabled(level.ToSentryLevel());
2323

2424
private static string FormatJavaString(string s, JavaObject[]? args) =>
2525
args is null ? s : JavaString.Format(s, args);

src/Sentry/Platforms/Android/Sentry.Android.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<Using Include="Android.Runtime" />
1010
<Using Include="Android.Content.Context" Alias="AndroidContext" />
1111
<Using Include="Android.OS.Build" Alias="AndroidBuild" />
12+
<Using Include="Android.OS.Looper" Alias="AndroidLooper" />
13+
<Using Include="Android.OS.Handler" Alias="AndroidHandler" />
1214
<Using Include="Java.Lang.Boolean" Alias="JavaBoolean" />
1315
<Using Include="Java.Lang.Class" Alias="JavaClass" />
1416
<Using Include="Java.Lang.Double" Alias="JavaDouble" />

src/Sentry/Platforms/Android/SentrySdk.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using Sentry.Android.Callbacks;
55
using Sentry.Android.Extensions;
66
using Sentry.Extensibility;
7+
using Sentry.JavaSdk;
78
using Sentry.JavaSdk.Android.Core;
9+
using Sentry.JavaSdk.Android.Core.Internal.Util;
810

911
// Don't let the Sentry Android SDK auto-init, as we do that manually in SentrySdk.Init
1012
// See https://docs.sentry.io/platforms/android/configuration/manual-init/
@@ -155,6 +157,16 @@ private static void InitSentryAndroidSdk(SentryOptions options)
155157

156158
// Don't capture managed exceptions in the native SDK, since we already capture them in the managed SDK
157159
o.AddIgnoredExceptionForType(JavaClass.ForName("android.runtime.JavaProxyThrowable"));
160+
161+
// Deliver network and system event breadcrumbs in the main thread
162+
// See https://github.com/getsentry/sentry-dotnet/issues/3828
163+
var networkLogger = new AndroidDiagnosticLogger(options.DiagnosticLogger);
164+
var buildInfoProvider = new BuildInfoProvider(networkLogger);
165+
var timeProvider = AndroidCurrentDateProvider.Instance!;
166+
var mainHandler = new AndroidHandler(AndroidLooper.MainLooper!);
167+
o.ConnectionStatusProvider =
168+
new AndroidConnectionStatusProvider(AppContext, o, buildInfoProvider, timeProvider, mainHandler).JavaCast<IConnectionStatusProvider>();
169+
o.AddIntegration(new SystemEventsBreadcrumbsIntegration(AppContext, mainHandler).JavaCast<JavaSdk.IIntegration>());
158170
});
159171

160172
// Now initialize the Android SDK (with a logger only if we're debugging)

0 commit comments

Comments
 (0)