Skip to content

Commit a276045

Browse files
fix: Source context for class libraries when running on Android in Release mode (#4294)
Resolves: #4278 - #4278
1 parent de3258c commit a276045

File tree

12 files changed

+72
-57
lines changed

12 files changed

+72
-57
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixes
66

7+
- Source context for class libraries when running on Android in Release mode ([#4294](https://github.com/getsentry/sentry-dotnet/pull/4294))
78
- Native AOT: don't load SentryNative on unsupported platforms ([#4347](https://github.com/getsentry/sentry-dotnet/pull/4347))
89
- Fixed issue introduced in release 5.12.0 that might prevent other middleware or user code from reading request bodies ([#4373](https://github.com/getsentry/sentry-dotnet/pull/4373))
910

src/Sentry.Android.AssemblyReader/V2/AndroidAssemblyStoreReaderV2.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,19 @@ public static bool TryReadStore(string inputFile, IList<string> supportedAbis, D
100100
return assembly;
101101
}
102102

103+
// If the assembly name ends with .dll or .exe, try to find it without the extension.
103104
if ((IsFileType(".dll") || IsFileType(".exe")) && FindBestAssembly(name[..^4], out assembly))
104105
{
105106
return assembly;
106107
}
107108

109+
// Conversely, if there is no extension, try with the dll extension (sometimes required for class libraries).
110+
// See: https://github.com/getsentry/sentry-dotnet/issues/4278#issuecomment-2986009125
111+
if (!IsFileType(".dll") && !IsFileType(".exe") && FindBestAssembly(name + ".dll", out assembly))
112+
{
113+
return assembly;
114+
}
115+
108116
return null;
109117

110118
bool IsFileType(string extension)
@@ -119,10 +127,12 @@ private bool FindBestAssembly(string name, out ExplorerStoreItem? explorerAssemb
119127
{
120128
if (explorer.AssembliesByName?.TryGetValue(name, out var assembly) is true)
121129
{
130+
_logger?.Invoke("Found best assembly {0} in APK AssemblyStore for target arch {1}", name, explorer.TargetArch);
122131
explorerAssembly = new(explorer, assembly);
123132
return true;
124133
}
125134
}
135+
_logger?.Invoke("No best assembly for {0} in APK AssemblyStore", name);
126136
explorerAssembly = null;
127137
return false;
128138
}

src/Sentry.Android.AssemblyReader/V2/AssemblyStoreExplorer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ private AssemblyStoreExplorer(Stream storeStream, string path, DebugLogger? logg
3333
{
3434
foreach (var item in Assemblies)
3535
{
36+
logger?.Invoke("Assembly {0} indexed from AssemblyStore {1}", item.Name, path);
3637
dict.Add(item.Name, item);
3738
}
3839
}

src/Sentry.Maui.CommunityToolkit.Mvvm/SentryOptionsExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class SentryOptionsExtensions
1212
/// </summary>
1313
public static SentryMauiOptions AddCommunityToolkitIntegration(this SentryMauiOptions options)
1414
{
15-
options.AddDefaultEventBinder<MauiCommunityToolkitMvvmEventsBinder>();
15+
options.AddIntegrationEventBinder<MauiCommunityToolkitMvvmEventsBinder>();
1616
return options;
1717
}
1818
}

src/Sentry.Maui/Internal/MauiButtonEventsBinder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void Bind(VisualElement element, Action<BreadcrumbEvent> addBreadcrumb)
2121
/// <inheritdoc />
2222
public void UnBind(VisualElement element)
2323
{
24+
_addBreadcrumbCallback = null;
2425
if (element is Button button)
2526
{
2627
button.Clicked -= OnButtonOnClicked;

src/Sentry.Maui/Internal/MauiImageButtonEventsBinder.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ namespace Sentry.Maui.Internal;
33
/// <inheritdoc />
44
public class MauiImageButtonEventsBinder : IMauiElementEventBinder
55
{
6-
private Action<BreadcrumbEvent>? addBreadcrumbCallback;
6+
private Action<BreadcrumbEvent>? _addBreadcrumbCallback;
77

88
/// <inheritdoc />
99
public void Bind(VisualElement element, Action<BreadcrumbEvent> addBreadcrumb)
1010
{
11-
addBreadcrumbCallback = addBreadcrumb;
11+
_addBreadcrumbCallback = addBreadcrumb;
1212

1313
if (element is ImageButton image)
1414
{
@@ -21,6 +21,7 @@ public void Bind(VisualElement element, Action<BreadcrumbEvent> addBreadcrumb)
2121
/// <inheritdoc />
2222
public void UnBind(VisualElement element)
2323
{
24+
_addBreadcrumbCallback = null;
2425
if (element is ImageButton image)
2526
{
2627
image.Clicked -= OnButtonOnClicked;
@@ -31,11 +32,11 @@ public void UnBind(VisualElement element)
3132

3233

3334
private void OnButtonOnClicked(object? sender, EventArgs _)
34-
=> addBreadcrumbCallback?.Invoke(new(sender, nameof(ImageButton.Clicked)));
35+
=> _addBreadcrumbCallback?.Invoke(new(sender, nameof(ImageButton.Clicked)));
3536

3637
private void OnButtonOnPressed(object? sender, EventArgs _)
37-
=> addBreadcrumbCallback?.Invoke(new(sender, nameof(ImageButton.Pressed)));
38+
=> _addBreadcrumbCallback?.Invoke(new(sender, nameof(ImageButton.Pressed)));
3839

3940
private void OnButtonOnReleased(object? sender, EventArgs _)
40-
=> addBreadcrumbCallback?.Invoke(new(sender, nameof(ImageButton.Released)));
41+
=> _addBreadcrumbCallback?.Invoke(new(sender, nameof(ImageButton.Released)));
4142
}

src/Sentry.Maui/SentryMauiAppBuilderExtensions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@ public static MauiAppBuilder UseSentry(this MauiAppBuilder builder,
5656
services.AddSingleton<IConfigureOptions<SentryMauiOptions>, SentryMauiOptionsSetup>();
5757
services.AddSingleton<Disposer>();
5858

59-
// Resolve the configured options and register any element event binders from these
59+
// Add default event binders
60+
services.AddSingleton<IMauiElementEventBinder, MauiButtonEventsBinder>();
61+
services.AddSingleton<IMauiElementEventBinder, MauiImageButtonEventsBinder>();
62+
services.AddSingleton<IMauiElementEventBinder, MauiGestureRecognizerEventsBinder>();
63+
services.AddSingleton<IMauiElementEventBinder, MauiVisualElementEventsBinder>();
64+
65+
// Resolve the configured options and register any event binders that have been injected by integrations
6066
var options = new SentryMauiOptions();
6167
configureOptions?.Invoke(options);
62-
foreach (var eventBinder in options.DefaultEventBinders)
68+
foreach (var eventBinder in options.IntegrationEventBinders)
6369
{
6470
eventBinder.Register(services);
6571
}

src/Sentry.Maui/SentryMauiOptions.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@ public SentryMauiOptions()
2323
#if !PLATFORM_NEUTRAL
2424
CacheDirectoryPath = Microsoft.Maui.Storage.FileSystem.CacheDirectory;
2525
#endif
26-
AddDefaultEventBinder<MauiButtonEventsBinder>();
27-
AddDefaultEventBinder<MauiImageButtonEventsBinder>();
28-
AddDefaultEventBinder<MauiGestureRecognizerEventsBinder>();
29-
AddDefaultEventBinder<MauiVisualElementEventsBinder>();
3026
}
3127

32-
internal List<IMauiElementEventBinderRegistration> DefaultEventBinders { get; } = [];
28+
internal List<IMauiElementEventBinderRegistration> IntegrationEventBinders { get; } = [];
3329

34-
internal void AddDefaultEventBinder<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TEventBinder>()
30+
internal void AddIntegrationEventBinder<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TEventBinder>()
3531
where TEventBinder : class, IMauiElementEventBinder
3632
{
37-
DefaultEventBinders.Add(new MauiElementEventBinderRegistration<TEventBinder>());
33+
IntegrationEventBinders.Add(new MauiElementEventBinderRegistration<TEventBinder>());
3834
}
3935

4036
/// <summary>

test/Sentry.Maui.Device.TestApp/Sentry.Maui.Device.TestApp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@
100100
<ProjectReference Include="..\Sentry.Extensions.Logging.Tests\Sentry.Extensions.Logging.Tests.csproj"/>
101101
<ProjectReference Include="..\Sentry.Maui.Tests\Sentry.Maui.Tests.csproj"/>
102102
<ProjectReference Include="..\Sentry.Maui.CommunityToolkit.Mvvm.Tests\Sentry.Maui.CommunityToolkit.Mvvm.Tests.csproj" Condition="'$(_SentryIsNet9OrGreater)' == 'true'"/>
103+
<ProjectReference Include="..\..\src\Sentry.SourceGenerators\Sentry.SourceGenerators.csproj"
104+
OutputItemType="Analyzer"
105+
ReferenceOutputAssembly="false"/>
103106
</ItemGroup>
104107

105108
</Project>

test/Sentry.Maui.Device.TestApp/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static MauiApp CreateMauiApp()
1919
typeof(Sentry.Maui.CommunityToolkit.Mvvm.Tests.MauiCommunityToolkitMvvmEventsBinderTests).Assembly,
2020
#endif
2121
#if ANDROID
22-
typeof(Sentry.Android.AssemblyReader.Tests.AndroidAssemblyReaderTests).Assembly,
22+
typeof(Sentry.Android.AssemblyReader.Tests.AndroidAssemblyReaderTests).Assembly,
2323
#endif
2424
]);
2525
var appBuilder = MauiApp.CreateBuilder()

0 commit comments

Comments
 (0)