From 9ecf35f310ea07843af5279f7a59b51a31770bb8 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 8 Jan 2025 16:23:45 -0600 Subject: [PATCH 1/5] [illink] consolidate & remove hardcoded assembly names Our codebase has various notions of "framework assembly", many of which are outdated and wrong! * `@(_SharedRuntimeAssemblies)` in `Xamarin.Android.Build.Tasks.targets` which generates... * `src\Xamarin.Android.Build.Tasks\obj\$(Configuration)\Profile.g.cs` * src\Xamarin.Android.Build.Tasks\Utilities\Profile.cs * `src\Microsoft.Android.Sdk.ILLink\Profile.cs` The new and *correct* way to identify a "framework assembly" is to check either: * `%(FrameworkReferenceName)` is `Microsoft.Android` or starts with `Microsoft.NETCore.` * `%(NuGetPackageId)` starts with `Microsoft.NETCore.App.Runtime.` or `Microsoft.Android.Runtime.` I moved the new logic into `MonoAndroidHelper.IsFrameworkAssembly()` and `MonoAndroidHelper.IsFromAKnownRuntimePack()` (useful for `.jar` and other files). I deleted all other instances of `Profile.cs`, which simplifies how trimmer steps are shared between the `` MSBuild task and the trimmer (illink). In these places, we were likely just checking against a stale list of assemblies *anyway*, so I simply removed all these checks. --- .../ApplyPreserveAttribute.cs | 2 +- .../MarkJavaObjects.cs | 13 +- .../Microsoft.Android.Sdk.ILLink.csproj | 4 - .../PreserveApplications.cs | 3 - .../PreserveExportedTypes.cs | 3 +- .../PreserveJavaExceptions.cs | 3 - src/Microsoft.Android.Sdk.ILLink/Profile.cs | 52 ----- src/Microsoft.Android.Sdk.ILLink/SetupStep.cs | 20 -- .../StripEmbeddedLibraries.cs | 5 - .../Linker/Mobile.Tuner/MobileProfile.cs | 205 ------------------ .../MonoDroid.Tuner/AddKeepAlivesStep.cs | 3 - .../MonoDroid.Tuner/FixAbstractMethodsStep.cs | 10 - .../FixLegacyResourceDesignerStep.cs | 1 - .../MonoDroid.Tuner/MonoDroidProfile.cs | 25 --- .../RemoveResourceDesignerStep.cs | 4 - .../Microsoft.Android.Sdk.ILLink.targets | 3 - .../Tasks/FilterAssemblies.cs | 7 +- .../Tasks/GenerateResourceDesigner.cs | 5 - .../Tasks/LinkAssembliesNoShrink.cs | 6 +- .../Tasks/ProcessAssemblies.cs | 11 +- .../Tasks/ResolveLibraryProjectImports.cs | 11 +- .../Tasks/ResolveSdksTask.cs | 2 - .../Utilities/MonoAndroidHelper.Linker.cs | 40 ---- .../Utilities/MonoAndroidHelper.cs | 22 ++ .../Utilities/Profile.cs | 44 ---- .../Xamarin.Android.Build.Tasks.csproj | 4 - .../Xamarin.Android.Build.Tasks.targets | 24 -- 27 files changed, 34 insertions(+), 498 deletions(-) delete mode 100644 src/Microsoft.Android.Sdk.ILLink/Profile.cs delete mode 100644 src/Microsoft.Android.Sdk.ILLink/SetupStep.cs delete mode 100644 src/Xamarin.Android.Build.Tasks/Linker/Mobile.Tuner/MobileProfile.cs delete mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidProfile.cs delete mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/Profile.cs diff --git a/src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs b/src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs index de2a9590cc0..3f2d43e75d7 100644 --- a/src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs +++ b/src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs @@ -25,7 +25,7 @@ public override SubStepTargets Targets { public override bool IsActiveFor (AssemblyDefinition assembly) { - return !Profile.IsSdkAssembly (assembly) && Annotations.GetAction (assembly) == AssemblyAction.Link; + return Annotations.GetAction (assembly) == AssemblyAction.Link; } public override void ProcessType (TypeDefinition type) diff --git a/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs b/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs index bb029b7421a..d4b73ee5ccb 100644 --- a/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs +++ b/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs @@ -6,7 +6,6 @@ using Mono.Linker.Steps; using Java.Interop.Tools.Cecil; using Xamarin.Android.Tasks; -using Profile = Microsoft.Android.Sdk.ILLink.Profile; namespace MonoDroid.Tuner { @@ -27,11 +26,6 @@ public override void Initialize (LinkContext context, MarkContext markContext) bool IsActiveFor (AssemblyDefinition assembly) { - if (Profile.IsSdkAssembly (assembly)) - return false; - if (Profile.IsProductAssembly (assembly)) - return false; - return assembly.MainModule.HasTypeReference ("System.Net.Http.HttpMessageHandler") || assembly.MainModule.HasTypeReference ("Java.Lang.Object") || assembly.MainModule.HasTypeReference ("Android.Util.IAttributeSet"); @@ -107,7 +101,7 @@ public void ProcessType (TypeDefinition type) // If a user overrode a method, we need to preserve it, // because it won't be referenced anywhere, but it will // be called from Java - if (IsUserType (type) && type.HasMethods) { + if (type.HasMethods) { foreach (var method in type.Methods.Where (m => m.Overrides != null)) PreserveMethod (type, method); } @@ -395,11 +389,6 @@ static bool IsImplementor (TypeDefinition type, IMetadataResolver cache) return type.Name.EndsWith ("Implementor", StringComparison.Ordinal) && type.Inherits ("Java.Lang.Object", cache); } - static bool IsUserType (TypeDefinition type) - { - return !MonoAndroidHelper.IsFrameworkAssembly (type.Module.Assembly.Name.Name + ".dll"); - } - void PreserveImplementor (TypeDefinition type) { if (!type.HasMethods) diff --git a/src/Microsoft.Android.Sdk.ILLink/Microsoft.Android.Sdk.ILLink.csproj b/src/Microsoft.Android.Sdk.ILLink/Microsoft.Android.Sdk.ILLink.csproj index ba08c1f2bb8..ebf15dba011 100644 --- a/src/Microsoft.Android.Sdk.ILLink/Microsoft.Android.Sdk.ILLink.csproj +++ b/src/Microsoft.Android.Sdk.ILLink/Microsoft.Android.Sdk.ILLink.csproj @@ -10,8 +10,6 @@ - - @@ -20,8 +18,6 @@ - - diff --git a/src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs b/src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs index 5e3e1b0c44d..f420635f07d 100644 --- a/src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs +++ b/src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs @@ -4,10 +4,7 @@ using Mono.Linker; using Mono.Linker.Steps; - using Mono.Tuner; -using Mobile.Tuner; - using Mono.Cecil; namespace MonoDroid.Tuner { diff --git a/src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs b/src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs index 13de9feb243..99188842b87 100644 --- a/src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs +++ b/src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs @@ -24,7 +24,8 @@ public override SubStepTargets Targets { public override bool IsActiveFor (AssemblyDefinition assembly) { - return !Profile.IsSdkAssembly (assembly); + return assembly.MainModule.HasTypeReference ("Java.Interop.ExportAttribute") || + assembly.MainModule.HasTypeReference ("Java.Interop.ExportFieldAttribute"); } public override void ProcessField (FieldDefinition field) diff --git a/src/Microsoft.Android.Sdk.ILLink/PreserveJavaExceptions.cs b/src/Microsoft.Android.Sdk.ILLink/PreserveJavaExceptions.cs index 1c0ad8f0898..6750c001c2f 100644 --- a/src/Microsoft.Android.Sdk.ILLink/PreserveJavaExceptions.cs +++ b/src/Microsoft.Android.Sdk.ILLink/PreserveJavaExceptions.cs @@ -4,10 +4,7 @@ using Mono.Linker; using Mono.Linker.Steps; - using Mono.Tuner; -using Mobile.Tuner; - using Mono.Cecil; namespace MonoDroid.Tuner { diff --git a/src/Microsoft.Android.Sdk.ILLink/Profile.cs b/src/Microsoft.Android.Sdk.ILLink/Profile.cs deleted file mode 100644 index 90a72c6d068..00000000000 --- a/src/Microsoft.Android.Sdk.ILLink/Profile.cs +++ /dev/null @@ -1,52 +0,0 @@ -using Mono.Cecil; -using MonoDroid.Tuner; - -namespace Microsoft.Android.Sdk.ILLink -{ - public abstract class Profile - { - static Profile current; - - public static Profile Current { - get { - if (current == null) - current = new MonoDroidProfile (); - - return current; - } - } - - public static bool IsSdkAssembly (AssemblyDefinition assembly) - { - return Current.IsSdk (assembly); - } - - public static bool IsSdkAssembly (string assemblyName) - { - return Current.IsSdk (assemblyName); - } - - public static bool IsProductAssembly (AssemblyDefinition assembly) - { - return Current.IsProduct (assembly); - } - - public static bool IsProductAssembly (string assemblyName) - { - return Current.IsProduct (assemblyName); - } - - protected virtual bool IsSdk (AssemblyDefinition assembly) - { - return IsSdk (assembly.Name.Name); - } - - protected virtual bool IsProduct (AssemblyDefinition assembly) - { - return IsProduct (assembly.Name.Name); - } - - protected abstract bool IsSdk (string assemblyName); - protected abstract bool IsProduct (string assemblyName); - } -} diff --git a/src/Microsoft.Android.Sdk.ILLink/SetupStep.cs b/src/Microsoft.Android.Sdk.ILLink/SetupStep.cs deleted file mode 100644 index 53ee2f3017f..00000000000 --- a/src/Microsoft.Android.Sdk.ILLink/SetupStep.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using Java.Interop.Tools.Cecil; -using Mono.Cecil; -using Mono.Linker; -using Mono.Linker.Steps; -using MonoDroid.Tuner; - -namespace Microsoft.Android.Sdk.ILLink -{ - class SetupStep : BaseStep - { - protected override void Process () - { - if (Context.TryGetCustomData ("XATargetFrameworkDirectories", out string tfmPaths)) - Xamarin.Android.Tasks.MonoAndroidHelper.TargetFrameworkDirectories = tfmPaths.Split (new char [] { ';' }); - } - } -} diff --git a/src/Microsoft.Android.Sdk.ILLink/StripEmbeddedLibraries.cs b/src/Microsoft.Android.Sdk.ILLink/StripEmbeddedLibraries.cs index e455d80dc1f..0a0adaf39bb 100644 --- a/src/Microsoft.Android.Sdk.ILLink/StripEmbeddedLibraries.cs +++ b/src/Microsoft.Android.Sdk.ILLink/StripEmbeddedLibraries.cs @@ -18,11 +18,6 @@ protected override void ProcessAssembly (AssemblyDefinition assembly) return; var fileName = assembly.Name.Name + ".dll"; - if (MonoAndroidHelper.IsFrameworkAssembly (fileName) && - !MonoAndroidHelper.FrameworkEmbeddedJarLookupTargets.Contains (fileName) && - !MonoAndroidHelper.FrameworkEmbeddedNativeLibraryAssemblies.Contains (fileName)) - return; - bool assembly_modified = false; foreach (var mod in assembly.Modules) { foreach (var r in mod.Resources.ToArray ()) { diff --git a/src/Xamarin.Android.Build.Tasks/Linker/Mobile.Tuner/MobileProfile.cs b/src/Xamarin.Android.Build.Tasks/Linker/Mobile.Tuner/MobileProfile.cs deleted file mode 100644 index 3fc0c4acbec..00000000000 --- a/src/Xamarin.Android.Build.Tasks/Linker/Mobile.Tuner/MobileProfile.cs +++ /dev/null @@ -1,205 +0,0 @@ -using System; -using System.Collections.Generic; - -using Mono.Cecil; - -using Mono.Tuner; -#if ILLINK -using Microsoft.Android.Sdk.ILLink; -#endif - -namespace Mobile.Tuner { - - public abstract class MobileProfile : Profile { - - static readonly HashSet Sdk = new HashSet { - "SMDiagnostics", - "mscorlib", - "System", - "System.ComponentModel.Composition", - "System.ComponentModel.DataAnnotations", - "System.Core", - "System.Data", - "System.Data.DataSetExtensions", - "System.Data.Services.Client", - "System.EnterpriseServices", - "System.IdentityModel", - "System.IO.Compression.FileSystem", - "System.IO.Compression", - "System.Json", - "System.Net", - "System.Net.Http", - "System.Net.Http.WinHttpHandler", - "System.Numerics", - "System.Numerics.Vectors", - "System.Reflection.Context", - "System.Runtime.Serialization", - "System.Security", - "System.ServiceModel", - "System.ServiceModel.Internals", - "System.ServiceModel.Web", - "System.Transactions", - "System.Web.Services", - "System.Windows", - "System.Xml", - "System.Xml.Linq", - "System.Xml.Serialization", - "Microsoft.CSharp", - "Microsoft.VisualBasic", - "Mono.CSharp", - "Mono.Cairo", - "Mono.CompilerServices.SymbolWriter", - "Mono.Data.Tds", - "Mono.Data.Sqlite", - "Mono.Posix", - "Mono.Security", - // Facades assemblies (PCL) - "Microsoft.Win32.Primitives", - "Microsoft.Win32.Registry.AccessControl", - "Microsoft.Win32.Registry", - "System.AppContext", - "System.Collections.Concurrent", - "System.Collections.NonGeneric", - "System.Collections.Specialized", - "System.Collections", - "System.ComponentModel.Annotations", - "System.ComponentModel.EventBasedAsync", - "System.ComponentModel.Primitives", - "System.ComponentModel.TypeConverter", - "System.ComponentModel", - "System.Console", - "System.Data.Common", - "System.Data.SqlClient", - "System.Diagnostics.Contracts", - "System.Diagnostics.Debug", - "System.Diagnostics.FileVersionInfo", - "System.Diagnostics.Process", - "System.Diagnostics.StackTrace", - "System.Diagnostics.TextWriterTraceListener", - "System.Diagnostics.Tools", - "System.Diagnostics.TraceEvent", - "System.Diagnostics.TraceSource", - "System.Diagnostics.Tracing", - "System.Drawing.Common", - "System.Drawing.Primitives", - "System.Dynamic.Runtime", - "System.Globalization.Calendars", - "System.Globalization.Extensions", - "System.Globalization", - "System.IO", - "System.IO.Compression.ZipFile", - "System.IO.FileSystem.AccessControl", - "System.IO.FileSystem.DriveInfo", - "System.IO.FileSystem.Primitives", - "System.IO.FileSystem.Watcher", - "System.IO.FileSystem", - "System.IO.IsolatedStorage", - "System.IO.MemoryMappedFiles", - "System.IO.Pipes", - "System.IO.UnmanagedMemoryStream", - "System.Linq.Expressions", - "System.Linq.Parallel", - "System.Linq.Queryable", - "System.Linq", - "System.Memory", - "System.Net.AuthenticationManager", - "System.Net.Cache", - "System.Net.HttpListener", - "System.Net.Mail", - "System.Net.NameResolution", - "System.Net.NetworkInformation", - "System.Net.Ping", - "System.Net.Primitives", - "System.Net.Requests", - "System.Net.Security", - "System.Net.ServicePoint", - "System.Net.Sockets", - "System.Net.Utilities", - "System.Net.WebHeaderCollection", - "System.Net.WebSockets.Client", - "System.Net.WebSockets", - "System.ObjectModel", - "System.Reflection.DispatchProxy", - "System.Reflection.Emit.ILGeneration", - "System.Reflection.Emit.Lightweight", - "System.Reflection.Emit", - "System.Reflection.Extensions", - "System.Reflection.Primitives", - "System.Reflection.TypeExtensions", - "System.Reflection", - "System.Resources.Reader", - "System.Resources.ReaderWriter", - "System.Resources.ResourceManager", - "System.Resources.Writer", - "System.Runtime.CompilerServices.VisualC", - "System.Runtime.Extensions", - "System.Runtime.Handles", - "System.Runtime.InteropServices", - "System.Runtime.InteropServices.RuntimeInformation", - "System.Runtime.InteropServices.WindowsRuntime", - "System.Runtime.Loader", - "System.Runtime.Numerics", - "System.Runtime.Serialization.Formatters", - "System.Runtime.Serialization.Json", - "System.Runtime.Serialization.Primitives", - "System.Runtime.Serialization.Xml", - "System.Runtime", - "System.Security.AccessControl", - "System.Security.Claims", - "System.Security.Cryptography.Algorithms", - "System.Security.Cryptography.Cng", - "System.Security.Cryptography.Csp", - "System.Security.Cryptography.DeriveBytes", - "System.Security.Cryptography.Encoding", - "System.Security.Cryptography.Encryption.Aes", - "System.Security.Cryptography.Encryption.ECDiffieHellman", - "System.Security.Cryptography.Encryption.ECDsa", - "System.Security.Cryptography.Encryption", - "System.Security.Cryptography.Hashing.Algorithms", - "System.Security.Cryptography.Hashing", - "System.Security.Cryptography.OpenSsl", - "System.Security.Cryptography.Pkcs", - "System.Security.Cryptography.Primitives", - "System.Security.Cryptography.ProtectedData", - "System.Security.Cryptography.RSA", - "System.Security.Cryptography.RandomNumberGenerator", - "System.Security.Cryptography.X509Certificates", - "System.Security.Principal.Windows", - "System.Security.Principal", - "System.Security.SecureString", - "System.ServiceModel.Duplex", - "System.ServiceModel.NetTcp", - "System.ServiceModel.Http", - "System.ServiceModel.Primitives", - "System.ServiceModel.Security", - "System.ServiceProcess.ServiceController", - "System.Text.Encoding.CodePages", - "System.Text.Encoding.Extensions", - "System.Text.Encoding", - "System.Text.RegularExpressions", - "System.Threading.AccessControl", - "System.Threading.Overlapped", - "System.Threading.Tasks.Parallel", - "System.Threading.Tasks", - "System.Threading.Thread", - "System.Threading.ThreadPool", - "System.Threading.Timer", - "System.Threading", - "System.ValueTuple", - "System.Xml.ReaderWriter", - "System.Xml.XDocument", - "System.Xml.XPath.XDocument", - "System.Xml.XPath.XmlDocument", - "System.Xml.XPath", - "System.Xml.XmlDocument", - "System.Xml.XmlSerializer", - "System.Xml.Xsl.Primitives", - "netstandard", - }; - - protected override bool IsSdk (string assemblyName) - { - return Sdk.Contains (assemblyName); - } - } -} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddKeepAlivesStep.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddKeepAlivesStep.cs index 8a27d1d7eb8..e143d5c312f 100644 --- a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddKeepAlivesStep.cs +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddKeepAlivesStep.cs @@ -10,9 +10,6 @@ using Mono.Linker.Steps; using Mono.Cecil.Cil; -#if ILLINK -using Microsoft.Android.Sdk.ILLink; -#endif // ILLINK namespace MonoDroid.Tuner { diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs index 70de2500931..d9bf7b6d281 100644 --- a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs @@ -12,7 +12,6 @@ using Mono.Tuner; #if ILLINK -using Microsoft.Android.Sdk.ILLink; using Resources = Microsoft.Android.Sdk.ILLink.Properties.Resources; #else // !ILLINK using Resources = Xamarin.Android.Tasks.Properties.Resources; @@ -53,9 +52,6 @@ bool CheckShouldProcessAssembly (AssemblyDefinition assembly) if (!Annotations.HasAction (assembly)) Annotations.SetAction (assembly, AssemblyAction.Skip); - if (IsProductOrSdkAssembly (assembly)) - return false; - CheckAppDomainUsage (assembly, (string msg) => #if ILLINK Context.LogMessage (MessageContainer.CreateCustomWarningMessage (Context, msg, 6200, new MessageOrigin (), WarnVersion.ILLink5)) @@ -130,12 +126,6 @@ internal void CheckAppDomainUsage (AssemblyDefinition assembly, Action w } } - bool IsProductOrSdkAssembly (AssemblyDefinition assembly) => - IsProductOrSdkAssembly (assembly.Name.Name); - - public bool IsProductOrSdkAssembly (string assemblyName) => - Profile.IsSdkAssembly (assemblyName) || Profile.IsProductAssembly (assemblyName); - bool MightNeedFix (TypeDefinition type) { return !type.IsAbstract && type.IsSubclassOf ("Java.Lang.Object", cache); diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs index 777c419fbc8..f60a9c7a2a0 100644 --- a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixLegacyResourceDesignerStep.cs @@ -13,7 +13,6 @@ using Mono.Tuner; #if ILLINK -using Microsoft.Android.Sdk.ILLink; using Resources = Microsoft.Android.Sdk.ILLink.Properties.Resources; #else // !ILLINK using Resources = Xamarin.Android.Tasks.Properties.Resources; diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidProfile.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidProfile.cs deleted file mode 100644 index b11d55e9be6..00000000000 --- a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidProfile.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; - -using Mono.Tuner; -using Mobile.Tuner; - -using Mono.Cecil; - -namespace MonoDroid.Tuner { - - class MonoDroidProfile : MobileProfile { - - protected override bool IsProduct (string assemblyName) - { - return assemblyName == "Mono.Android"; - } - - protected override bool IsSdk (string assemblyName) - { - return assemblyName.Equals ("Java.Interop", StringComparison.Ordinal) - || assemblyName.Equals ("Java.Interop.GenericMarshaler", StringComparison.Ordinal) - || base.IsSdk (assemblyName); - } - } -} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveResourceDesignerStep.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveResourceDesignerStep.cs index 0ac0e0e7c33..ba22a283e46 100644 --- a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveResourceDesignerStep.cs +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveResourceDesignerStep.cs @@ -93,10 +93,6 @@ internal override bool ProcessAssemblyDesigner (AssemblyDefinition assembly) { if (mainDesigner == null) return false; - var fileName = assembly.Name.Name + ".dll"; - if (MonoAndroidHelper.IsFrameworkAssembly (fileName)) - return false; - LogMessage ($" Fixing up {assembly.Name.Name}"); TypeDefinition localDesigner = null; CustomAttribute designerAttribute; diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.ILLink.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.ILLink.targets index 1cd67019059..6e783fdf500 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.ILLink.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.ILLink.targets @@ -32,7 +32,6 @@ This file contains the .NET 5-specific targets to customize ILLink --> <_TrimmerCustomData Include="AndroidHttpClientHandlerType" Value="$(AndroidHttpClientHandlerType)" /> <_TrimmerCustomData Include="AndroidCustomViewMapFile" Value="$(_OuterCustomViewMapFile)" /> - <_TrimmerCustomData Include="XATargetFrameworkDirectories" Value="$(_XATargetFrameworkDirectories)" /> <_TrimmerCustomData Condition=" '$(_ProguardProjectConfiguration)' != '' " Include="ProguardConfiguration" @@ -44,8 +43,6 @@ This file contains the .NET 5-specific targets to customize ILLink https://github.com/dotnet/sdk/blob/a5393731b5b7b225692fff121f747fbbc9e8b140/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.ILLink.targets#L131 --> - - <_TrimmerCustomSteps Include="$(_AndroidLinkerCustomStepAssembly)" BeforeStep="MarkStep" Type="Microsoft.Android.Sdk.ILLink.SetupStep" /> <_TrimmerCustomSteps Include="$(_AndroidLinkerCustomStepAssembly)" Type="Microsoft.Android.Sdk.ILLink.PreserveSubStepDispatcher" /> <_TrimmerCustomSteps Include="$(_AndroidLinkerCustomStepAssembly)" Type="MonoDroid.Tuner.MarkJavaObjects" /> diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/FilterAssemblies.cs b/src/Xamarin.Android.Build.Tasks/Tasks/FilterAssemblies.cs index 9c3fe84407a..057bcfb9bd7 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/FilterAssemblies.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/FilterAssemblies.cs @@ -34,12 +34,7 @@ public override bool RunTask () var output = new List (InputAssemblies.Length); foreach (var assemblyItem in InputAssemblies) { - // Skip .NET 6.0 assemblies - var frameworkReferenceName = assemblyItem.GetMetadata ("FrameworkReferenceName") ?? ""; - if (frameworkReferenceName == "Microsoft.Android") { - continue; // No need to process Mono.Android.dll or Java.Interop.dll - } - if (frameworkReferenceName.StartsWith ("Microsoft.NETCore.", StringComparison.OrdinalIgnoreCase)) { + if (MonoAndroidHelper.IsFrameworkAssembly (assemblyItem)) { continue; // No need to process BCL assemblies } if (string.Equals (assemblyItem.GetMetadata ("TargetPlatformIdentifier"), "android", StringComparison.OrdinalIgnoreCase)) { diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesigner.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesigner.cs index 35474f678ac..4afb58ccbe7 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesigner.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesigner.cs @@ -111,11 +111,6 @@ public override bool RunTask () foreach (var assembly in References) { var assemblyPath = assembly.ItemSpec; var fileName = Path.GetFileName (assemblyPath); - if (MonoAndroidHelper.IsFrameworkAssembly (fileName) && - !MonoAndroidHelper.FrameworkEmbeddedJarLookupTargets.Contains (fileName)) { - Log.LogDebugMessage ($"Skipping framework assembly '{fileName}'."); - continue; - } if (!File.Exists (assemblyPath)) { Log.LogDebugMessage ($"Skipping non-existent dependency '{assemblyPath}'."); continue; diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs b/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs index bb7d7c54516..db271d4948b 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs @@ -130,16 +130,16 @@ void DoRunTask (ITaskItem source, ITaskItem destination, RunState runState, Writ CopyIfChanged (source, destination); return; } - if (runState.fixAbstractMethodsStep!.IsProductOrSdkAssembly (assemblyName)) { + if (MonoAndroidHelper.IsFrameworkAssembly (source)) { CopyIfChanged (source, destination); return; } // Only run the step on "MonoAndroid" assemblies - if (MonoAndroidHelper.IsMonoAndroidAssembly (source) && !MonoAndroidHelper.IsSharedRuntimeAssembly (source.ItemSpec)) { + if (MonoAndroidHelper.IsMonoAndroidAssembly (source)) { AssemblyDefinition assemblyDefinition = runState.resolver!.GetAssembly (source.ItemSpec); - bool save = runState.fixAbstractMethodsStep.FixAbstractMethods (assemblyDefinition); + bool save = runState.fixAbstractMethodsStep!.FixAbstractMethods (assemblyDefinition); if (UseDesignerAssembly) save |= runState.fixLegacyResourceDesignerStep!.ProcessAssemblyDesigner (assemblyDefinition); if (AddKeepAlives) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ProcessAssemblies.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ProcessAssemblies.cs index a5b7cfcf36d..bce699d3883 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/ProcessAssemblies.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ProcessAssemblies.cs @@ -84,7 +84,7 @@ public override bool RunTask () if (InputJavaLibraries != null) { var javaLibraries = new Dictionary (StringComparer.OrdinalIgnoreCase); foreach (var item in InputJavaLibraries) { - if (!IsFromAKnownRuntimePack (item)) + if (!MonoAndroidHelper.IsFromAKnownRuntimePack (item)) continue; var name = Path.GetFileNameWithoutExtension(item.ItemSpec); if (!javaLibraries.ContainsKey (name)) { @@ -126,7 +126,7 @@ void SetMetadataForAssemblies (List output, Dictionary output, Dictionary symbols, ITaskItem assembly) { var symbolPath = Path.ChangeExtension (assembly.ItemSpec, ".pdb"); diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs index 637667b72b8..1b735ee61dc 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs @@ -170,12 +170,6 @@ void Extract ( foreach (var assemblyItem in Assemblies) { var assemblyPath = assemblyItem.ItemSpec; var fileName = Path.GetFileName (assemblyPath); - if (MonoAndroidHelper.IsFrameworkAssembly (fileName) && - !MonoAndroidHelper.FrameworkEmbeddedJarLookupTargets.Contains (fileName) && - !MonoAndroidHelper.FrameworkEmbeddedNativeLibraryAssemblies.Contains (fileName)) { - Log.LogDebugMessage ($"Skipping framework assembly '{fileName}'."); - continue; - } if (!File.Exists (assemblyPath)) { Log.LogDebugMessage ($"Skipping non-existent dependency '{assemblyPath}'."); continue; @@ -184,8 +178,7 @@ void Extract ( Log.LogDebugMessage ("Skipping resource extraction for '{0}' .", assemblyPath); continue; } - string assemblyFileName = Path.GetFileName (assemblyPath); - string assemblyIdentName = assemblyMap.GetLibraryImportDirectoryNameForAssembly (assemblyFileName); + string assemblyIdentName = assemblyMap.GetLibraryImportDirectoryNameForAssembly (fileName); string outDirForDll = Path.Combine (OutputImportDirectory, assemblyIdentName); string importsDir = Path.Combine (outDirForDll, ImportsDirectory); string nativeimportsDir = Path.Combine (outDirForDll, NativeImportsDirectory); @@ -226,7 +219,7 @@ void Extract ( continue; } - Log.LogDebugMessage ($"Refreshing {assemblyFileName}"); + Log.LogDebugMessage ($"Refreshing {fileName}"); using (var pe = new PEReader (File.OpenRead (assemblyPath))) { var reader = pe.GetMetadataReader (); diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs index a93bdb23536..8a0aa3f95d6 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs @@ -128,8 +128,6 @@ public override bool RunTask () return false; } - MonoAndroidHelper.TargetFrameworkDirectories = ReferenceAssemblyPaths; - Log.LogDebugMessage ($"{nameof (ResolveSdks)} Outputs:"); Log.LogDebugMessage ($" {nameof (AndroidSdkPath)}: {AndroidSdkPath}"); Log.LogDebugMessage ($" {nameof (AndroidNdkPath)}: {AndroidNdkPath}"); diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Linker.cs b/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Linker.cs index 3d13492aec7..722941594d5 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Linker.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Linker.cs @@ -7,46 +7,6 @@ namespace Xamarin.Android.Tasks { public partial class MonoAndroidHelper { - public static string [] TargetFrameworkDirectories; - - internal static readonly string [] FrameworkEmbeddedJarLookupTargets = { - "Mono.Android.Support.v13.dll", - "Mono.Android.Support.v4.dll", - "Xamarin.Android.NUnitLite.dll", // AndroidResources - }; - internal static readonly string [] FrameworkEmbeddedNativeLibraryAssemblies = { - "Mono.Data.Sqlite.dll", - "Mono.Posix.dll", - }; - - public static bool IsFrameworkAssembly (string assembly) - { - return IsFrameworkAssembly (assembly, false); - } - - public static bool IsFrameworkAssembly (string assembly, bool checkSdkPath) - { - if (IsSharedRuntimeAssembly (assembly)) { - return true; - } - return TargetFrameworkDirectories == null || !checkSdkPath ? false : ExistsInFrameworkPath (assembly); - } - - public static bool IsSharedRuntimeAssembly (string assembly) - { - return Array.BinarySearch (Profile.SharedRuntimeAssemblies, Path.GetFileName (assembly), StringComparer.OrdinalIgnoreCase) >= 0; - } - - public static bool ExistsInFrameworkPath (string assembly) - { - return TargetFrameworkDirectories - // TargetFrameworkDirectories will contain a "versioned" directory, - // e.g. $prefix/lib/xamarin.android/xbuild-frameworks/MonoAndroid/v1.0. - // Trim off the version. - .Select (p => Path.GetDirectoryName (p.TrimEnd (Path.DirectorySeparatorChar))) - .Any (p => assembly.StartsWith (p, StringComparison.OrdinalIgnoreCase)); - } - static readonly char [] CustomViewMapSeparator = [';']; public static Dictionary> LoadCustomViewMapFile (string mapFile) diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs b/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs index b4daa6d8bc0..b4d03f1084f 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs @@ -415,6 +415,28 @@ public static ZipArchive ReadZipFile (string filename) } #if MSBUILD + public static bool IsFrameworkAssembly (ITaskItem assembly) + { + // Known %(FrameworkReferenceName) + var frameworkReferenceName = assembly.GetMetadata ("FrameworkReferenceName") ?? ""; + if (frameworkReferenceName == "Microsoft.Android") { + return true; // Mono.Android.dll, Java.Interop.dll, etc. + } + if (frameworkReferenceName.StartsWith ("Microsoft.NETCore.", StringComparison.OrdinalIgnoreCase)) { + return true; // BCL assemblies + } + + return IsFromAKnownRuntimePack (assembly); + } + + public static bool IsFromAKnownRuntimePack (ITaskItem assembly) + { + // Known %(NuGetPackageId) runtime pack names + string packageId = assembly.GetMetadata ("NuGetPackageId") ?? ""; + return packageId.StartsWith ("Microsoft.NETCore.App.Runtime.", StringComparison.Ordinal) || + packageId.StartsWith ("Microsoft.Android.Runtime.", StringComparison.Ordinal); + } + public static bool SaveMapFile (IBuildEngine4 engine, string mapFile, Dictionary map) { engine?.RegisterTaskObjectAssemblyLocal (mapFile, map, RegisteredTaskObjectLifetime.Build); diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/Profile.cs b/src/Xamarin.Android.Build.Tasks/Utilities/Profile.cs deleted file mode 100644 index 7c9e72a0a0b..00000000000 --- a/src/Xamarin.Android.Build.Tasks/Utilities/Profile.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System.Collections.Generic; - -namespace Xamarin.Android.Tasks -{ - public partial class Profile - { - public static readonly HashSet Sdk = new HashSet { - "mscorlib", - "System", - "System.Core", - "System.Data", - "System.EnterpriseServices", - "System.Net.Http", - "System.Runtime.Serialization", - "System.ServiceModel", - "System.ServiceModel.Web", - "System.Transactions", - "System.Web.Services", - "System.Xml", - "System.Xml.Linq", - "System.Json", - "System.Numerics", - "Microsoft.CSharp", - "Mono.CSharp", - "Mono.CompilerServices.SymbolWriter", - "Mono.Security", - "Mono.Data.Tds", - "Mono.Data.Sqlite", - }; - - // KEEP THIS SORTED ALPHABETICALLY, CASE-INSENSITIVE - public static readonly string[] ValidAbis = new[]{ - "arm64-v8a", - "armeabi-v7a", - "x86", - "x86_64", - }; - - public static readonly string[] ValidProfilers = new[]{ - "log", - }; - } -} - diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj index 7e4e38509b9..030deb6a015 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj @@ -44,7 +44,6 @@ - @@ -56,8 +55,6 @@ - - @@ -78,7 +75,6 @@ - Mono.Android\IntentFilterAttribute.cs diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets index f002c237b6c..23bccb376a9 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets @@ -8,7 +8,6 @@ <_SharedRuntimeBuildPath Condition=" '$(_SharedRuntimeBuildPath)' == '' ">$(XAInstallPrefix)xbuild-frameworks\MonoAndroid\ - <_GeneratedProfileClass>$(MSBuildThisFileDirectory)$(IntermediateOutputPath)Profile.g.cs $(ResolveReferencesDependsOn); _GenerateSupportedPlatforms; @@ -163,16 +162,6 @@ PreserveNewest - - <_SharedRuntimeAssemblies Include="@(MonoProfileAssembly->'$(_SharedRuntimeBuildPath)v1.0\%(Identity)')" /> - <_SharedRuntimeAssemblies Include="$(_SharedRuntimeBuildPath)v1.0\Mono.Data.Sqlite.dll" /> - <_SharedRuntimeAssemblies Include="$(_SharedRuntimeBuildPath)v1.0\Mono.Posix.dll" /> - <_SharedRuntimeAssemblies Include="$(_SharedRuntimeBuildPath)v1.0\Java.Interop.dll" /> - <_SharedRuntimeAssemblies Include="$(_SharedRuntimeBuildPath)v1.0\System.EnterpriseServices.dll" /> - <_SharedRuntimeAssemblies Include="$(_SharedRuntimeBuildPath)$(AndroidFrameworkVersion)\Mono.Android.Export.dll" /> - - <_SharedRuntimeAssemblies Include="$(_SharedRuntimeBuildPath)$(AndroidFrameworkVersion)\Mono.Android.dll" /> - - - - - - - - Date: Thu, 9 Jan 2025 09:03:47 -0600 Subject: [PATCH 2/5] Restore some checks --- .../ApplyPreserveAttribute.cs | 2 +- src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs | 3 +++ .../PreserveApplications.cs | 3 +++ .../PreserveExportedTypes.cs | 3 +++ .../PreserveJavaExceptions.cs | 3 +++ .../StripEmbeddedLibraries.cs | 5 +++-- .../MonoDroid.Tuner/FixAbstractMethodsStep.cs | 3 +++ .../MonoDroid.Tuner/RemoveResourceDesignerStep.cs | 3 +++ .../Tasks/ResolveLibraryProjectImports.cs | 4 ++++ .../Utilities/MonoAndroidHelper.Linker.cs | 13 +++++++++++++ .../Utilities/MonoAndroidHelper.cs | 10 +++++++--- 11 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs b/src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs index 3f2d43e75d7..a951e57b467 100644 --- a/src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs +++ b/src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs @@ -25,7 +25,7 @@ public override SubStepTargets Targets { public override bool IsActiveFor (AssemblyDefinition assembly) { - return Annotations.GetAction (assembly) == AssemblyAction.Link; + return !MonoAndroidHelper.IsFrameworkAssembly (assembly) && Annotations.GetAction (assembly) == AssemblyAction.Link; } public override void ProcessType (TypeDefinition type) diff --git a/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs b/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs index d4b73ee5ccb..4718cca0aa6 100644 --- a/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs +++ b/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs @@ -26,6 +26,9 @@ public override void Initialize (LinkContext context, MarkContext markContext) bool IsActiveFor (AssemblyDefinition assembly) { + if (MonoAndroidHelper.IsFrameworkAssembly (assembly)) + return false; + return assembly.MainModule.HasTypeReference ("System.Net.Http.HttpMessageHandler") || assembly.MainModule.HasTypeReference ("Java.Lang.Object") || assembly.MainModule.HasTypeReference ("Android.Util.IAttributeSet"); diff --git a/src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs b/src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs index f420635f07d..5e3e1b0c44d 100644 --- a/src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs +++ b/src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs @@ -4,7 +4,10 @@ using Mono.Linker; using Mono.Linker.Steps; + using Mono.Tuner; +using Mobile.Tuner; + using Mono.Cecil; namespace MonoDroid.Tuner { diff --git a/src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs b/src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs index 99188842b87..38c1f2e8d0c 100644 --- a/src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs +++ b/src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs @@ -24,6 +24,9 @@ public override SubStepTargets Targets { public override bool IsActiveFor (AssemblyDefinition assembly) { + if (MonoAndroidHelper.IsFrameworkAssembly (assembly)) + return false; + return assembly.MainModule.HasTypeReference ("Java.Interop.ExportAttribute") || assembly.MainModule.HasTypeReference ("Java.Interop.ExportFieldAttribute"); } diff --git a/src/Microsoft.Android.Sdk.ILLink/PreserveJavaExceptions.cs b/src/Microsoft.Android.Sdk.ILLink/PreserveJavaExceptions.cs index 6750c001c2f..1c0ad8f0898 100644 --- a/src/Microsoft.Android.Sdk.ILLink/PreserveJavaExceptions.cs +++ b/src/Microsoft.Android.Sdk.ILLink/PreserveJavaExceptions.cs @@ -4,7 +4,10 @@ using Mono.Linker; using Mono.Linker.Steps; + using Mono.Tuner; +using Mobile.Tuner; + using Mono.Cecil; namespace MonoDroid.Tuner { diff --git a/src/Microsoft.Android.Sdk.ILLink/StripEmbeddedLibraries.cs b/src/Microsoft.Android.Sdk.ILLink/StripEmbeddedLibraries.cs index 0a0adaf39bb..041bead3f92 100644 --- a/src/Microsoft.Android.Sdk.ILLink/StripEmbeddedLibraries.cs +++ b/src/Microsoft.Android.Sdk.ILLink/StripEmbeddedLibraries.cs @@ -17,12 +17,13 @@ protected override void ProcessAssembly (AssemblyDefinition assembly) if (action == AssemblyAction.Skip || action == AssemblyAction.Delete) return; - var fileName = assembly.Name.Name + ".dll"; + if (MonoAndroidHelper.IsFrameworkAssembly (assembly)) + return; bool assembly_modified = false; foreach (var mod in assembly.Modules) { foreach (var r in mod.Resources.ToArray ()) { if (ShouldStripResource (r)) { - Context.LogMessage ($" Stripped {r.Name} from {fileName}"); + Context.LogMessage ($" Stripped {r.Name} from {assembly.Name.Name}.dll"); mod.Resources.Remove (r); assembly_modified = true; } diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs index d9bf7b6d281..5c7bf7c9420 100644 --- a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs @@ -52,6 +52,9 @@ bool CheckShouldProcessAssembly (AssemblyDefinition assembly) if (!Annotations.HasAction (assembly)) Annotations.SetAction (assembly, AssemblyAction.Skip); + if (MonoAndroidHelper.IsFrameworkAssembly (assembly)) + return false; + CheckAppDomainUsage (assembly, (string msg) => #if ILLINK Context.LogMessage (MessageContainer.CreateCustomWarningMessage (Context, msg, 6200, new MessageOrigin (), WarnVersion.ILLink5)) diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveResourceDesignerStep.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveResourceDesignerStep.cs index ba22a283e46..3c1e1a71a9d 100644 --- a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveResourceDesignerStep.cs +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveResourceDesignerStep.cs @@ -93,6 +93,9 @@ internal override bool ProcessAssemblyDesigner (AssemblyDefinition assembly) { if (mainDesigner == null) return false; + if (MonoAndroidHelper.IsFrameworkAssembly (assembly)) + return false; + LogMessage ($" Fixing up {assembly.Name.Name}"); TypeDefinition localDesigner = null; CustomAttribute designerAttribute; diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs index 1b735ee61dc..2ac5c3e8334 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs @@ -170,6 +170,10 @@ void Extract ( foreach (var assemblyItem in Assemblies) { var assemblyPath = assemblyItem.ItemSpec; var fileName = Path.GetFileName (assemblyPath); + if (MonoAndroidHelper.IsFrameworkAssembly (fileName)) { + Log.LogDebugMessage ($"Skipping framework assembly '{fileName}'."); + continue; + } if (!File.Exists (assemblyPath)) { Log.LogDebugMessage ($"Skipping non-existent dependency '{assemblyPath}'."); continue; diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Linker.cs b/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Linker.cs index 722941594d5..c6ea65ac52b 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Linker.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.Linker.cs @@ -2,11 +2,24 @@ using System.Collections.Generic; using System.Linq; using System.IO; +using Mono.Cecil; namespace Xamarin.Android.Tasks { public partial class MonoAndroidHelper { + static readonly HashSet KnownAssemblyNames = new (StringComparer.Ordinal) { + "Mono.Android", + "Mono.Android.Export", + "Java.Interop", + }; + + public static bool IsFrameworkAssembly (AssemblyDefinition assembly) => + KnownAssemblyNames.Contains (assembly.Name.Name); + + public static bool IsFrameworkAssembly (string assembly) => + KnownAssemblyNames.Contains (Path.GetFileNameWithoutExtension (assembly)); + static readonly char [] CustomViewMapSeparator = [';']; public static Dictionary> LoadCustomViewMapFile (string mapFile) diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs b/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs index b4d03f1084f..350f27d9845 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs @@ -417,21 +417,25 @@ public static ZipArchive ReadZipFile (string filename) #if MSBUILD public static bool IsFrameworkAssembly (ITaskItem assembly) { + // Known assembly names: Mono.Android, Java.Interop, etc. + if (IsFrameworkAssembly (assembly.ItemSpec)) + return true; + // Known %(FrameworkReferenceName) var frameworkReferenceName = assembly.GetMetadata ("FrameworkReferenceName") ?? ""; if (frameworkReferenceName == "Microsoft.Android") { - return true; // Mono.Android.dll, Java.Interop.dll, etc. + return true; // Microsoft.Android assemblies } if (frameworkReferenceName.StartsWith ("Microsoft.NETCore.", StringComparison.OrdinalIgnoreCase)) { return true; // BCL assemblies } - + + // Known %(NuGetPackageId) runtime pack names return IsFromAKnownRuntimePack (assembly); } public static bool IsFromAKnownRuntimePack (ITaskItem assembly) { - // Known %(NuGetPackageId) runtime pack names string packageId = assembly.GetMetadata ("NuGetPackageId") ?? ""; return packageId.StartsWith ("Microsoft.NETCore.App.Runtime.", StringComparison.Ordinal) || packageId.StartsWith ("Microsoft.Android.Runtime.", StringComparison.Ordinal); From 3adcd2a597f2e21c1c567520a61f40ce3732692b Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Thu, 9 Jan 2025 09:11:17 -0600 Subject: [PATCH 3/5] Restore a little more code --- src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs | 7 ++++++- .../Tasks/GenerateResourceDesigner.cs | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs b/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs index 4718cca0aa6..dc39f615a08 100644 --- a/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs +++ b/src/Microsoft.Android.Sdk.ILLink/MarkJavaObjects.cs @@ -104,7 +104,7 @@ public void ProcessType (TypeDefinition type) // If a user overrode a method, we need to preserve it, // because it won't be referenced anywhere, but it will // be called from Java - if (type.HasMethods) { + if (IsUserType (type) && type.HasMethods) { foreach (var method in type.Methods.Where (m => m.Overrides != null)) PreserveMethod (type, method); } @@ -392,6 +392,11 @@ static bool IsImplementor (TypeDefinition type, IMetadataResolver cache) return type.Name.EndsWith ("Implementor", StringComparison.Ordinal) && type.Inherits ("Java.Lang.Object", cache); } + static bool IsUserType (TypeDefinition type) + { + return !MonoAndroidHelper.IsFrameworkAssembly (type.Module.Assembly); + } + void PreserveImplementor (TypeDefinition type) { if (!type.HasMethods) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesigner.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesigner.cs index 4afb58ccbe7..5986793d0a8 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesigner.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesigner.cs @@ -111,6 +111,10 @@ public override bool RunTask () foreach (var assembly in References) { var assemblyPath = assembly.ItemSpec; var fileName = Path.GetFileName (assemblyPath); + if (MonoAndroidHelper.IsFrameworkAssembly (fileName)) { + Log.LogDebugMessage ($"Skipping framework assembly '{fileName}'."); + continue; + } if (!File.Exists (assemblyPath)) { Log.LogDebugMessage ($"Skipping non-existent dependency '{assemblyPath}'."); continue; From 69aa87f53de6381b9bacb8b433f136a5ffe2ed9d Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Thu, 9 Jan 2025 09:18:25 -0600 Subject: [PATCH 4/5] build errors --- src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs | 7 ++----- .../PreserveJavaExceptions.cs | 7 ++----- .../Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs | 8 +++----- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs b/src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs index 5e3e1b0c44d..ddf99cf90df 100644 --- a/src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs +++ b/src/Microsoft.Android.Sdk.ILLink/PreserveApplications.cs @@ -1,14 +1,11 @@ using System; using System.Collections; using System.Linq; - +using Mono.Cecil; using Mono.Linker; using Mono.Linker.Steps; - using Mono.Tuner; -using Mobile.Tuner; - -using Mono.Cecil; +using Xamarin.Android.Tasks; namespace MonoDroid.Tuner { diff --git a/src/Microsoft.Android.Sdk.ILLink/PreserveJavaExceptions.cs b/src/Microsoft.Android.Sdk.ILLink/PreserveJavaExceptions.cs index 1c0ad8f0898..d60b064ef32 100644 --- a/src/Microsoft.Android.Sdk.ILLink/PreserveJavaExceptions.cs +++ b/src/Microsoft.Android.Sdk.ILLink/PreserveJavaExceptions.cs @@ -1,14 +1,11 @@ using System; using System.Collections; using System.Linq; - +using Mono.Cecil; using Mono.Linker; using Mono.Linker.Steps; - using Mono.Tuner; -using Mobile.Tuner; - -using Mono.Cecil; +using Xamarin.Android.Tasks; namespace MonoDroid.Tuner { diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs index 5c7bf7c9420..01890ba8c56 100644 --- a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs @@ -2,15 +2,13 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; - -using Mono.Cecil; - using Java.Interop.Tools.Cecil; - +using Mono.Cecil; using Mono.Linker; using Mono.Linker.Steps; - using Mono.Tuner; +using Xamarin.Android.Tasks; + #if ILLINK using Resources = Microsoft.Android.Sdk.ILLink.Properties.Resources; #else // !ILLINK From 065adf9dbec284ba5005deae7b2899c56476394b Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Thu, 9 Jan 2025 09:43:45 -0600 Subject: [PATCH 5/5] Missed two files --- src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs | 5 ++--- src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs | 7 +++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs b/src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs index a951e57b467..4c4af5ab10e 100644 --- a/src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs +++ b/src/Microsoft.Android.Sdk.ILLink/ApplyPreserveAttribute.cs @@ -2,11 +2,10 @@ using System.Collections; using System.Collections.Generic; using System.Linq; - +using Mono.Cecil; using Mono.Linker; using Mono.Linker.Steps; - -using Mono.Cecil; +using Xamarin.Android.Tasks; namespace Microsoft.Android.Sdk.ILLink { diff --git a/src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs b/src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs index 38c1f2e8d0c..1b2664dc953 100644 --- a/src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs +++ b/src/Microsoft.Android.Sdk.ILLink/PreserveExportedTypes.cs @@ -2,12 +2,11 @@ using System.Collections; using System.Collections.Generic; using System.Linq; - +using Microsoft.Android.Sdk.ILLink; +using Mono.Cecil; using Mono.Linker; using Mono.Linker.Steps; - -using Mono.Cecil; -using Microsoft.Android.Sdk.ILLink; +using Xamarin.Android.Tasks; namespace Mono.Tuner {