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.

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