uno: [SVG - Android] Failed to load SVG image - System.Xml.XmlException: Invalid character in the given encoding.

Current behavior

Failed to load SVG image - System.Xml.XmlException: Invalid character in the given encoding happening when loading the MainPage of the sample app on Android.

[Choreographer] Skipped 54 frames!  The application may be doing too much work on its main thread.
[monodroid-assembly] open_from_bundles: failed to load assembly Svg.Model.dll
[monodroid-assembly] open_from_bundles: failed to load assembly ShimSkiaSharp.dll
Loaded assembly: /data/data/com.companyname.UnoSvgSample/files/.__override__/Svg.Model.dll [External]
Loaded assembly: /data/data/com.companyname.UnoSvgSample/files/.__override__/ShimSkiaSharp.dll [External]
[monodroid-assembly] open_from_bundles: failed to load assembly System.Runtime.InteropServices.RuntimeInformation.dll
Loaded assembly: /data/data/com.companyname.UnoSvgSample/files/.__override__/System.Runtime.InteropServices.RuntimeInformation.dll [External]
[monodroid-assembly] open_from_bundles: failed to load assembly Svg.Custom.dll
Loaded assembly: /data/data/com.companyname.UnoSvgSample/files/.__override__/Svg.Custom.dll [External]
[monodroid-assembly] open_from_bundles: failed to load assembly ExCSS.dll
Loaded assembly: /data/data/com.companyname.UnoSvgSample/files/.__override__/ExCSS.dll [External]
[DOTNET] fail: Uno.UI.Svg.SvgProvider[0]
[DOTNET]       Failed to load SVG image.
[DOTNET]       System.Xml.XmlException: Invalid character in the given encoding. Line 1, position 1.
[DOTNET]          at System.Xml.XmlTextReaderImpl.Throw(Exception e)
[DOTNET]          at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
[DOTNET]          at System.Xml.XmlTextReaderImpl.Throw(Int32 pos, String res)
[DOTNET]          at System.Xml.XmlTextReaderImpl.InvalidCharRecovery(Int32& bytesCount, Int32& charsCount)
[DOTNET]          at System.Xml.XmlTextReaderImpl.GetChars(Int32 maxCharsCount)
[DOTNET]          at System.Xml.XmlTextReaderImpl.ReadData()
[DOTNET]          at System.Xml.XmlTextReaderImpl.SwitchEncoding(Encoding newEncoding)
[DOTNET]          at System.Xml.XmlTextReaderImpl.SwitchEncodingToUTF8()
[DOTNET]          at System.Xml.XmlTextReaderImpl.ParseXmlDeclaration(Boolean isTextDecl)
[DOTNET]          at System.Xml.XmlTextReaderImpl.Read()
[DOTNET]          at System.Xml.XmlTextReader.Read()
[DOTNET]          at Svg.SvgTextReader.Read() in /_/externals/SVG/Source/SvgTextReader.cs:line 66
[DOTNET]          at Svg.SvgDocument.Create[SvgDocument](XmlReader reader) in /_/externals/SVG/Source/SvgDocument.cs:line 363
[DOTNET]          at Svg.SvgDocument.Open[SvgDocument](Stream stream, Dictionary`2 entities) in /_/externals/SVG/Source/SvgDocument.cs:line 299
[DOTNET]          at Svg.Model.SvgExtensions.Open(Stream stream) in /_/src/Svg.Model/SvgExtensions.IO.cs:line 329
[DOTNET]          at Svg.Skia.SKSvg.Load(Stream stream) in /_/src/Svg.Skia/SKSvg.Model.cs:line 63
[DOTNET]          at Uno.UI.Svg.SvgProvider.<>c__DisplayClass22_0.<LoadSvgAsync>b__0()

Expected behavior

The application should not throw an exception while using an SVG file for Image control like it is currently working on Windows, WASM, Skia, and iOS.

How to reproduce it (as minimally and precisely as possible)

  • Open the sample app attached here (UnoSvgSample.zip)
  • Build and launch the app for Android
  • Noticed the exception mentioned above in the output when the MainPage is displayed and that the SVG is not visible like on the other platforms

In MainPage.xaml

    <Image Source="ms-appx:///UnoSvgSample/Assets/unologo.svg"
           Height="80"
           Width="80" />

In UnoSvgSample.csproj

  <ItemGroup>
    <Content Include="Assets\unologo.svg" />
  </ItemGroup>
  <ItemGroup>
    <None Remove="Assets\unologo.svg" />
  </ItemGroup>

Workaround

N/A

Works on UWP/WinUI

Yes

Environment

Uno.UI / Uno.UI.WebAssembly / Uno.UI.Skia

NuGet package version(s)

Uno.WinUI : 5.1.87

Affected platforms

Android

IDE

Visual Studio 2022

IDE version

17.9.2

Relevant plugins

No response

Anything else we need to know?

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (14 by maintainers)

Most upvoted comments

the linked PR will be breaking, so it has to wait for a major release, But it is not crucial one right now anyway.

I thought I closed this issue already… As I mentioned here the work and progress will be on Resizetizer project.

If so, was it properly QA with latest packages?

@agneszitte The latest package (dev) should be good to test it.

Any further updates, please use the linked issue

@agneszitte @pictos So after quite a bit of debugging I finally have some answers 😂 . In the end Resizetizer is indeed causing the problem. If you build the sample above and extract the apk, you will find that all of the SVGs are included not only as .svg in the root assets folder:

image

But Resizetizer also generates png versions of them in drawable folders, even though at least two of them are explicitly “removed” in the .csproj:

image

This is unfortunately a big problem because our code does the following:

image

FindResourceIdFromPath unfortunately does not include the file extension in the name which means it just searches for a resource with name unosvgandroidtests_assets_flowbite_opensource for example, so the condition of the if passes and it tries to read the svg from the png file -> which makes it fail, even though it should use the Android Asset instead. I could theoretically invert the if to give precedence to the asset instead, but there still are other issues:

  1. We need to generate the Android resource names including their extension (I will take care of this)
  2. We must add a way for the user to explicitly ignore a file or even better a wildcard pattern (like Assets/Svg/**.svg) and avoid processing it completely so that those drawables that could conflict will not be generated (plus so the apk does not contain duplicates unnecessarily when the user actually wants a SVG file) @pictos