-
Notifications
You must be signed in to change notification settings - Fork 526
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
[Xamarin.Android.Build.Tasks] Link Resource Nested Types from assemblies #5317
Conversation
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/StripEmbeddedLibraries.cs
Outdated
Show resolved
Hide resolved
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/StripEmbeddedLibraries.cs
Outdated
Show resolved
Hide resolved
@radekdoulik this is a work in progress, but if you have any advice on how to do this more efficiently please feel free to comment. |
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/StripEmbeddedLibraries.cs
Outdated
Show resolved
Hide resolved
6f0e10d
to
c8cdb58
Compare
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveResourceDesignerStep.cs
Outdated
Show resolved
Hide resolved
d283fe7
to
8af10e9
Compare
6f0e3f9
to
6c89991
Compare
4599cb6
to
87cc566
Compare
87cc566
to
bfd62f0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to wait on: xamarin/Xamarin.Forms#13176
Is there a way to make a test for this?
- Xamarin.Forms app using
<Image Source="foo" />
wherefoo
isAndroidResource
- Launch the app, verify the image is displayed?
1efc1cf
to
e5ab173
Compare
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/StripEmbeddedLibraries.cs
Outdated
Show resolved
Hide resolved
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveResourceDesignerStep.cs
Show resolved
Hide resolved
847a9cb
to
428996c
Compare
xamarin/Xamarin.Forms#13176 has been merged. How do we know when we can rely on it? |
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveResourceDesignerStep.cs
Show resolved
Hide resolved
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveResourceDesignerStep.cs
Show resolved
Hide resolved
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveResourceDesignerStep.cs
Outdated
Show resolved
Hide resolved
@jonpryor the [Obsolete("GetDrawableByName(string) is obsolete as of version 4.8. "
+ "Please use GetDrawableId(string, context) instead.")] I see the change in if I download & ilspy Xamarin.Forms 5.0.0.2012 but not 4.8.0.1821. So maybe this is only in Xamarin.Forms 5.0 and higher. |
428996c
to
1532df6
Compare
/azp run |
Azure Pipelines failed to run 1 pipeline(s). |
1532df6
to
7b92c99
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise, LGTM 👍
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AndroidLinkContext.cs
Outdated
Show resolved
Hide resolved
7b92c99
to
e8dd82f
Compare
The idea is to remove the `Resource` class NestedTypes from the final linked assemblies in release mode. We do this by examining all the locations where the fields are used and replacing those IL calls with a call which specifies the actual resource id directly. This will remove the need to call `global::Android.Runtime.ResourceIdManager.UpdateIdValues();` completely. It will also allow us to completely remove the `Resource` class sub classes. Due to an odd linker failure we cannot completely remove the `Resource` class itself. However it will be completely empty. As mentioned the he call to `UpdateIdValues` will no longer be required. This should reduce startup time slightly. One thing to note is this will only be true for Release apps. Debug apps will still call `UpdateIdValues`. This is mostly due to the time is takes to run this new Linker Step. Applying this to a Debug build would effect the build times during development. A new MSBuild property `AndroidLinkResources` has been added to control this feature. It is turned off by default for now. This seems to save between 100-280kb for an apk. This is dependent on how many assemblies are Android assemblies and contain or use Resources. Xamarin.Forms base app apkdiff results ``` Size difference in bytes ([*1] apk1 only, [*2] apk2 only): - 10 assemblies/Mono.Android.dll - 4,001 assemblies/Xamarin.Essentials.dll - 45,426 assemblies/Xamarin.Forms.Platform.Android.dll - 102,839 assemblies/formstest.Android.dll Summary: + 0 Other entries 0.00% (of 887,808) + 0 Dalvik executables 0.00% (of 3,341,152) - 152,276 Assemblies -3.23% (of 4,709,077) + 0 Shared libraries 0.00% (of 41,693,796) - 151,552 Package size difference -0.73% (of 20,820,695) ``` Maps sample apkdiff results ``` Size difference in bytes ([*1] apk1 only, [*2] apk2 only): - 4 lib/armeabi-v7a/libxamarin-app.so - 14 assemblies/Mono.Android.dll - 4,757 assemblies/Xamarin.Essentials.dll - 46,051 assemblies/Xamarin.Forms.Platform.dll - 47,586 assemblies/Xamarin.Forms.Platform.Android.dll - 48,470 assemblies/Xamarin.Forms.Maps.Android.dll - 135,589 assemblies/FormsMapsSample.Android.dll Summary: + 0 Other entries 0.00% (of 1,137,209) + 0 Dalvik executables 0.00% (of 2,519,972) - 282,467 Assemblies -5.51% (of 5,123,833) - 4 Shared libraries -0.00% (of 41,843,596) - 282,624 Package size difference -1.34% (of 21,075,664) ```
e8dd82f
to
f297e88
Compare
Squash-and-merge: Summary:
commit message: The generated `Resource` class in `Resource.designer.cs` can be quite
large.
When doing "Release" builds, update the linker to *remove* the
`Resource` class and its nested types. We do this by examining all
the locations where the fields are used and replacing those IL calls
with a call which specifies the actual resource id value directly.
This will remove the need to call
`global::Android.Runtime.ResourceIdManager.UpdateIdValues();` from
the static constructors in the `Resource` class and its nested types,
which will improve app launch times (as `UpdateIdValues()` involves
Reflection, which is not "fast").
Due to an odd linker failure we cannot completely remove the
`Resource` class itself. It *will* be completely empty.
"Debug" builds will *not* remove the `Resource` type, and will retain
the call to `UpdateIdValues()`, in order to ensure that app rebuild
and redeploy times are as fast as possible.
The new `$(AndroidLinkResources)` MSBuild property has been added to
control this feature. It is disabled by default for now.
`$(AndroidLinkResources)`=True appears to save between 100-280kb for
an `.apk`. This is dependent on how many assemblies are Android
assemblies and contain or use Resources.
Xamarin.Forms base app `apkdiff` results
Size difference in bytes ([*1] apk1 only, [*2] apk2 only):
- 10 assemblies/Mono.Android.dll
- 4,001 assemblies/Xamarin.Essentials.dll
- 45,426 assemblies/Xamarin.Forms.Platform.Android.dll
- 102,839 assemblies/formstest.Android.dll
Summary:
+ 0 Other entries 0.00% (of 887,808)
+ 0 Dalvik executables 0.00% (of 3,341,152)
- 152,276 Assemblies -3.23% (of 4,709,077)
+ 0 Shared libraries 0.00% (of 41,693,796)
- 151,552 Package size difference -0.73% (of 20,820,695)
Maps sample `apkdiff` results
Size difference in bytes ([*1] apk1 only, [*2] apk2 only):
- 4 lib/armeabi-v7a/libxamarin-app.so
- 14 assemblies/Mono.Android.dll
- 4,757 assemblies/Xamarin.Essentials.dll
- 46,051 assemblies/Xamarin.Forms.Platform.dll
- 47,586 assemblies/Xamarin.Forms.Platform.Android.dll
- 48,470 assemblies/Xamarin.Forms.Maps.Android.dll
- 135,589 assemblies/FormsMapsSample.Android.dll
Summary:
+ 0 Other entries 0.00% (of 1,137,209)
+ 0 Dalvik executables 0.00% (of 2,519,972)
- 282,467 Assemblies -5.51% (of 5,123,833)
- 4 Shared libraries -0.00% (of 41,843,596)
- 282,624 Package size difference -1.34% (of 21,075,664) |
The idea is to remove the
Resource
class NestedTypes from the finallinked assemblies in release mode. We do this by examining all the
locations where the fields are used and replacing those IL calls with a
call which specifies the actual resource id directly. This will remove
the need to call
global::Android.Runtime.ResourceIdManager.UpdateIdValues();
completely. It will also allow us to completely remove the
Resource
class sub classes. Due to an odd linker failure we cannot completely
remove the
Resource
class itself. However it will be completely empty.As mentioned the call to
UpdateIdValues
will no longer be required.This should reduce startup time slightly. One thing to note is this will
only be true for Release apps when enabled. Debug apps will still
call
UpdateIdValues
. This is due to the time is takes to runthe Linker Step. Applying this to a Debug build would effect the build
times during development.
A new MSBuild property
AndroidLinkResources
has been added to controlthis feature. It is turned off by default for now.
This seems to save between 100-280kb for an apk. This is dependent on
how many assemblies are Android assemblies and contain or use Resources.
Xamarin.Forms base app apkdiff results
Maps sample apkdiff results