forked from dotnet/android
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[trimming] preserve custom views and
$(AndroidHttpClientHandlerType)
Fixes: dotnet#8797 Context: https://github.com/dotnet/runtime/blob/714a4420805ed53c311b05381c83c88894100fa9/docs/tools/illink/data-formats.md Context: Here are two cases `TrimMode=full` can break applications: * `AndroidHttpClientHandlerType` set to a custom type * Custom views (Android `.xml`) that are not referenced in C# code For either of these cases, the traditional approach in Xamarin.Android would have been to author a trimmer step to preserve these types. However, thinking about NativeAOT in the future, it is more "future-proof" to write an MSBuild task that generates trimmer `.xml` to preserve these types. This same XML file could be passed to the NativeAOT trimmer (`Ilc`). If we had a custom trimmer step, we would have to reimplement the same logic for the NativeAOT trimmer. This is WIP as custom views are not yet preserved.
- Loading branch information
1 parent
98c1063
commit 768005e
Showing
7 changed files
with
136 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/Xamarin.Android.Build.Tasks/Tasks/GenerateILLinkXml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System.Xml.Linq; | ||
using Microsoft.Android.Build.Tasks; | ||
using Microsoft.Build.Framework; | ||
|
||
namespace Xamarin.Android.Tasks; | ||
|
||
public class GenerateILLinkXml : AndroidTask | ||
{ | ||
public override string TaskPrefix => "GILX"; | ||
|
||
[Required] | ||
public string AndroidHttpClientHandlerType { get; set; } | ||
|
||
[Required] | ||
public string CustomViewMapFile { get; set; } | ||
|
||
[Required] | ||
public string OutputFile { get; set; } | ||
|
||
public override bool RunTask () | ||
{ | ||
string assemblyName, typeName; | ||
|
||
var index = AndroidHttpClientHandlerType.IndexOf (','); | ||
if (index != -1) { | ||
typeName = AndroidHttpClientHandlerType.Substring (0, index).Trim (); | ||
assemblyName = AndroidHttpClientHandlerType.Substring (index + 1).Trim (); | ||
} else { | ||
typeName = AndroidHttpClientHandlerType; | ||
assemblyName = "Mono.Android"; | ||
} | ||
|
||
// public parameterless constructors | ||
// example: https://github.com/dotnet/runtime/blob/039d2ecb46687e89337d6d629c295687cfe226be/src/mono/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.xml | ||
var ctor = new XElement ("method", new XAttribute("signature", "System.Void .ctor()")); | ||
|
||
XElement linker; | ||
var doc = new XDocument ( | ||
linker = new XElement ("linker", | ||
new XElement ("assembly", | ||
new XAttribute ("fullname", assemblyName), | ||
new XElement ("type", new XAttribute ("fullname", typeName), ctor) | ||
) | ||
) | ||
); | ||
|
||
var customViewMap = MonoAndroidHelper.LoadCustomViewMapFile (BuildEngine4, CustomViewMapFile); | ||
foreach (var pair in customViewMap) { | ||
index = pair.Key.IndexOf (','); | ||
if (index == -1) | ||
continue; | ||
|
||
typeName = pair.Key.Substring (0, index).Trim (); | ||
assemblyName = pair.Key.Substring (index + 1).Trim (); | ||
|
||
linker.Add (new XElement ("assembly", | ||
new XAttribute ("fullname", assemblyName), | ||
new XElement ("type", new XAttribute ("fullname", typeName), ctor) | ||
)); | ||
} | ||
|
||
if (doc.SaveIfChanged (OutputFile)) { | ||
Log.LogDebugMessage ($"Saving {OutputFile}"); | ||
} else { | ||
Log.LogDebugMessage ($"{OutputFile} is unchanged. Skipping."); | ||
} | ||
|
||
return !Log.HasLoggedErrors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters