maui
maui copied to clipboard
[Android, .NET MAUI 9.0 Preview 5] When building with Visual Studio 17.11.0 Preview 2.1, Maui fails randomly to import the fonts, but still finishes building without errors.
Description
Randomly, when building a Maui app with Visual Studio in Windows, the Android version of Maui fails randomly to import the fonts, but still builds apparently fine. Other versions (iOS, Windows) seem to always build fine. This can be especially problematic with release builds going to Google Play Store if the fonts are left out of the package without any errors or warnings.
Steps to Reproduce
Not sure, since it happens very randomly, but still consistently every now and then.
Link to public reproduction project repository
https://github.com/hyvanmielenpelit/GnollHack
Version with bug
9.0.0-preview.4.10690
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 14
Did you find any workaround?
Delete obj and bin directories and hope that the fonts are included next time when building.
Relevant log output
No response
Hi I'm an AI powered bot that finds similar issues based off the issue title.
Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!
Open similar issues:
- Copy font assets only works at second build (#23268), similarity score: 0.72
Closed similar issues:
- Sometimes .ttf files are missing in the application after the apk or aab build (#22806), similarity score: 0.76
- [BUG] On Android (when also connected to Mac), resizetizer does not often run on time on first rebuild / empty obj directory, leading to processing of AndroidManifest failing (Visual Studio 17.10.0 Preview 2 / .NET 9.0 Preview 2.0) (#21542), similarity score: 0.73
- Build fails due to problems with Android manifest generation. (#21545), similarity score: 0.72
- Font not found in Android, but works well in Windows. (#19596), similarity score: 0.72
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
Possibly related/duplicate #23268
I'm pretty sure this is related to the very unreliably working resizetizer on Android. Fonts are copied (for whatever reason) to obj/Debug/net9.0/resizetizer/f directory and this seems to fail frequently along with many other resizetizer tasks on Android.
The workaround is not to use resizetizer with fonts on Android, but rather copy them to the right location using MauiAsset, as follows:
<MauiFont Include="Resources\Fonts\*" Condition="'$(TargetFramework)'!='net9.0-android'" />
<MauiAsset Include="Resources\Fonts\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" Condition="'$(TargetFramework)'=='net9.0-android'" />
This is happening in .NET 8.0 now also. It only happens on Debug mode, not Release for me.
Something has changed. This only started in the last few weeks for me.
Deleting bin and obj sometimes works. I have only had it happen ~5 times so I can't pin down what I'm doing if anything to trigger or fix it.
But I am not in .NET 9.0 so it is certainly not a .NET 9.0 only issue.
I am using
<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.61" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.61" />
</ItemGroup>
This started happening for me with the latest service release (8.0.90) at the time of writing, but only in Release mode. In Debug mode, fonts are loaded and displayed correctly on Android. It works fine with the previous release, so I had to roll back to an earlier version to create a release build for a client.
In my particular case, it's not random, though, the fonts simply don't get included in the APK, the file is smaller than it should be and thus, the app only shows an "X" where a font icon should be and all other text is displayed with the default system font.
We are seeing the same thing with SR9 @ewerspej and reverting is not a great option for us because it fixes other issues. The workaround above (thanks @janne-hmp!) seems to have worked though if you didn't try it.
Not sure where MS is with fixing this, but my guess is that this results (along with many other similar issues on Android) from VS 2022 IDE launching design-time build exactly at the same time as when the user selects Rebuild command in VS, which leads to two same build processes running at the same time, causing random sharing violations. If you build the app with dotnet build using command-line tool with Visual Studio closed, fonts are most likely always included correctly. It is also worth noting that if you keep Visual Studio open in the same configuration as where you run dotnet build (e.g., both in Release), then Visual Studio will also start design-time build that causes sharing violations with the dotnet build process, so make sure that Visual Studio is either closed or set in a configuration different to the configuration used in dotnet build (e.g. Debug if dotnet build is Release, or vice versa).
We are seeing the same thing with SR9 @ewerspej and reverting is not a great option for us because it fixes other issues. The workaround above (thanks @janne-hmp!) seems to have worked though if you didn't try it.
I'm glad the workaround works for you, I haven't tried that one yet. While workarounds are okay sometimes, this would be the kind that is easily forgotten.
I just reverted to the previous version, which works just fine for me.
The error is still here. Periodically, font files are missing in *.msix assemblies and in unpacked assemblies for Windows Visual Studio Community 2022 v17.11.5
Microsoft.Maui.FontRegistrar: Warning: Unable to load font 'MaterialFont'.
System.IO.FileNotFoundException: Native font with the name materialdesignicons-webfont.ttf was not found.
at Microsoft.Maui.FontRegistrar.LoadNativeAppFont(String font, String filename, String alias)
at Microsoft.Maui.FontRegistrar.GetFont(String font)
Microsoft.Maui.FontManager: Error: Error loading font 'Assets/Fonts/MaterialFont.ttf'.
System.InvalidOperationException: This operation is not supported for a relative URI.
at System.Uri.get_AbsolutePath()
at Microsoft.Maui.FontManager.FindFontFamilyName(String fontFile)
Microsoft.Maui.FontManager: Error: Error loading font 'Assets/Fonts/MaterialFont.otf'.
System.InvalidOperationException: This operation is not supported for a relative URI.
at System.Uri.get_AbsolutePath()
at Microsoft.Maui.FontManager.FindFontFamilyName(String fontFile)
<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.61" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.61" />
</ItemGroup>
Actually I'm going to close this one as a duplicate so we won't have issues and info all over the place. I'll look into this soon.
Duplicate of #23268
@janne-hmp The workaround is working for NET 8 too like this:
<MauiAsset Include="Resources\Fonts\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.100" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.100" />
On 8.0.20 fonts were working fine. Now workaround is needed.