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

Disable assembly preload by default #5790

Merged
merged 1 commit into from
Mar 30, 2021

Conversation

grendello
Copy link
Contributor

Disable the current default behavior of Xamarin.Android runtime to
preload all the assemblies shipped in the apk on application startup.
This behavior has been the default in order to support older versions of
Xamarin.Forms, sacrificing startup performance.

Comment on lines 293 to 295
<_AndroidEnablePreloadAssembliesDefault>True</_AndroidEnablePreloadAssembliesDefault>
<_AndroidEnablePreloadAssembliesDefault>False</_AndroidEnablePreloadAssembliesDefault>
<AndroidEnablePreloadAssemblies Condition=" '$(AndroidEnablePreloadAssemblies)' == '' ">$(_AndroidEnablePreloadAssembliesDefault)</AndroidEnablePreloadAssemblies>
Copy link
Member

@jonathanpeppers jonathanpeppers Mar 29, 2021

Choose a reason for hiding this comment

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

Maybe we should only do this for .NET 6, so instead something like:

<AndroidEnablePreloadAssemblies Condition=" '$(AndroidEnablePreloadAssemblies)' == '' and '$(UsingAndroidNETSdk)' == 'true' ">false</AndroidEnablePreloadAssemblies>
<AndroidEnablePreloadAssemblies Condition=" '$(AndroidEnablePreloadAssemblies)' == '' and '$(UsingAndroidNETSdk)' != 'true' ">true</AndroidEnablePreloadAssemblies>

$(UsingAndroidNETSdk) is true under .NET 6.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And drop _AndroidEnablePreloadAssembliesDefault completely?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A couple other places use $(_AndroidEnablePreloadAssembliesDefault) so I instead added the condition to _AndroidEnablePreloadAssembliesDefault itself

@grendello grendello force-pushed the disable-assembly-preload-by-default branch 3 times, most recently from d834002 to 05ee6bd Compare March 29, 2021 18:15
@@ -290,7 +290,8 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<AndroidMakeBundleKeepTemporaryFiles Condition=" '$(AndroidMakeBundleKeepTemporaryFiles)' == '' ">False</AndroidMakeBundleKeepTemporaryFiles>

<!-- If true it will cause all the assemblies in the apk to be preloaded on startup time -->
<_AndroidEnablePreloadAssembliesDefault>True</_AndroidEnablePreloadAssembliesDefault>
<_AndroidEnablePreloadAssembliesDefault Condition=" '$(UsingAndroidNETSdk)' == 'true' ">False</_AndroidEnablePreloadAssembliesDefault>
<_AndroidEnablePreloadAssembliesDefault Condition=" '$(UsingAndroidNETSdk)' == 'false' Or '$(UsingAndroidNETSdk)' == '' ">True</_AndroidEnablePreloadAssembliesDefault>
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest using != 'true, which will nicely match both false and empty string and /p:UsingAndroidNETSDK=ThisIsASeriouslyWeirdValue.

@grendello grendello force-pushed the disable-assembly-preload-by-default branch from 05ee6bd to 7f36eb3 Compare March 29, 2021 21:04
@jonpryor
Copy link
Member

We now have unit test failures, e.g. PackageNamingPolicy("LowercaseMD5"):

Expected string length 41 but was 72. Strings differ at index 41.
Expected: "...E_NAMING_POLICY__=LowercaseMD5"
But was:  "...E_NAMING_POLICY__=LowercaseMD5\nmono.enable_assembly_preload=0"
--------------------------------------------^

Appears to be this assert: https://github.com/xamarin/xamarin-android/blob/c9e2d7540393f25d854014d158b4eb7c66a2b7c2/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs#L3935

Is $(AndroidEnablePreloadAssemblies) supposed to impact __environment__.txt? (I forget.)

@@ -290,7 +290,8 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<AndroidMakeBundleKeepTemporaryFiles Condition=" '$(AndroidMakeBundleKeepTemporaryFiles)' == '' ">False</AndroidMakeBundleKeepTemporaryFiles>

