KEMBAR78
Rework use of ObtainStyledAttributes so we can enable `AndroidLinkResources` by dellis1972 · Pull Request #4912 · dotnet/maui · GitHub
Skip to content

Conversation

@dellis1972
Copy link
Contributor

@dellis1972 dellis1972 commented Feb 25, 2022

Fixes #4889

Xamarin Android has a build property AndroidLinkResources which allows
any unused Resource.designer.cs code to be removed during the Link
process. In order to use this however we need to make sure that we do
NOT use any of the int[] fields from the Resource.designer.cs in
our C# code.

Currently we are using Resource.Styleable.Toolbar when we call
ObtainStyledAttributes. If we do not change this code the app will
crash when using AndroidLinkResources because the underlying field
Resource.Styleable.Toolbar will have been removed.

So lets move the code which obtains the ColorStateList to java and
provide a method which will then be bound as part of the maui.aar C#
binding. This way we can remove the C# usage of Resource.Styleable.Toolbar
and turn on AndroidLinkResources.

As a result we see a small improvement in startup times.

Before After Δ Notes
469.850 468.550 -0.28% ✓ defaults; 32-bit build
478.650 466.500 -2.54% ✓ defaults; profiled AOT; 32-bit build
490.450 480.450 -2.04% ✓ defaults; full AOT; 32-bit build
484.650 476.650 -1.65% ✓ defaults; 64-bit build
480.450 468.250 -2.54% ✓ defaults; profiled AOT; 64-bit build
477.800 471.550 -1.31% ✓ defaults; full AOT; 64-bit build

as well as a small reduction in package size.

Size difference in bytes ([*1] apk1 only, [*2] apk2 only):
  -          52 lib/armeabi-v7a/libaot-Xamarin.Kotlin.StdLib.Common.dll.so
  -          52 lib/x86/libaot-Xamarin.Kotlin.StdLib.Common.dll.so
  -          72 lib/arm64-v8a/libaot-Xamarin.Kotlin.StdLib.Common.dll.so
  -          80 lib/x86_64/libaot-Xamarin.Kotlin.StdLib.Common.dll.so
  -       4,096 lib/arm64-v8a/libaot-Microsoft.Maui.Controls.dll.so
  -       4,096 lib/arm64-v8a/libaot-Microsoft.Maui.dll.so
  -       4,096 lib/armeabi-v7a/libaot-Microsoft.Maui.Controls.dll.so
  -       4,096 lib/armeabi-v7a/libaot-Microsoft.Maui.dll.so
  -       4,096 lib/x86/libaot-Microsoft.Maui.Controls.dll.so
  -      24,576 lib/x86_64/libaot-BasicMauiApp.dll.so
  -      28,672 lib/arm64-v8a/libaot-BasicMauiApp.dll.so
  -      28,672 lib/armeabi-v7a/libaot-BasicMauiApp.dll.so
  -      28,672 lib/x86/libaot-BasicMauiApp.dll.so
  -     170,446 assemblies/assemblies.blob
Summary:
  -     170,446 Other entries -1.68% (of 10,148,006)
  +           0 Dalvik executables 0.00% (of 11,463,896)
  -     131,328 Shared libraries -0.27% (of 47,970,224)
  -     200,704 Package size difference -0.66% (of 30,356,729)

@@ -0,0 +1,3 @@
<resources>
<string name="maui_empty_unused"></string>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, if we have to put one, maybe it's worth putting library_name=.NET MAUI? I feel like I remember some convention for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And a comment, saying we need at least 1 resource.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with using library_name is that it might be used on other libraries. And depending on the order of import you might override something since the resources will overlay on top of each other.

I would prefer this be unique if possible so we don't trip over any existing resources.

@mattleibow
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@mattleibow mattleibow added this to the 6.0.300-rc.2 milestone Apr 1, 2022
@dellis1972
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Commenter does not have sufficient privileges for PR 4912 in repo dotnet/maui

@jonathanpeppers
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

Comment on lines -151 to +169
var a = TintTypedArray.ObtainStyledAttributes(nativeToolbar.Context?.GetThemedContext(), null, Resource.Styleable.Toolbar, Resource.Attribute.toolbarStyle, 0);
_defaultTitleTextColor = a.GetColorStateList(Resource.Styleable.Toolbar_titleTextColor);
a.Recycle();
var context = nativeToolbar.Context?.GetThemedContext ();
_defaultTitleTextColor = PlatformInterop.GetColorStateListForToolbarStyleableAttribute (context,
Resource.Attribute.toolbarStyle, Resource.Styleable.Toolbar_titleTextColor);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PureWeen what control should we test for this? Would we just need to test changing the toolbar color in a template using Shell?

Copy link
Member

@PureWeen PureWeen Apr 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior is currently in place for non-shell apps. Non-shell apps only ever have a single Toolbar so the purpose of this code was to reset the toolbar back to the default if you navigate away to a page that has a color set and then back to one that doesn't.

