Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RichTextKit/SkiaSharp on Android: Could not resolve type with token 0100000b from typeref (expected class 'System.ReadOnlySpan`1' in assembly 'mscorlib') #5142

Closed
mikegoatly opened this issue Mar 8, 2022 · 8 comments
Labels
area-drawing Shapes, Borders, Shadows, Graphics, BoxView, custom drawing platform/android 🤖 s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version t/bug Something isn't working
Milestone

Comments

@mikegoatly
Copy link

Description

When running a MAUI project on Android using RichTextKit to render text on a SkiaSharp SKCanvasView control the following exception is thrown:

System.TypeLoadException
  Message=Could not resolve type with token 0100000b from typeref (expected class 'System.ReadOnlySpan`1' in assembly 'mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e')

Repo here: https://github.com/mikegoatly/MauiExperiments/tree/main/MauiRichTextKit

I'm guessing that code is being trimmed that's needed?

Steps to Reproduce

  1. Create a new MAUI project
  2. Add a package reference to SkiaSharp.Views.Maui.Controls
  3. Add .UseSkiaSharp() to MauiProgram
  4. Change MainPage.xaml to have this as content:
<Grid>
    <sk:SKCanvasView PaintSurface="SKCanvasView_PaintSurface"/>
</Grid>
  1. Add this to MainPage.xaml.cs:
private void SKCanvasView_PaintSurface(object sender, SkiaSharp.Views.Maui.SKPaintSurfaceEventArgs e)
{
  var text = new TextBlock();
  var styleManager = StyleManager.Default.Value;
  styleManager.FontSize(40);
  styleManager.HaloColor(SKColors.Red);
  styleManager.HaloWidth(4);
  styleManager.HaloBlur(2);
  
  text.AddText("Hello MAUI!", styleManager.CurrentStyle);
  
  text.Paint(e.Surface.Canvas, new SKPoint(100, 200));
}

