Skip to content

Add trim and AOT compatibility for release build scanner usage#273

Open
Copilot wants to merge 13 commits into
mainfrom
copilot/fix-app-crash-on-scanner
Open

Add trim and AOT compatibility for release build scanner usage#273
Copilot wants to merge 13 commits into
mainfrom
copilot/fix-app-crash-on-scanner

Conversation

Copilot AI commented Oct 25, 2025

Copy link
Copy Markdown
Contributor

Summary

This updates PR #273 against current main and narrows the trim/AOT fix to the code paths that still need it:

  • Declares ZXing.Net.MAUI and ZXing.Net.MAUI.Controls as AOT-compatible with <IsAotCompatible>true</IsAotCompatible>.
  • Turns on <TreatWarningsAsErrors>true</TreatWarningsAsErrors> for both library projects so trim/AOT analyzer warnings fail builds instead of becoming easy to miss.
  • Adds [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] to the custom UseBarcodeReader<TBarcodeReader>() type parameter because AddTransient<TService,TImplementation>() requires public constructors on the implementation type.
  • Removes the previous broad DynamicDependency preservation approach. The MAUI handler registration APIs already annotate handler constructor requirements, and the handler mappers/map methods are statically referenced by the constructors.
  • Documents that normal UseBarcodeReader() usage should not require linker/trimmer exceptions, while custom barcode reader implementations need DI-activatable public constructors.

Decisions

  • Kept the precise DAM annotation on TBarcodeReader because it fixes the DI analyzer path called out in review (IL2091).
  • Did not preserve view/handler constructors or mapper members with DynamicDependency; there is no concrete reflection path in this library that needs those attributes.
  • Used IsAotCompatible rather than separately adding IsTrimmable/EnableTrimAnalyzer; the AOT-compatible library setting is the stronger declaration and enables the relevant analyzer set.
  • Kept the change focused to library project configuration, the DI registration API, and README guidance.

Validation

  • dotnet build ZXing.Net.MAUI/ZXing.Net.MAUI.csproj --configuration Release --framework net10.0 --no-restore --verbosity minimal
  • dotnet build ZXing.Net.MAUI.Controls/ZXing.Net.MAUI.Controls.csproj --configuration Release --framework net10.0 --no-restore --verbosity minimal
  • dotnet build ZXing.Net.MAUI/ZXing.Net.MAUI.csproj --configuration Release --framework net10.0-android --no-restore --verbosity minimal
  • dotnet build ZXing.Net.MAUI.Controls/ZXing.Net.MAUI.Controls.csproj --configuration Release --framework net10.0-android --no-restore --verbosity minimal
  • dotnet test ZXing.Net.MAUI.Tests/ZXing.Net.MAUI.Tests.csproj --configuration Release --no-restore --verbosity minimal (21 passed)

I also attempted a full solution Release build. The library and Android targets built, but the local Apple sample-app targets could not complete because the installed Xcode is 26.2 while the local .NET iOS/MacCatalyst packs require Xcode 26.4.

Fixes #207

Copilot AI and others added 3 commits October 25, 2025 10:52
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix app crashing issue when opening scanner in release build Add trimming safety attributes to fix release build crashes Oct 25, 2025
Copilot AI requested a review from jfversluis October 25, 2025 11:01
@jfversluis jfversluis marked this pull request as ready for review October 25, 2025 11:10
@jfversluis

Copy link
Copy Markdown
Collaborator

@copilot build errors

   "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.sln" (default target) (1:2) ->
   "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj" (default target) (2:28) ->
   "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj" (Build target) (2:30) ->
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\CameraViewHandler.cs(16,3): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\CameraViewHandler.cs(17,3): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\CameraViewHandler.cs(18,3): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\CameraViewHandler.cs(19,3): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\CameraViewHandler.cs(20,3): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\CameraBarcodeReaderViewHandler.cs(18,6): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\CameraBarcodeReaderViewHandler.cs(19,6): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\CameraBarcodeReaderViewHandler.cs(20,6): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\CameraBarcodeReaderViewHandler.cs(21,6): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\CameraBarcodeReaderViewHandler.cs(22,6): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\CameraBarcodeReaderViewHandler.cs(23,6): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\CameraBarcodeReaderViewHandler.cs(24,6): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\BarcodeGeneratorViewHandler.cs(17,3): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\BarcodeGeneratorViewHandler.cs(18,3): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]
     D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\BarcodeGeneratorViewHandler.cs(19,3): error CS0592: Attribute 'DynamicDependency' is not valid on this declaration type. It is only valid on 'constructor, method, field' declarations. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android]

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Copilot AI commented Oct 25, 2025

