Skip to content

Commit

Permalink
[android] eliminate lots of fields in Resource.designer.cs
Browse files Browse the repository at this point in the history
Context: https://github.com/jonathanpeppers/CustomResourceDesigner
Context: dotnet/android#6310

We found a case where a .NET MAUI app that included SkiaSharp and
Telerik controls was setting ~90,000 fields at startup in the
`Resource.designer.cs` file via the `UpdateIdValues()` method.

This also would happen in any Xamarin.Android class library:

* Include AndroidX & Google Material
* Include at least one `@(AndroidResource)` and use the ID from C#
* `Resource.designer.cs` has 2,700+ fields

So SkiaSharp is not unique to the problem here...

Reviewing the SkiaSharp fields, I found:

    source\SkiaSharp.Views.Maui\SkiaSharp.Views.Maui.Controls\obj\Debug\net6.0-android\Resource.designer.cs
    5335 fields
    source\SkiaSharp.Views.Maui\SkiaSharp.Views.Maui.Core\obj\Debug\net6.0-android\Resource.designer.cs
    5313 fields
    source\SkiaSharp.Views.Maui\SkiaSharp.Views.Maui.Controls.Compatibility\obj\Debug\net6.0-android\Resource.designer.cs
    5335 fields

We can simply set `$(AndroidGenerateResourceDesigner)` to `false` in
these projects, as they do not use any of the fields. This will
prevent .NET 6 apps using these libraries from setting 16,005 fields
at startup.

I also did a spot check on `SkiaSharp.Views.Android`, but it's OK --
it only has 3 fields:

    public class Resource
    {
        public class Attribute
        {
            public static int ignorePixelScaling;
            //...
        }
        public class Styleable
        {
            public static int[] SKCanvasView;
            public static int SKCanvasView_ignorePixelScaling;
            //...
        }
        //...
    }

We are working on a long-term solution for this issue in
Xamarin.Android, but we can do a simple workaround in SkiaSharp for
now.
  • Loading branch information
jonathanpeppers committed Sep 20, 2021
1 parent 68e80fa commit d744a28
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<DefineConstants>$(DefineConstants);__MAUI__</DefineConstants>
<PackagingPlatform Condition="$(TargetFramework.Contains('-windows'))">net6.0-windows</PackagingPlatform>
<UseMaui>true</UseMaui>
<AndroidGenerateResourceDesigner>false</AndroidGenerateResourceDesigner>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\binding\SkiaSharp\SkiaSharp.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<DefineConstants>$(DefineConstants);__MAUI__</DefineConstants>
<PackagingPlatform Condition="$(TargetFramework.Contains('-windows'))">net6.0-windows</PackagingPlatform>
<UseMaui>true</UseMaui>
<AndroidGenerateResourceDesigner>false</AndroidGenerateResourceDesigner>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\binding\SkiaSharp\SkiaSharp.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<DefineConstants>$(DefineConstants);__MAUI__</DefineConstants>
<PackagingPlatform Condition="$(TargetFramework.Contains('-windows'))">net6.0-windows</PackagingPlatform>
<UseMauiCore>true</UseMauiCore>
<AndroidGenerateResourceDesigner>false</AndroidGenerateResourceDesigner>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\binding\SkiaSharp\SkiaSharp.csproj" />
Expand Down

0 comments on commit d744a28

Please sign in to comment.