<!-- If true it will cause all the assemblies in the apk to be preloaded on startup time -->
<_AndroidEnablePreloadAssembliesDefault>True</_AndroidEnablePreloadAssembliesDefault>
<_AndroidEnablePreloadAssembliesDefault Condition=" '$(UsingAndroidNETSdk)' == 'true' ">False</_AndroidEnablePreloadAssembliesDefault>
<_AndroidEnablePreloadAssembliesDefault Condition=" '$(UsingAndroidNETSdk)' != 'true' ">True</_AndroidEnablePreloadAssembliesDefault>
Copy link
Member

Choose a reason for hiding this comment

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

Since this PR will require additional changes anyway…

Could you please indent this line to match the following line, removing some of the leading whitespace?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@grendello grendello force-pushed the disable-assembly-preload-by-default branch from 7f36eb3 to 1e65355 Compare March 30, 2021 07:04
Disable the current default behavior of Xamarin.Android runtime to
preload all the assemblies shipped in the apk on application startup.
This behavior has been the default in order to support older versions of
`Xamarin.Forms`, sacrificing startup performance.
@grendello grendello force-pushed the disable-assembly-preload-by-default branch from 1e65355 to 7fce2dd Compare March 30, 2021 09:53
@jonpryor jonpryor merged commit d13d0f9 into dotnet:main Mar 30, 2021
@grendello grendello deleted the disable-assembly-preload-by-default branch March 30, 2021 14:52
@grendello grendello restored the disable-assembly-preload-by-default branch May 5, 2021 20:11
grendello added a commit to grendello/xamarin-android that referenced this pull request May 5, 2021
…#5790)

Fixes: dotnet#5838
Context: e0da1f1

Partially reverts 522d7fb which
reverted d13d0f9

The [`$(AndroidEnablePreloadAssemblies)`][0] property controls
whether or not *all* `.dll` files contained within a `.apk` are
loaded during process startup.  *Not* doing so reduces process
startup times, which is desirable, but this also caused certain
Xamarin.Forms apps to fail to run as they previously had, as
not loading all assemblies during startup broke their
Dependency Injection infrastructure.

For .NET 6, we feel we have *some* "wiggle-room" to change default
semantics, so for .NET 6 projects set the default value of
`$(AndroidEnablePreloadAssemblies)` to False, so that assemblies are
*not* pre-loaded during process startup.

`$(AndroidEnablePreloadAssemblies)` can be set to True within the
app's `.csproj` file to return to "legacy" semantics.  This will
cause all assemblies to be loaded during startup, with a
commensurate increase in app startup overheads.

[0]: https://docs.microsoft.com/en-us/xamarin/android/deploy-test/building-apps/build-properties#androidenablepreloadassemblies
jonpryor pushed a commit that referenced this pull request May 7, 2021
…#5914)

Fixes: #5838
Context: e0da1f1

Partially reverts 522d7fb which, reverted d13d0f9.

The [`$(AndroidEnablePreloadAssemblies)`][0] property controls whether
or not *all* `.dll` files contained within a `.apk` are loaded during
process startup.  *Not* doing so reduces process startup times, which
is desirable, but this also caused certain Xamarin.Forms apps to fail
to run as they previously had, as not loading all assemblies during
startup broke their Dependency Injection infrastructure.

For .NET 6, we feel we have *some* "wiggle-room" to change default
semantics, so for .NET 6 projects set the default value of
`$(AndroidEnablePreloadAssemblies)` to False, so that assemblies are
*not* pre-loaded during process startup.

`$(AndroidEnablePreloadAssemblies)` can be set to True within the
app's `.csproj` file to return to "legacy" semantics.  This will cause
all assemblies to be loaded during startup, with a commensurate
increase in app startup overheads.

[0]: https://docs.microsoft.com/en-us/xamarin/android/deploy-test/building-apps/build-properties#androidenablepreloadassemblies
@github-actions github-actions bot locked and limited conversation to collaborators Jan 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants