Trimming/Linker breaks SpriteFont loading

This can be easily reproduced by creating a new Android project (MonoGame, .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.

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:

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

Then reference that in your .csproj with:

	<TrimmerRootDescriptor Include="TrimmerRoots.xml" />

You, my friend, are a wizard! thank you

1 Like


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.


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