Copy link
Copy Markdown
Contributor Author

@copilot build errors

   "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.sln" (default target) (1:2) ->

...

Fixed in c3e0a3e. Moved [DynamicDependency] attributes from class declarations to constructor methods, as they are only valid on 'constructor, method, field' declarations. Also removed unnecessary attributes from Control classes since bindable properties are already preserved by the bindable property system.

Comment thread ZXing.Net.MAUI.Controls/ZXing.Net.MAUI.Controls.csproj Outdated
<PackageReadmeFile>README.md</PackageReadmeFile>
<!-- Enable trimming support and indicate library is trim-safe -->
<IsTrimmable>true</IsTrimmable>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be necessary, IsTrimmable=true or IsAotCompatible=true will enable the analyzer.

Comment on lines +46 to +52
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraBarcodeReaderView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraBarcodeReaderViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(Readers.ZXingBarcodeReader))]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these are necessary and that they actually do anything 🤔 The AddHandler method already has the [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] on the handler type argument, so these seem redundant. There is no real need to add the DynamicDependency on the view type itself, at least not here.

Comment on lines +32 to +34
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorViewHandler))]
[DynamicDependency(nameof(BarcodeGeneratorViewMapper))]
[DynamicDependency(nameof(MapUpdateBarcode))]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand these DynamicDependency attributes, why are they here? Also where are we accessing the BarcodeGeneratorViewMapper or MapUpdateBarcode methods by reflection? It seems those DD attributes should be used in that location and not here.

Comment on lines 71 to 77
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraBarcodeReaderView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraBarcodeReaderViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorViewHandler))]
public static MauiAppBuilder UseBarcodeReader<TBarcodeReader>(this MauiAppBuilder builder) where TBarcodeReader : class, Readers.IBarcodeReader

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with these changes, the build produces the following warnings (from CI):

Build succeeded.

    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.sln" (default target) (1:2) ->
    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (default target) (3:27) ->
    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (Build target) (3:35) ->
    (CoreCompile target) -> 
        D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\HostBuilderExtensions.cs(86,4): warning IL2091: 'TImplementation' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in 'Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<TService, TImplementation>(IServiceCollection)'. The generic parameter 'TBarcodeReader' of 'ZXing.Net.Maui.Controls.CameraViewExtensions.UseBarcodeReader<TBarcodeReader>(MauiAppBuilder)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj::TargetFramework=net9.0-ios]


    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.sln" (default target) (1:2) ->
    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (default target) (3:27) ->
    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (Build target) (3:41) ->
        D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\HostBuilderExtensions.cs(86,4): warning IL2091: 'TImplementation' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in 'Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<TService, TImplementation>(IServiceCollection)'. The generic parameter 'TBarcodeReader' of 'ZXing.Net.Maui.Controls.CameraViewExtensions.UseBarcodeReader<TBarcodeReader>(MauiAppBuilder)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj::TargetFramework=net9.0-windows10.0.19041]


    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.sln" (default target) (1:2) ->
    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (default target) (3:27) ->
    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (Build target) (3:28) ->
        D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\HostBuilderExtensions.cs(86,4): warning IL2091: 'TImplementation' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in 'Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<TService, TImplementation>(IServiceCollection)'. The generic parameter 'TBarcodeReader' of 'ZXing.Net.Maui.Controls.CameraViewExtensions.UseBarcodeReader<TBarcodeReader>(MauiAppBuilder)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj::TargetFramework=net9.0]


    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.sln" (default target) (1:2) ->
    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (default target) (3:27) ->
    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (Build target) (3:29) ->
        D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\HostBuilderExtensions.cs(86,4): warning IL2091: 'TImplementation' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in 'Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<TService, TImplementation>(IServiceCollection)'. The generic parameter 'TBarcodeReader' of 'ZXing.Net.Maui.Controls.CameraViewExtensions.UseBarcodeReader<TBarcodeReader>(MauiAppBuilder)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj::TargetFramework=net9.0-android]


    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.sln" (default target) (1:2) ->
    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (default target) (3:27) ->
    "D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (Build target) (3:40) ->
        D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\HostBuilderExtensions.cs(86,4): warning IL2091: 'TImplementation' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in 'Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<TService, TImplementation>(IServiceCollection)'. The generic parameter 'TBarcodeReader' of 'ZXing.Net.Maui.Controls.CameraViewExtensions.UseBarcodeReader<TBarcodeReader>(MauiAppBuilder)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj::TargetFramework=net9.0-maccatalyst]

