Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] [Android] Intermittent error in production: Font not found /data/user/0/com.package.whatever/cache/font.ttf #15012

Open
greg84 opened this issue Dec 30, 2021 · 30 comments
Labels
a/fonts s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/unverified New report that has yet to be verified t/bug 🐛

Comments

@greg84
Copy link

greg84 commented Dec 30, 2021

Description

This exception is being reported intermittently, where the application seems to be unable to find the font file in the cache folder.

Error message:

Font not found /data/user/0/com.package.whatever/cache/font.ttf

Stack trace
JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args)
JniPeerMembers+JniStaticMethods.InvokeObjectMethod (System.String encodedMember, Java.Interop.JniArgumentValue* parameters)
Typeface.CreateFromFile (System.String path)
FontExtensions.TryGetFromAssets (System.String fontName)
FontExtensions.ToTypeFace (System.String fontfamily, Xamarin.Forms.FontAttributes attr)
FontExtensions.CreateTypeface (System.Tuple`2[T1,T2] key)
ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory)
FontExtensions.ToTypeface (System.String fontFamily, Xamarin.Forms.FontAttributes fontAttributes)
FontExtensions.ToTypeface (Xamarin.Forms.Font self)
LabelRenderer.UpdateFont ()
LabelRenderer.UpdateText ()
LabelRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1[TElement] e)
LabelRenderer.set_Element (Xamarin.Forms.Label value)
IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element)
Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context)
VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren)
VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement)
VisualElementPackager.Load ()
VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager)
VisualElementRenderer`1[TElement].SetElement (TElement element)
IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element)
Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context)
VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren)
VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement)
VisualElementPackager.Load ()
VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager)
VisualElementRenderer`1[TElement].SetElement (TElement element)
IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element)
Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context)
VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren)
VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement)
VisualElementPackager.Load ()
VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager)
VisualElementRenderer`1[TElement].SetElement (TElement element)
IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element)
Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context)
VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren)
VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement)
VisualElementPackager.Load ()
FrameRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1[TElement] e)
FrameRenderer.set_Element (Xamarin.Forms.Frame value)
IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element)
Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context)
VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren)
VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement)
VisualElementPackager.Load ()
VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager)
VisualElementRenderer`1[TElement].SetElement (TElement element)
IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element)
Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context)
VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren)
VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement)
VisualElementPackager.Load ()
VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager)
VisualElementRenderer`1[TElement].SetElement (TElement element)
IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element)
Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context)
ShellContentFragment.OnCreateView (Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_inflater, System.IntPtr native_container, System.IntPtr native_savedInstanceState)
(wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.51(intptr,intptr,intptr,intptr,intptr)
java.lang.RuntimeException: Font not found /data/user/0/com.packagename.redacted/cache/font.ttf
android.graphics.Typeface.createFromFile Typeface.java:332
crc643f46942d9dd1fff9.ShellContentFragment.n_onCreateView(Native Method)
crc643f46942d9dd1fff9.ShellContentFragment.onCreateView ShellContentFragment.java:69
androidx.fragment.app.Fragment.performCreateView Fragment.java:2963
androidx.fragment.app.FragmentStateManager.createView FragmentStateManager.java:518
androidx.fragment.app.FragmentStateManager.moveToExpectedState FragmentStateManager.java:282
androidx.fragment.app.FragmentManager.executeOpsTogether FragmentManager.java:2189
androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute FragmentManager.java:2100
androidx.fragment.app.FragmentManager.execPendingActions FragmentManager.java:2002
androidx.fragment.app.FragmentManager$5.run FragmentManager.java:524
android.os.Handler.handleCallback Handler.java:751
android.os.Handler.dispatchMessage Handler.java:95
android.os.Looper.loop Looper.java:186
android.app.ActivityThread.main ActivityThread.java:6513
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run ZygoteInit.java:914
com.android.internal.os.ZygoteInit.main ZygoteInit.java:804

Steps to Reproduce

  1. Add ExportFont assembly directive.
  2. Include ttf font file.
  3. Deploy an app to production

Expected Behavior

The app won't crash and fonts will always be loaded successfully.

Actual Behavior

I've seen this error reported twice in the last couple of weeks, both with similar stack traces as above.

Basic Information

  • Version with issue: 5.0.0.2291
  • Last known good version: n/a (didn't use fonts in previous app version)
  • Platform Target Frameworks:
    • iOS: 15.4.0.0
    • Android: 12.0.0.3
    • UWP: n/a
  • Android Support Library / AndroidX Version: n/a
  • NuGet Packages: Xamarin.Forms
  • Affected Devices: Reported on CPH1801 and Galaxy J6

Environment

Show/Hide Visual Studio info
Visual Studio Community 2019 for Mac
Version 8.10.16 (build 2)
Installation UUID: d84992da-d070-4eea-b6f4-ffdcfdf9880c
	GTK+ 2.24.23 (Raleigh theme)
	Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

	Package version: 612000162

Mono Framework MDK
Runtime:
	Mono 6.12.0.162 (2020-02/2ca650f1f62) (64-bit)
	Package version: 612000162

Roslyn (Language Service)
3.10.0-4.21269.26+029847714208ebe49668667c60ea5b0a294e0fcb

NuGet
Version: 5.9.0.7134

.NET SDK (x64)
SDK: /usr/local/share/dotnet/sdk/5.0.404/Sdks
SDK Versions:
	5.0.404
	5.0.403
	5.0.402
	5.0.401
	5.0.400
	5.0.302
	5.0.301
	5.0.203
	5.0.202
	5.0.201
	5.0.103
	5.0.102
	5.0.101
	5.0.100
	3.1.416
	3.1.415
	3.1.414
	3.1.413
	3.1.412
	3.1.411
	3.1.410
	3.1.409
	3.1.408
	3.1.407
	3.1.406
	3.1.405
	3.1.404
	3.1.403
	3.1.402
	3.1.401
	3.1.302
	3.0.100
	2.1.401
	2.1.4
	2.0.0
MSBuild SDKs: /Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/Sdks

.NET Core Runtime
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	5.0.13
	5.0.12
	5.0.11
	5.0.10
	5.0.9
	5.0.8
	5.0.7
	5.0.6
	5.0.5
	5.0.4
	5.0.3
	5.0.2
	5.0.1
	5.0.0
	3.1.22
	3.1.21
	3.1.20
	3.1.19
	3.1.18
	3.1.17
	3.1.16
	3.1.15
	3.1.14
	3.1.13
	3.1.12
	3.1.11
	3.1.10
	3.1.9
	3.1.8
	3.1.7
	3.1.6
	3.0.0
	2.1.23
	2.1.22
	2.1.21
	2.1.20
	2.1.3
	2.0.5
	2.0.0
	1.0.5

Xamarin.Profiler
Version: 1.6.15.68
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

Updater
Version: 11

Xamarin.Android
Version: 12.0.0.3 (Visual Studio Community)
Commit: xamarin-android/d16-11/f0e3c2d
Android SDK: /Users/greg/Library/Android/sdk
	Supported Android versions:
		None installed

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 30.0.4
SDK Build Tools Version: 30.0.2

Build Information: 
Mono: c633fe9
Java.Interop: xamarin/java.interop/d16-11@476bb5b
ProGuard: Guardsquare/proguard/v7.0.1@912d149
SQLite: xamarin/sqlite/3.35.4@85460d3
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-11@87af37b

Eclipse Temurin JDK
Java SDK: Not Found

Android SDK Manager
Version: 16.10.0.13
Hash: 1b81df5
Branch: remotes/origin/d16-10
Build date: 2021-11-12 01:17:32 UTC

Android Device Manager
Version: 16.10.0.15
Hash: 89dcc0b
Branch: remotes/origin/d16-10
Build date: 2021-11-12 01:17:52 UTC

Apple Developer Tools
Xcode 13.2.1 (19586)
Build 13C100

Xamarin.Mac
Version: 8.4.0.0 (Visual Studio Community)
Hash: 8fc41ae82
Branch: xcode13.2
Build date: 2021-12-09 01:07:56-0500

Xamarin.iOS
Version: 15.4.0.0 (Visual Studio Community)
Hash: 8fc41ae82
Branch: xcode13.2
Build date: 2021-12-09 01:07:56-0500

Xamarin Designer
Version: 16.11.0.39
Hash: cd672761d
Branch: remotes/origin/d16-11
Build date: 2021-10-06 21:09:18 UTC

Build Information
Release ID: 810160002
Git revision: 6dbc7886fa608147f685a7df3912249400c84716
Build date: 2021-12-14 15:54:37-05
Build branch: release-8.10

Operating System
Mac OS X 11.6.1
Darwin 20.6.0 Darwin Kernel Version 20.6.0
    Tue Oct 12 18:33:42 PDT 2021
    root:xnu-7195.141.8~1/RELEASE_X86_64 x86_64

Build Logs

Screenshots

Reproduction Link

Workaround

@greg84 greg84 added s/unverified New report that has yet to be verified t/bug 🐛 labels Dec 30, 2021
@greg84 greg84 changed the title [Bug] Intermittent error in production: Font not found /data/user/0/com.package.whatever/cache/font.ttf [Bug] [Android] Intermittent error in production: Font not found /data/user/0/com.package.whatever/cache/font.ttf Dec 30, 2021
@jfversluis
Copy link
Member

Do you have any additional code that does something with the fonts? How do you load/add the fonts to your project? Do you see this only on Android or also iOS?

@jfversluis jfversluis added the s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. label Dec 31, 2021
@greg84
Copy link
Author

greg84 commented Jan 1, 2022

Not doing anything special, just followed your guide here: https://devblogs.microsoft.com/xamarin/embedded-fonts-xamarin-forms/

I think the problem is that EmbeddedFontLoader for Android creates the font file in the cache folder:

var tmpdir = IOPath.GetTempPath();

The OS can clear the cache folder at any time, so we need to handle when the file doesn't exist by just creating the font file again. (https://developer.android.com/training/data-storage/app-specific#internal-create-cache)

I can submit a PR if you could talk me through your preferred fix (was thinking maybe move all the calls to CreateFromFile in FontExtensions into a method somewhere and call LoadFont again inside a catch?).

@jfversluis
Copy link
Member

But if it exists, we return it and else we try to move it there, that is already happening, right?

if (fileInfo.Exists && fileInfo.Length == font.ResourceStream.Length)
return (true, filePath);
try
{
using (var fileStream = File.Create(filePath))
{
font.ResourceStream.CopyTo(fileStream);
}
return (true, filePath);
}

@greg84
Copy link
Author

greg84 commented Jan 2, 2022

Yes, but it appears that FontRegistrar keeps a cache of loaded fonts, so the LoadFont method isn't called again after the first time:

Maybe bypass the cache on Android and call LoadFont every time? This would ensure the file always exists.

@greg84
Copy link
Author

greg84 commented Jan 7, 2022

Closing this as I've been unable to reproduce it. Will continue trying and open a new issue if necessary.

@greg84 greg84 closed this as completed Jan 7, 2022
@EmilAlipiev
Copy link
Contributor

thats rare but coming now and then, I have just got for android. Font is there its working fine but I dont get why this error is occuring even not very often. I am using the latest version of the XF and AndroidX libraries.

Android: 6.0.1
Android Build: MXB48T
Manufacturer: LGE
Model: LG-K220

Java.Lang.RuntimeException: Font not found /data/user/0/myapp.myapp/cache/Roboto-Bold.ttf
  at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in <6cf3794da10241fd9d34414fe7eafbd2>:0
  at Java.Interop.JniPeerMembers+JniStaticMethods.InvokeObjectMethod (System.String encodedMember, Java.Interop.JniArgumentValue* parameters) <0x9681a13c + 0x00077> in <6cf3794da10241fd9d34414fe7eafbd2>:0
  at Android.Graphics.Typeface.CreateFromFile (System.String path) [0x00029] in <5751d045d69b4cbbb7d00f614ccc9184>:0
  at Xamarin.Forms.Platform.Android.FontExtensions.TryGetFromAssets (System.String fontName) [0x00016] in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.FontExtensions.ToTypeFace (System.String fontfamily, Xamarin.Forms.FontAttributes attr) [0x0000c] in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.FontExtensions.CreateTypeface (System.Tuple`2[T1,T2] key) [0x0004b] in <04c545f414d24a37af95d995791bb9a9>:0
  at System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) <0x968ede8c + 0x000d7> in <05554d90551f418e8302a63853e107d6>:0
  at Xamarin.Forms.Platform.Android.FontExtensions.ToTypeface (System.String fontFamily, Xamarin.Forms.FontAttributes fontAttributes) [0x00018] in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.FontExtensions.ToTypeface (Xamarin.Forms.Font self) <0x956b3b68 + 0x0008f> in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.FastRenderers.LabelRenderer.UpdateFont () [0x0000b] in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.FastRenderers.LabelRenderer.UpdateText () [0x0011e] in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.FastRenderers.LabelRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1[TElement] e) [0x00086] in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.FastRenderers.LabelRenderer.set_Element (Xamarin.Forms.Label value) [0x00025] in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.FastRenderers.LabelRenderer.Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00015] in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) <0x956a9e90 + 0x00107> in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) <0x956c409c + 0x00227> in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) <0x956c45e8 + 0x003f3> in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) <0x956c8294 + 0x00063> in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) <0x956c653c + 0x00333> in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) <0x956c5688 + 0x00093> in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) <0x956a9e90 + 0x00107> in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) <0x956c409c + 0x00227> in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) <0x956c45e8 + 0x003f3> in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () <0x956c4048 + 0x00037> in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.FastRenderers.FrameRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1[TElement] e) [0x00098] in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.FastRenderers.FrameRenderer.set_Element (Xamarin.Forms.Frame value) [0x00025] in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.FastRenderers.FrameRenderer.Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00015] in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) <0x956a9e90 + 0x00107> in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) <0x956c409c + 0x00227> in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Platform.Android.VisualElementPackager.OnChildAdded (System.Object sender, Xamarin.Forms.ElementEventArgs e) [0x0000f] in <04c545f414d24a37af95d995791bb9a9>:0
  at Xamarin.Forms.Element.OnChildAdded (Xamarin.Forms.Element child) <0x955c0c00 + 0x000a7> in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0
  at Xamarin.Forms.VisualElement.OnChildAdded (Xamarin.Forms.Element child) <0x955f6818 + 0x00047> in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0
  at Syncfusion.ListView.XForms.ListViewItem.OnChildAdded (Xamarin.Forms.Element child) [0x00000] in <f031c2d0f0dc4e3a9f19bf76336fbc57>:0

@greg84
Copy link
Author

greg84 commented May 21, 2022

I'm going to reopen this, I have a reproduction scenario. I'll put something together and add it here.

I encountered this today during development on an emulator, so I've got a full stack trace with line numbers now, which is more useful.

My current theory is that the Dictionary used to cache the font files in FontRegistrar is preventing the file from being recreated because LoadFont is only ever called once - the first time it's needed (the stack trace I've got doesn't pass through that method so it doesn't check that the file exists).

The font files are being deleted while the app is running, the emulator was low on disk space when the crash occurred. It's likely that the few users who are being affected have a low amount of storage available on their device.

@uragonda-rajesh
Copy link

uragonda-rajesh commented Jul 8, 2022

Hi @greg84 ,

We see similar crashes (#15416) on our app, any ETA for the fix?

@varyamereon
Copy link

Seeing this issue too by adding a new font to a currently installed app. The font doesn't load and the error message detailed above is shown in the output.

@uragonda-rajesh
Copy link

Hi Team, any update on this issue?

@jherink
Copy link

jherink commented Apr 20, 2023

Also would like updates on this.

@kkppstudios
Copy link

My app is showing crashes from this issue as well

@darrabam
Copy link

I'm also getting this crash reported in AppCenter for few Android devices,
Any workaround?

@MitchBomcanhao
Copy link

MitchBomcanhao commented Jul 27, 2023

started seeing these crashes too (Font asset not found) - just from one user at the moment (Galaxy A22s 5G, on Android 13)... this is in code that has been in production for years and as far as I know we've not had these failures before. Maybe google have changed something recently which is causing these to be more common?

Edit: noticed we also had another on a Galaxy Tab A7 on Android 12

@kkppstudios
Copy link

kkppstudios commented Jul 27, 2023

Same here. Definitely a more common issue that I haven't seen before. All of my AppCenter crashes are coming from Android 13 devices. All Galaxy devices as well

Screenshot_20230727-110410

@Colinkl
Copy link

Colinkl commented Jul 27, 2023

Any ETA on fix?

@arvindrajachourasiya
Copy link

I am also facing same issue

@MitchBomcanhao
Copy link

now getting more and more of these crashes.

@tamasszadvari
Copy link

This is a critical issue for me also, as it makes production apps crash constantly. Please resolve it. Migrating to MAUI would be a lot bigger job, and out client won't will to pay for that, just because there's a framework bug in Xamarin.Forms.

@MitchBomcanhao
Copy link

the number of these crashes keeps going up for us...

@jherink
Copy link

jherink commented Aug 17, 2023

This is also a growing issue for us.

@tamasszadvari
Copy link

Try running your app in a really lowend emulator with low resources. For us it turned out that one of our ViewModels started a resource-heavy task on the main thread, which resulted in an out of memory exception. This crash about missing a font was just the simptom of this memory problem. (Possibly the device went out of memory, and to try to handle that, it cleared the cache also.) When I move that task to a background thread, the problem disappeared.

@DennisWelu
Copy link

Same here. Definitely a more common issue that I haven't seen before. All of my AppCenter crashes are coming from Android 13 devices. All Galaxy devices as well

Screenshot_20230727-110410

Similar experience for us - only has become an issue relatively recently for apps that are otherwise years old. Happening on Android 13 / Galaxy Tab devices...

@ArtjomP
Copy link

ArtjomP commented Sep 6, 2023

Any update on this?

@ArtjomP
Copy link

ArtjomP commented Sep 6, 2023

Add added the next code to my Android project:

[assembly: ExportRenderer(typeof(EmbeddedFont), typeof(MyEmbeddedFontLoader))]

namespace MyProject.Droid
{
    public class MyEmbeddedFontLoader : IEmbeddedFontLoader
    {
        public (bool success, string? filePath) LoadFont(EmbeddedFont font)
        {
            var tmpdir = Xamarin.Essentials.FileSystem.AppDataDirectory;
            var filePath = Path.Combine(tmpdir, font.FontName);
            var fileInfo = new FileInfo(filePath);
            if (fileInfo.Exists && fileInfo.Length == font.ResourceStream.Length)
                return (true, filePath);
            try
            {
                using (var fileStream = File.Create(filePath))
                {
                    font.ResourceStream.CopyTo(fileStream);
                }
                return (true, filePath);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
                File.Delete(filePath);
            }

            return (false, null);
        }
    }
}

@MitchBomcanhao
Copy link

Add added the next code to my Android project:

[assembly: ExportRenderer(typeof(EmbeddedFont), typeof(MyEmbeddedFontLoader))]

namespace MyProject.Droid
{
    public class MyEmbeddedFontLoader : IEmbeddedFontLoader
    {
        public (bool success, string? filePath) LoadFont(EmbeddedFont font)
        {
            var tmpdir = Xamarin.Essentials.FileSystem.AppDataDirectory;
            var filePath = Path.Combine(tmpdir, font.FontName);
            var fileInfo = new FileInfo(filePath);
            if (fileInfo.Exists && fileInfo.Length == font.ResourceStream.Length)
                return (true, filePath);
            try
            {
                using (var fileStream = File.Create(filePath))
                {
                    font.ResourceStream.CopyTo(fileStream);
                }
                return (true, filePath);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
                File.Delete(filePath);
            }

            return (false, null);
        }
    }
}

@ArtjomP that code is essentially exactly the same as the default font loader already in xamarin forms. how is that supposed to change things? [pardon me if i've missed something obvious - this is a genuine question, not a critique]

@ArtjomP
Copy link

ArtjomP commented Sep 18, 2023

Add added the next code to my Android project:

[assembly: ExportRenderer(typeof(EmbeddedFont), typeof(MyEmbeddedFontLoader))]

namespace MyProject.Droid
{
    public class MyEmbeddedFontLoader : IEmbeddedFontLoader
    {
        public (bool success, string? filePath) LoadFont(EmbeddedFont font)
        {
            var tmpdir = Xamarin.Essentials.FileSystem.AppDataDirectory;
            var filePath = Path.Combine(tmpdir, font.FontName);
            var fileInfo = new FileInfo(filePath);
            if (fileInfo.Exists && fileInfo.Length == font.ResourceStream.Length)
                return (true, filePath);
            try
            {
                using (var fileStream = File.Create(filePath))
                {
                    font.ResourceStream.CopyTo(fileStream);
                }
                return (true, filePath);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
                File.Delete(filePath);
            }

            return (false, null);
        }
    }
}

@ArtjomP that code is essentially exactly the same as the default font loader already in xamarin forms. how is that supposed to change things? [pardon me if i've missed something obvious - this is a genuine question, not a critique]

It uses Xamarin.Essentials.FileSystem.AppDataDirectory instead of temp folder. Android should not remove files from Xamarin.Essentials.FileSystem.AppDataDirectory.

@MitchBomcanhao
Copy link

Ah yes that makes perfect sense, thanks for clarifying.

@Colinkl
Copy link

Colinkl commented Sep 20, 2023

Although I found way to reproduce this issue.
Requirements: Samsung device with Android 13.
Steps:

  1. Open an app
  2. Quit app, app should be in background
  3. Open device settings, open app details
  4. Clear app cache
  5. Resume app
    After that app will crash.
    For some reason issue does not reproduces on Android Emulator and other android devices.

@tscholze
Copy link

I have no issues working in emulators, but as I start to test my app on an actual device, I also get such error.

Host: MacOS, Windows
IDE: Rider (MACOS), Visual Studio (Windows)
Android Device: Surface Duo 1

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/fonts s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/unverified New report that has yet to be verified t/bug 🐛
Projects
None yet
Development

No branches or pull requests