So the test here would be to use a navigation page and then push to a page that changes toolbar styles and then pop back to the page which is default, or setup navigation pages inside some tabbedpages and switch between the tabs.

@Redth Redth modified the milestones: 6.0.300-rc.2, 6.0.300-rc.3 Apr 20, 2022
…urces

Xamarin Android has a build property `AndroidLinkResources` which allows
any unused `Resource.designer.cs` code to be removed during the Link
process. In order to use this however we need to make sure that we do
NOT use any of the `int[]` fields from the `Resource.designer.cs` in
our C# code.

Currently we are using `Resource.Styleable.Toolbar` when we call
`ObtainStyledAttributes`. If we do not change this code the app will
crash when using `AndroidLinkResources` because the underlying field
`Resource.Styleable.Toolbar` will have been removed.

So lets move the code which obtains the `ColorStateList` to java and
provide a method which will then be bound as part of the `maui.aar` C#
binding. This way we can remove the C# usage of `Resource.Styleable.Toolbar`
and turn on `AndroidLinkResources`.

As a result we see a small improvement in startup times.

| Before  | After   | Δ        | Notes                                |
| ------- | ------- | -------- | ------------------------------------ |
| 469.850 | 468.550 | -0.28% ✓ | defaults; 32-bit build               |
| 478.650 | 466.500 | -2.54% ✓ | defaults; profiled AOT; 32-bit build |
| 490.450 | 480.450 | -2.04% ✓ | defaults; full AOT; 32-bit build     |
| 484.650 | 476.650 | -1.65% ✓ | defaults; 64-bit build               |
| 480.450 | 468.250 | -2.54% ✓ | defaults; profiled AOT; 64-bit build |
| 477.800 | 471.550 | -1.31% ✓ | defaults; full AOT; 64-bit build     |

as well as a small reduction in package size.

Size difference in bytes ([*1] apk1 only, [*2] apk2 only):
  -          52 lib/armeabi-v7a/libaot-Xamarin.Kotlin.StdLib.Common.dll.so
  -          52 lib/x86/libaot-Xamarin.Kotlin.StdLib.Common.dll.so
  -          72 lib/arm64-v8a/libaot-Xamarin.Kotlin.StdLib.Common.dll.so
  -          80 lib/x86_64/libaot-Xamarin.Kotlin.StdLib.Common.dll.so
  -       4,096 lib/arm64-v8a/libaot-Microsoft.Maui.Controls.dll.so
  -       4,096 lib/arm64-v8a/libaot-Microsoft.Maui.dll.so
  -       4,096 lib/armeabi-v7a/libaot-Microsoft.Maui.Controls.dll.so
  -       4,096 lib/armeabi-v7a/libaot-Microsoft.Maui.dll.so
  -       4,096 lib/x86/libaot-Microsoft.Maui.Controls.dll.so
  -      24,576 lib/x86_64/libaot-BasicMauiApp.dll.so
  -      28,672 lib/arm64-v8a/libaot-BasicMauiApp.dll.so
  -      28,672 lib/armeabi-v7a/libaot-BasicMauiApp.dll.so
  -      28,672 lib/x86/libaot-BasicMauiApp.dll.so
  -     170,446 assemblies/assemblies.blob
Summary:
  -     170,446 Other entries -1.68% (of 10,148,006)
  +           0 Dalvik executables 0.00% (of 11,463,896)
  -     131,328 Shared libraries -0.27% (of 47,970,224)
  -     200,704 Package size difference -0.66% (of 30,356,729)
@dellis1972 dellis1972 changed the title [WIP] Rework use of ObtainStyledAttributes so we can enable AndroidLinkResources Rework use of ObtainStyledAttributes so we can enable AndroidLinkResources Apr 21, 2022
@dellis1972 dellis1972 marked this pull request as ready for review April 21, 2022 10:08
@mattleibow
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@mattleibow mattleibow self-assigned this Apr 29, 2022
@mattleibow mattleibow added perf/app-size Application Size / Trimming (sub: perf) legacy-area-perf Startup / Runtime performance labels Apr 29, 2022
@mattleibow mattleibow merged commit 9fea5cf into dotnet:main May 2, 2022
@dellis1972 dellis1972 deleted the styleablerework branch May 2, 2022 20:19
@github-actions github-actions bot locked and limited conversation to collaborators Dec 23, 2023
@Eilon Eilon added area-publishing Issues with the app packaging/publishing process (ipk/apk/msix/trimming) perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) and removed legacy-area-perf Startup / Runtime performance labels May 10, 2024
@samhouts samhouts added the fixed-in-6.0.300-rc.3 Look for this fix in 6.0.300-rc.3! label Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-publishing Issues with the app packaging/publishing process (ipk/apk/msix/trimming) fixed-in-6.0.300-rc.3 Look for this fix in 6.0.300-rc.3! perf/app-size Application Size / Trimming (sub: perf) perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) platform/android t/enhancement ☀️ New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Android] Remove all uses of Styleable arrays inside the .NET MAUI code

7 participants