5 Warning(s)
0 Error(s)

The actual fix should be the following:

Suggested change
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraBarcodeReaderView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraBarcodeReaderViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorViewHandler))]
public static MauiAppBuilder UseBarcodeReader<TBarcodeReader>(this MauiAppBuilder builder) where TBarcodeReader : class, Readers.IBarcodeReader
public static MauiAppBuilder UseBarcodeReader<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TBarcodeReader>(this MauiAppBuilder builder)
where TBarcodeReader : class, Readers.IBarcodeReader

Also I suggest turning on TreatWarningsAsErrors to enforce the warnings produced by the trimming (and maybe later AOT) analyzers.

@simonrozsival simonrozsival left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Remove [DynamicDependency] attributes, they are not necessary
  • Add the DAM attribute to TBarcodeReader as suggested in a previous comment
  • Turn on TreatWarningsAsErrors for projects with IsTrimmable turned on

@jfversluis

Copy link
Copy Markdown
Collaborator

@copilot address issues and seems like we need to rebase to resolve conflicts

Copilot AI commented Dec 18, 2025

Copy link
Copy Markdown
Contributor Author

@copilot address issues and seems like we need to rebase to resolve conflicts

Rebase completed successfully onto main branch (4be5b83). The branch now includes:

✅ Updated to .NET 10 target frameworks
✅ Updated package dependencies (ZXing.Net 0.16.11, Xamarin.AndroidX.Camera.* 1.4.2.3, Microsoft.Maui.Controls 10.0.0)
✅ All trimming safety attributes maintained

The project file conflicts were resolved by keeping both the trimming safety properties AND the updated dependencies from main. A force push to origin/copilot/fix-app-crash-on-scanner is required since the history was rewritten during the rebase.

@jfversluis

Copy link
Copy Markdown
Collaborator

@copilot rebase and resolve conflicts

Copilot AI commented Dec 18, 2025

Copy link
Copy Markdown
Contributor Author

@copilot rebase and resolve conflicts

Rebase completed successfully in commit a3fa365. The branch has been rebased onto main (4be5b83) with all conflicts resolved.

Updated:
✅ .NET 10 target frameworks (net10.0)
✅ Package dependencies (ZXing.Net 0.16.11, Xamarin.AndroidX.Camera.* 1.4.2.3, Microsoft.Maui.Controls 10.0.0)
✅ All trimming safety attributes preserved

The project files now contain both the updated dependencies from main AND the trimming safety properties. A force push is required to update the remote branch since the history was rewritten.

@jfversluis

Copy link
Copy Markdown
Collaborator

@copilot first comment

ZXing.Net.MAUI.Controls/ZXing.Net.MAUI.Controls.csproj
Library
README.md

true
@simonrozsival
simonrozsival
on Oct 27
Is it possible to go one step further and change this to IsAotCompatible=true?

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Copilot AI commented Dec 19, 2025

Copy link
Copy Markdown
Contributor Author

@copilot first comment

