Error compiling .NET9 project in release mode for Android

I use the latest verison of Visual Studio (17.14.16). I create a new MAUI project targeting .NET9 and using NuGet manager I add "TMS.FlexCel" version 7.25 (previously I had 7.23 and had the same issues). If I compile for Android in debug mode it works. If I switch to Release mode it fails with the below two errors:

Precompiling failed for C:\Users\mariusrusu\Desktop\MauiApp1\obj\Release\net9.0-android\android-arm64\linked\System.Private.Windows.Core.dll with exit code -1073741819.
Mono Ahead of Time compiler - compiling assembly C:\Users\mariusrusu\Desktop\MauiApp1\obj\Release\net9.0-android\android-arm64\linked\System.Private.Windows.Core.dll
AOTID 9471C855-6C71-E4FA-FA68-DAE0D68D7E0D
Using profile data file 'C:\Users\mariusrusu.nuget\packages\microsoft.maui.controls.build.tasks\9.0.82\buildTransitive\netstandard2.0\maui.aotprofile'
Using profile data file 'C:\Users\mariusrusu.nuget\packages\microsoft.maui.controls.build.tasks\9.0.82\buildTransitive\netstandard2.0\maui-sc.aotprofile'
Using profile data file 'C:\Users\mariusrusu.nuget\packages\microsoft.maui.controls.build.tasks\9.0.82\buildTransitive\netstandard2.0\maui-blazor.aotprofile'
Added 0 methods from profile.
Added 0 methods from profile.
Added 0 methods from profile.
Could not load signature of System.Windows.Forms.Nrbf.SerializationRecordExtensions+TryGetDelegate:BeginInvoke due to: Could not load file or assembly 'System.Formats.Nrbf, Version=9.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.
Precompiling failed for C:\Users\mariusrusu\Desktop\MauiApp1\obj\Release\net9.0-android\android-x64\linked\System.Private.Windows.Core.dll with exit code -1073741819.
Mono Ahead of Time compiler - compiling assembly C:\Users\mariusrusu\Desktop\MauiApp1\obj\Release\net9.0-android\android-x64\linked\System.Private.Windows.Core.dll
AOTID 828205EA-53D2-4491-853A-D30FBD223221
Using profile data file 'C:\Users\mariusrusu.nuget\packages\microsoft.maui.controls.build.tasks\9.0.82\buildTransitive\netstandard2.0\maui.aotprofile'
Using profile data file 'C:\Users\mariusrusu.nuget\packages\microsoft.maui.controls.build.tasks\9.0.82\buildTransitive\netstandard2.0\maui-sc.aotprofile'
Using profile data file 'C:\Users\mariusrusu.nuget\packages\microsoft.maui.controls.build.tasks\9.0.82\buildTransitive\netstandard2.0\maui-blazor.aotprofile'
Added 0 methods from profile.
Added 0 methods from profile.
Added 0 methods from profile.
Could not load signature of System.Windows.Forms.Nrbf.SerializationRecordExtensions+TryGetDelegate:BeginInvoke due to: Could not load file or assembly 'System.Formats.Nrbf, Version=9.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.

If I add the below code to my csproj file, it compiles succesfully in Release mode:

	<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net9.0-android|AnyCPU'">
	  <RunAOTCompilation>false</RunAOTCompilation>
	</PropertyGroup>

What should I do/try?

Thanks

Hi,
This looks related to .NET 9 removing binaryformatter. We don't use it directly, but it looks like skiasharp (which we use) is using it.
See Building -f net9.0-android -c Release fails due to missing package System.Formats.Nrbf · Issue #27057 · dotnet/maui · GitHub

The things you might try:

  1. Verify skiasharp is in the latest version. Nuget will always use the minimum version for dependencies, so FlexCel 7.25 requires at least 3.119.0, but if you don't reference skiasharp anywhere else, you will get 3.119.0, not the latest. You can get the latest by adding a manual dependency to skiasharp in your project, and updating it. I don't think this will solve it, but might be worth trying. In Could not find `System.Formats.Nrbf` · Issue #3212 · mono/SkiaSharp · GitHub they say they can't repro with 3.119 but it isn't the exact same problem (here it is with iOS)

  2. You might try the workaround mentioned in the first issue above: Add System.Formats.Nrbf manually to your project via nuget.

Let me know if any of those option fixes it. I will investigate more on our side, but as said, we don't really use BinaryFormatter in our code, and we can't control what our deps use.

Yes, my SkiaSharp is 3.119.0
Adding "System.Formats.Nrbf" does solve the issue.
Many thanks for your reply!