From d13d0f972a4eb663251b2a6b5a934c59a6789db1 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Tue, 30 Mar 2021 14:50:20 +0000 Subject: [PATCH] [One .NET] $(AndroidEnablePreloadAssemblies)=False by default (#5790) Context: e0da1f152de84c4cc39983f2fda775ec98c08250 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 --- .../Tests/Xamarin.Android.Build.Tests/BuildTest.cs | 6 +++++- .../Xamarin.Android.Common.targets | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs index c0627aa7823..c08b20db66d 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs @@ -3932,7 +3932,11 @@ public void PackageNamingPolicy ([Values ("LowercaseMD5", "LowercaseCrc64")] str Assert.IsTrue (b.Build (proj), "build should have succeeded."); var environment = b.Output.GetIntermediaryPath (Path.Combine ("__environment__.txt")); FileAssert.Exists (environment); - Assert.AreEqual ($"__XA_PACKAGE_NAMING_POLICY__={packageNamingPolicy}", File.ReadAllText (environment).Trim ()); + if (Builder.UseDotNet) { + Assert.AreEqual ($"__XA_PACKAGE_NAMING_POLICY__={packageNamingPolicy}{Environment.NewLine}mono.enable_assembly_preload=0", File.ReadAllText (environment).Trim ()); + } else { + Assert.AreEqual ($"__XA_PACKAGE_NAMING_POLICY__={packageNamingPolicy}", File.ReadAllText (environment).Trim ()); + } } } diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index f64b08aa6eb..a9fcc31f1ce 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -290,7 +290,8 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved. False - <_AndroidEnablePreloadAssembliesDefault>True + <_AndroidEnablePreloadAssembliesDefault Condition=" '$(UsingAndroidNETSdk)' == 'true' ">False + <_AndroidEnablePreloadAssembliesDefault Condition=" '$(UsingAndroidNETSdk)' != 'true' ">True $(_AndroidEnablePreloadAssembliesDefault) <_NativeAssemblySourceDir>$(IntermediateOutputPath)android\ <_AndroidUseNewTypemaps>True