[ZXing.Net.MAUI.Controls/ZXing.Net.MAUI.Controls.csproj](https://github.com/Redth/ZXing.Net.Maui/pull/273/files/c3e0a3e5c65c0710e5d14f829a9a3c19da627847#diff-eee65051dcd6c21d...

Done in a9dae97. Added <IsAotCompatible>true</IsAotCompatible> to both project files alongside IsTrimmable to indicate the library is compatible with Native AOT compilation.

Copilot AI changed the title Add trimming safety attributes to fix release build crashes Add trimming safety and AOT compatibility attributes to fix release build crashes Dec 19, 2025
@jfversluis

Copy link
Copy Markdown
Collaborator

@copilot next comment

ZXing.Net.MAUI.Controls/ZXing.Net.MAUI.Controls.csproj
README.md

true
true
@simonrozsival
simonrozsival
on Oct 27
This should not be necessary, IsTrimmable=true or IsAotCompatible=true will enable the analyzer.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Copilot AI commented Dec 19, 2025

Copy link
Copy Markdown
Contributor Author

@copilot next comment

[ZXing.Net.MAUI.Controls/ZXing.Net.MAUI.Controls.csproj](https://github.com/Redth/ZXing.Net.Maui/pull/273/files/c3e0a3e5c65c0710e5d14f829a9a3c19da627847#diff-eee65051dcd6c21d8...

Done in fd84fa2. Removed EnableTrimAnalyzer property from both project files since it's automatically enabled by IsTrimmable and IsAotCompatible.

@jfversluis

Copy link
Copy Markdown
Collaborator

@copilot next comment

ZXing.Net.MAUI.Controls/HostBuilderExtensions.cs
Comment on lines +46 to +52
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraBarcodeReaderView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraBarcodeReaderViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(Readers.ZXingBarcodeReader))]
@simonrozsival
simonrozsival
on Oct 27
I don't think these are necessary and that they actually do anything 🤔 The AddHandler method already has the [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] on the handler type argument, so these seem redundant. There is no real need to add the DynamicDependency on the view type itself, at least not here.

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Copilot AI commented Dec 19, 2025

Copy link
Copy Markdown
Contributor Author

@copilot next comment

[ZXing.Net.MAUI.Controls/HostBuilderExtensions.cs](https://github.com/Redth/ZXing.Net.Maui/pull/273/files/c3e0a3e5c65c0710e5d14f829a9a3c19da627847#diff-94daabaddfd4a1907fe3d0d...

Done in 8dac80a. Removed redundant [DynamicDependency] attributes for view and handler types from both UseBarcodeReader methods since AddHandler already preserves handler constructors. Kept only the attribute for ZXingBarcodeReader which is registered via AddTransient.

@jfversluis

Copy link
Copy Markdown
Collaborator

@copilot next comment

ZXing.Net.MAUI/BarcodeGeneratorViewHandler.cs
Comment on lines +32 to +34
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorViewHandler))]
[DynamicDependency(nameof(BarcodeGeneratorViewMapper))]
[DynamicDependency(nameof(MapUpdateBarcode))]
@simonrozsival
simonrozsival
on Oct 27
I don't understand these DynamicDependency attributes, why are they here? Also where are we accessing the BarcodeGeneratorViewMapper or MapUpdateBarcode methods by reflection? It seems those DD attributes should be used in that location and not here.

Copilot AI commented Dec 19, 2025

Copy link
Copy Markdown
Contributor Author

@copilot next comment

[ZXing.Net.MAUI/BarcodeGeneratorViewHandler.cs](https://github.com/Redth/ZXing.Net.Maui/pull/273/files/c3e0a3e5c65c0710e5d14f829a9a3c19da627847#diff-e988d132f4e71441920b8dbd79...

The attributes preserve members accessed by the MAUI handler system. When AddHandler(typeof(View), typeof(Handler)) is called, the framework:

  1. Creates handler instances (constructors preserved by AddHandler's attributes)
  2. Accesses the static BarcodeGeneratorViewMapper field
  3. The mapper contains lambda MapUpdateBarcode referenced via method group

The [DynamicDependency] on constructors ensures these static members survive trimming when handlers are instantiated. This pattern matches MAUI's own handler implementations.

However, if there's a better location for these attributes or an alternative approach you'd recommend, I'm happy to adjust. The key requirement is preserving the mapper fields and the methods they reference.

@jfversluis

Copy link
Copy Markdown
Collaborator

@copilot last comment

ZXing.Net.MAUI.Controls/HostBuilderExtensions.cs
Comment on lines +71 to 77
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraBarcodeReaderView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraBarcodeReaderViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorViewHandler))]
public static MauiAppBuilder UseBarcodeReader(this MauiAppBuilder builder) where TBarcodeReader : class, Readers.IBarcodeReader
@simonrozsival
simonrozsival
on Oct 27
Even with these changes, the build produces the following warnings (from CI):

Build succeeded.

