Trimming/Linker breaks SpriteFont loading

This can be easily reproduced by creating a new Android project (MonoGame 3.8.1.303, .NET6, Android 31), though I have the same issue with iOS. Add a new SpriteFont to your content (default Arial template is fine), and then try to build a release build (or enable the trimmer/linker on a debug build to actually see the error).

It will crash when attempting to load the SpriteFont with Could not find ContentTypeReader Type

I have tried to set the trim mode to SDK Only. I have tried to add a RootAssembly directive for MonoGame.Framework. I’ve also attempted it for Microsoft.Xna.Framework.Content.ContentTypeReader as I had read in another thread, but that errors out saying it cannot be found.

The only way I’ve been able to build my mobile builds is by disabling the trimmer/linker entirely.

1 Like

I believe I’ve found a solution for this. After ripping into the SpriteFont’s XNB, I discovered that the MG content loader uses System.Char by reflection when loading SpriteFonts. So, stop the linker from removing it. I tried a couple methods to only block the trimming of the System.Char type without luck. I would be interested if anyone else is doing this a bit cleaner. Here’s what you can do:

Create an XML file (ex: TrimmerRoots.xml) with this content:

<linker>
	<assembly fullname="System.Private.CoreLib" preserve="all" />
	<assembly fullname="mscorlib" preserve="all" />
</linker>

Then reference that in your .csproj with:

<ItemGroup>
	<TrimmerRootDescriptor Include="TrimmerRoots.xml" />
</ItemGroup>
4 Likes

You, my friend, are a wizard! thank you

1 Like

Hi,

I have tried the solution but did not work. Can you tell me what should be the Build Action for the file “TrimmerRoots.xml”?

It shouldn’t have a Build Action. Its only mention in your .csproj should be the TrimmerRootDescriptor.

Hi,

Its working now. I had to delete bin and obj folder and rebuild the project.
Thank you very much

1 Like

+1 from me, on the latest MG version with VS2022. Would not know where to start without your answer!

1 Like

I could not quite get this fix to work (I get the error below). However I simply set PublishTrimmed to false and that did the trick. I also had to delete the bin and obj folders before building. My build is not that much bigger with trimming disabled anyway. Thanks for your help!

XABBA7009: System.InvalidOperationException: Assembly list for ABI ‘arm64-v8a’ has a different number of assemblies than other ABI lists (expected 27, found 26
at Xamarin.Android.Tasks.ArchAssemblyStore.Generate(String outputDirectory, List1 globalIndex, List1 blobPaths)
at Xamarin.Android.Tasks.AssemblyStoreGenerator.Generate(String outputDirectory)
at Xamarin.Android.Tasks.BuildApk.AddAssemblies(ZipArchiveEx apk, Boolean debug, Boolean compress, IDictionary2 compressedAssembliesInfo, String assemblyStoreApkName) at Xamarin.Android.Tasks.BuildApk.ExecuteWithAbi(String[] supportedAbis, String apkInputPath, String apkOutputPath, Boolean debug, Boolean compress, IDictionary2 compressedAssembliesInfo, String assemblyStoreApkName)
at Xamarin.Android.Tasks.BuildApk.RunTask()
at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 17

Confirmed. This solution works. Thanks.

1 Like