Unable to use SkiaSharp 3.116.0+ with Avalonia on iOS
Describe the bug
When using SkiaSharp versions above 2.88.9, the Avalonia iOS application crashes during the call to AppBuilder.Setup(). This does not occur when using SkiaSharp 2.88.9 or below.
Here is the call stack from the crash:
System.TypeInitializationException: The type initializer for 'SkiaSharp.SKFontManager' threw an exception.
---> System.TypeInitializationException: The type initializer for 'SkiaSharp.SKObject' threw an exception.
---> System.InvalidOperationException: The version of the native libSkiaSharp library (88.1) is incompatible with this version of SkiaSharp. Supported versions of the native libSkiaSharp library are in the range [119.0, 120.0).
at SkiaSharp.SkiaSharpVersion.CheckNativeLibraryCompatible(Version minSupported, Version current, Boolean throwIfIncompatible)
at SkiaSharp.SkiaSharpVersion.CheckNativeLibraryCompatible(Boolean throwIfIncompatible)
at SkiaSharp.SKObject..cctor()
--- End of inner exception stack trace ---
at SkiaSharp.SKFontManager..ctor(IntPtr handle, Boolean owns)
at SkiaSharp.SKFontManager.SKFontManagerStatic..ctor(IntPtr x)
at SkiaSharp.SKFontManager..cctor()
--- End of inner exception stack trace ---
at Avalonia.Skia.FontManagerImpl..ctor()
at Avalonia.Skia.SkiaPlatform.Initialize(SkiaOptions options)
at Avalonia.SkiaApplicationExtensions.<>c.<UseSkia>b__0_0()
at Avalonia.AppBuilder.SetupUnsafe()
at Avalonia.AppBuilder.Setup()
at Avalonia.AppBuilder.SetupWithoutStarting()
at Devolutions.RemoteDesktopManager.AppDelegate.FinishedLaunching(UIApplication app, NSDictionary options) in /opt/dev/RDM3/iOS/RemoteDesktopManager/Clients/RemoteDesktopManager/AppDelegate.cs:line 126
at Devolutions.RemoteDesktopManager.AppDelegate.__Registrar_Callbacks__.callback_338_Devolutions_RemoteDesktopManager_AppDelegate_FinishedLaunching(IntPtr pobj, IntPtr sel, IntPtr p0, IntPtr p1, IntPtr* exception_gchandle)
To Reproduce
To Reproduce
- Create a new Avalonia Cross Platform App using the default template.
- In the iOS project, add a reference to SkiaSharp 3.116.0 or newer.
- Build and deploy the app to an iOS simulator.
Result: The application crashes immediately on startup.
I’ve included a sample project demonstrating the issue. To reproduce AvaloniaSkiaIssueForIOS.zip:
- Open the solution.
- Build and deploy the iOS project to a simulator.
Optional Test:
- Downgrade SkiaSharp to 2.88.9.
- Clean the
binandobjfolders. - Rebuild and run the application — it should launch successfully.
Expected behavior
No response
Avalonia version
11.3.0
OS
iOS
Additional context
No response
Also, I saw some workraound for linux, how ever I could not make it work for iOS. https://github.com/mono/SkiaSharp/issues/3117#issuecomment-2896259517
https://github.com/AvaloniaUI/Avalonia/issues/15503#issuecomment-2622131305 https://github.com/AvaloniaUI/Avalonia/pull/18981
You're out of luck - SkiaSharp 3 is not supported with Avalonia 11.
Ahh thanks for the information.
SkiaSharp 3 has been supported as opt-in for at least half a year now. You must ensure that you also include the correct native assets for the platorm. ie. SkiaSharp.NativeAssets.iOS.
On a best-effort basis, as clearly evidenced by the fact that it doesn't work on iOS.
@stevemonaco I have tried with SkiaSharp.NativeAssets.iOS and I have the same issue. I have also tried playing with the TrimMode but still have not found a way to make it work.
@maxbrousseau Did you clean bin/obj before trying it to reference SkiaSharp.NativeAssets.iOS?
That first error is that it's trying to use the 2.x libraries, so the native assets need to match the version. But when I tried that on a fresh build with the referenced libraries, I got an error in MetalPlatformGraphics TryCreate, where GRContext.CreateMetal is null.
It would be interesting to try that PR to see if it's a general SkiaSharp 3 issue with UIKit platforms.
e. The code in the method I linked to will throw on SkiaSharp 3 since it's being disposed after being called. Removing it fixes it. I've posted about it in the PR.
Closing, as this issue is expected to be solved on latest nighly builds. Let me know if it's still failing.