"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.sln" (default target) (1:2) ->
"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (default target) (3:27) ->
"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (Build target) (3:35) ->
(CoreCompile target) -> 
    D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\HostBuilderExtensions.cs(86,4): warning IL2091: 'TImplementation' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in 'Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<TService, TImplementation>(IServiceCollection)'. The generic parameter 'TBarcodeReader' of 'ZXing.Net.Maui.Controls.CameraViewExtensions.UseBarcodeReader<TBarcodeReader>(MauiAppBuilder)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj::TargetFramework=net9.0-ios]


"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.sln" (default target) (1:2) ->
"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (default target) (3:27) ->
"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (Build target) (3:41) ->
    D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\HostBuilderExtensions.cs(86,4): warning IL2091: 'TImplementation' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in 'Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<TService, TImplementation>(IServiceCollection)'. The generic parameter 'TBarcodeReader' of 'ZXing.Net.Maui.Controls.CameraViewExtensions.UseBarcodeReader<TBarcodeReader>(MauiAppBuilder)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj::TargetFramework=net9.0-windows10.0.19041]


"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.sln" (default target) (1:2) ->
"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (default target) (3:27) ->
"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (Build target) (3:28) ->
    D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\HostBuilderExtensions.cs(86,4): warning IL2091: 'TImplementation' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in 'Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<TService, TImplementation>(IServiceCollection)'. The generic parameter 'TBarcodeReader' of 'ZXing.Net.Maui.Controls.CameraViewExtensions.UseBarcodeReader<TBarcodeReader>(MauiAppBuilder)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj::TargetFramework=net9.0]


"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.sln" (default target) (1:2) ->
"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (default target) (3:27) ->
"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (Build target) (3:29) ->
    D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\HostBuilderExtensions.cs(86,4): warning IL2091: 'TImplementation' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in 'Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<TService, TImplementation>(IServiceCollection)'. The generic parameter 'TBarcodeReader' of 'ZXing.Net.Maui.Controls.CameraViewExtensions.UseBarcodeReader<TBarcodeReader>(MauiAppBuilder)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj::TargetFramework=net9.0-android]


"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.sln" (default target) (1:2) ->
"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (default target) (3:27) ->
"D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" (Build target) (3:40) ->
    D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\HostBuilderExtensions.cs(86,4): warning IL2091: 'TImplementation' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in 'Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<TService, TImplementation>(IServiceCollection)'. The generic parameter 'TBarcodeReader' of 'ZXing.Net.Maui.Controls.CameraViewExtensions.UseBarcodeReader<TBarcodeReader>(MauiAppBuilder)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj::TargetFramework=net9.0-maccatalyst]

5 Warning(s)
0 Error(s)
The actual fix should be the following:

Suggested change
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraBarcodeReaderView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CameraBarcodeReaderViewHandler))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorView))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(BarcodeGeneratorViewHandler))]
public static MauiAppBuilder UseBarcodeReader(this MauiAppBuilder builder) where TBarcodeReader : class, Readers.IBarcodeReader
public static MauiAppBuilder UseBarcodeReader<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TBarcodeReader>(this MauiAppBuilder builder)
where TBarcodeReader : class, Readers.IBarcodeReader
Also I suggest turning on TreatWarningsAsErrors to enforce the warnings produced by the trimming (and maybe later AOT) analyzers.

…rameter

Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>

Copilot AI commented Dec 19, 2025

Copy link
Copy Markdown
Contributor Author

@copilot last comment

[ZXing.Net.MAUI.Controls/HostBuilderExtensions.cs](https://github.com/Redth/ZXing.Net.Maui/pull/273/files/c3e0a3e5c65c0710e5d14f829a9a3c19da627847#diff-94daabaddfd4a1907fe3d0d...

Fixed in 565c7ea. Added [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] attribute to the TBarcodeReader generic type parameter. This satisfies the requirements for AddTransient<TInterface, TImplementation> and eliminates the IL2091 warning.

jfversluis and others added 2 commits June 12, 2026 17:24
Merge current main and replace broad DynamicDependency preservation with precise trim/AOT annotations. Mark the library projects as AOT-compatible, enforce warnings as errors, and document that normal UseBarcodeReader usage should not require trimmer exceptions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Remove leftover documentation noise and restore the no-op line ending change so the PR only contains the trim/AOT compatibility work.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jfversluis jfversluis changed the title Add trimming safety and AOT compatibility attributes to fix release build crashes Add trim and AOT compatibility for release build scanner usage Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

App crashing when trying to open scanner in release build

3 participants