Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error with ConcurrentHashMap on Android <= 9 #1761

Merged
merged 2 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Fix error with `ConcurrentHashMap` on Android <= 9 ([#1761](https://github.com/getsentry/sentry-dotnet/pull/1761))

## 3.19.0

### Features
Expand Down
4 changes: 3 additions & 1 deletion src/Sentry/Android/Extensions/BreadcrumbExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ internal static class BreadcrumbExtensions
{
public static Breadcrumb ToBreadcrumb(this Java.Breadcrumb breadcrumb)
{
var data = breadcrumb.Data.ToDictionary(x => x.Key, x => x.Value?.ToString() ?? "");
var data = breadcrumb.Data
.WorkaroundKeyIteratorBug()
.ToDictionary(x => x.Key, x => x.Value?.ToString() ?? "");

return new(breadcrumb.Timestamp.ToDateTimeOffset(),
breadcrumb.Message,
Expand Down
18 changes: 18 additions & 0 deletions src/Sentry/Android/Extensions/JavaExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Collections;
using Android.OS;

namespace Sentry.Android.Extensions;

internal static class JavaExtensions
Expand All @@ -9,4 +12,19 @@ internal static class JavaExtensions
public static Exception ToException(this Throwable throwable) => Throwable.ToException(throwable);

public static Throwable ToThrowable(this Exception exception) => Throwable.FromException(exception);

public static IDictionary<TKey, TValue> WorkaroundKeyIteratorBug<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
// Workaround for https://github.com/getsentry/sentry-dotnet/issues/1751
// Java.Lang.Error: no non-static method "Ljava/util/concurrent/ConcurrentHashMap$KeyIterator;.hasNext()Z"
// Affects Android 9 ("Pie") and older (API 28)

if (AndroidBuild.VERSION.SdkInt > BuildVersionCodes.P)
mattjohnsonpint marked this conversation as resolved.
Show resolved Hide resolved
{
return dictionary;
}

var map = new JavaHashMap((IDictionary)dictionary);
return new JavaDictionary<TKey, TValue>(map.Handle, JniHandleOwnership.DoNotRegister);
}
}
1 change: 1 addition & 0 deletions src/Sentry/Android/Sentry.Android.props
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<Using Include="Java.IO.InputStreamReader" Alias="JavaInputStreamReader" />
<Using Include="Java.IO.OutputStreamWriter" Alias="JavaOutputStreamWriter" />
<Using Include="Java.Util.Date" Alias="JavaDate" />
<Using Include="Java.Util.HashMap" Alias="JavaHashMap" />
</ItemGroup>

<Target Name="DownloadSentryAndroidSdk" BeforeTargets="CollectPackageReferences">
Expand Down