Debug the app on Android (I'm using Pixel 5 - API 30 (Android 11.0 - API 30)) and get the error. Note that the app works fine when running as a Windows app.

Version with bug

Preview 13 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 11 and up

Did you find any workaround?

No response

Relevant log output

No response

@mikegoatly mikegoatly added s/needs-verification Indicates that this issue needs initial verification before further triage will happen t/bug Something isn't working labels Mar 8, 2022
@Amir-Care Amir-Care added the s/triaged Issue has been reviewed label Mar 9, 2022
@Amir-Care
Copy link

Amir-Care commented Mar 9, 2022

Verified repro with Version 17.2.0 Preview 2.0 [32208.484.main] on Android and can works fine on Windwos.

  • Throw exception:

    image

Sample project: #5142.zip

@Amir-Care Amir-Care added s/verified Verified / Reproducible Issue ready for Engineering Triage and removed s/triaged Issue has been reviewed labels Mar 9, 2022
@jsuarezruiz jsuarezruiz removed the s/needs-verification Indicates that this issue needs initial verification before further triage will happen label Mar 9, 2022
@Redth Redth added this to the 6.0.300-rc.2 milestone Mar 21, 2022
@Redth Redth added the area-drawing Shapes, Borders, Shadows, Graphics, BoxView, custom drawing label Mar 21, 2022
@mattleibow
Copy link
Member

I am wondering if you are using too old a SkiaSharp and RichTextKit? The latest version of vs brings maui p14 which is onky compatible with skia preview 232

@mikegoatly
Copy link
Author

@mattleibow I've just updated the repo project to use SkiaSharp.Views.Maui.Controls 2.88.0-preview.232 and still get the same error.

The sample code is already running on the latest available version of RichTextKit, and the callstack at the point of error is:

0x2B in Topten.RichTextKit.TextBlock.CreateFontRun at C:\Users\Brad\Projects\RichTextKit\Topten.RichTextKit\TextBlock.cs:1305,17
0x65 in Topten.RichTextKit.TextBlock.FlushUnshapedRuns at C:\Users\Brad\Projects\RichTextKit\Topten.RichTextKit\TextBlock.cs:1236,17
0x367 in Topten.RichTextKit.TextBlock.BuildFontRuns at C:\Users\Brad\Projects\RichTextKit\Topten.RichTextKit\TextBlock.cs:1129,17
0xF6 in Topten.RichTextKit.TextBlock.Layout at C:\Users\Brad\Projects\RichTextKit\Topten.RichTextKit\TextBlock.cs:313,17
0xB in Topten.RichTextKit.TextBlock.Paint at C:\Users\Brad\Projects\RichTextKit\Topten.RichTextKit\TextBlock.cs:359,13
0x1E in Topten.RichTextKit.TextBlock.Paint at C:\Users\Brad\Projects\RichTextKit\Topten.RichTextKit\TextBlock.cs:419,13
0x76 in MauiRichTextKit.MainPage.SKCanvasView_PaintSurface at C:\Users\mikeg\source\repos\MauiExperiments\MauiRichTextKit\MauiRichTextKit\MainPage.xaml.cs:25,2
0xD in SkiaSharp.Views.Maui.Controls.SKCanvasView.OnPaintSurface at D:\a\1\s\source\SkiaSharp.Views.Forms\SkiaSharp.Views.Forms.Shared\SKCanvasView.cs:74,4
0x2 in SkiaSharp.Views.Maui.Controls.SKCanvasView.SkiaSharp.Views.Maui.ISKCanvasView.OnPaintSurface at D:\a\1\s\source\SkiaSharp.Views.Maui\SkiaSharp.Views.Maui.Controls\SKCanvasView.HandlerImpl.cs:33,4
0x52 in SkiaSharp.Views.Maui.Handlers.SKCanvasViewHandler.OnPaintSurface at D:\a\1\s\source\SkiaSharp.Views.Maui\SkiaSharp.Views.Maui.Core\Handlers\SKCanvasView\SKCanvasViewHandler.Android.cs:68,4
0xD in SkiaSharp.Views.Android.SKCanvasView.OnPaintSurface at D:\a\1\s\source\SkiaSharp.Views\SkiaSharp.Views.Android\SKCanvasView.cs:134,4
0xB6 in SkiaSharp.Views.Android.SKCanvasView.OnDraw at D:\a\1\s\source\SkiaSharp.Views\SkiaSharp.Views.Android\SKCanvasView.cs:113,4
0x11 in Android.Views.View.n_OnDraw_Landroid_graphics_Canvas_ at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net6.0/android-32/mcw/Android.Views.View.cs:18211,4
0x9 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:110,5

Which is this line:

shaped = shaper.Shape(_textShapingBuffers, codePoints, style, direction, codePoints.Start, asFallbackFor, ResolveTextAlignment());

However because the error is a type resolution error, I don't think that line is actually to blame. Looking at what's going on in the TextShaper.Shape method that's being called, this is the first time I think the HarfBuzzSharp library will be invoked, so it's very possible that there's something being trimmed in that that's actually the problem here?

@softlion
Copy link
Contributor

softlion commented Jan 4, 2023

@mikegoatly have you resolved the issue ?
I have the same issue with another lib. It may be an issue with the current maui toolset.

The current project hierarchy is:
nuget net7 (release) lib => debug net7 lib project => debug maui android .exe project
=> The runtime error when running the android app is the same as you, it does not find a type in the nuget package.

So I tried referencing directly the nuget lib from it's source code.
debug net7 lib project => debug net7 lib project => debug maui android .exe project
=> And surprise, I get the same error. That means it's not a release issue.
The error is, a type from the leftmost lib is not found by the middle lib when the exe instantiate an object having this type as member.

I tried on both VS windows and Rider windows.
My conclusion is, the current maui toolset is not working as expected and not usable.

@sacOO7
Copy link

sacOO7 commented Feb 13, 2023

This is true, these people are planning to replace Xamarin with MAUI, yet MAUI is full of runtime bugs, mostly related to mscorlib.

@akoeplinger
Copy link
Member

akoeplinger commented Feb 23, 2023

The reason for this error is because the build is pulling in an assembly that was compiled against monoandroid and that hits an issue with the compatibility shim, see my explanation here: dotnet/runtime#82193 (comment)

The easiest solution is to add a net6.0 or later target to whichever nuget is causing the issue. I'll see if there is some other way we can improve this for net8.0

akoeplinger added a commit to akoeplinger/runtime that referenced this issue Feb 24, 2023
…b.dll

The legacy Xamarin.Android version of mscorlib.dll differed a bit compared to the .NET Framework mscorlib.dll, mostly because of additions for .NET Standard 2.1 support.

This meant that an assembly which was compiled against that mscorlib expects types there but since we didn't have type-forwarders in our mscorlib.dll shim to point them to the right assembly you'd get a TypeLoadException when running on modern .NET 6 Android.

Fixes dotnet#82193

Should also fix dotnet/maui#5142, dotnet/maui#5078, dotnet/maui#3903
@samhouts samhouts removed the s/verified Verified / Reproducible Issue ready for Engineering Triage label Apr 5, 2023
@XamlTest XamlTest added s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version labels Apr 13, 2023
@ghost
Copy link

ghost commented Apr 13, 2023

Hi @mikegoatly. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@XamlTest
Copy link

Verified this issue on Visual Studio Enterprise 17.6.0 Preview 3.0. This issue does not repro on Android emulator (13.0-API 33) with below Project: Uploading MauiRichTextKit.zip…
image

@ghost ghost closed this as completed Apr 20, 2023
@ghost ghost locked as resolved and limited conversation to collaborators May 20, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-drawing Shapes, Borders, Shadows, Graphics, BoxView, custom drawing platform/android 🤖 s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

10 participants