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

[trimming] preserve custom views and $(AndroidHttpClientHandlerType) #8944

Closed
wants to merge 1 commit into from

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented May 14, 2024

Fixes: #8797
Context: https://github.com/dotnet/runtime/blob/714a4420805ed53c311b05381c83c88894100fa9/docs/tools/illink/data-formats.md

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.

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.
Comment on lines +47 to +60
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)
));
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This code doesn't work yet, as the file contains:

mono.android_test.library.CustomTextView;D:\src\xamarin-android\tests\Mono.Android-Tests\Runtime-Microsoft.Android.Sdk\obj\Release\net9.0-android\lp\6\jl\res\layout\upper_lower_custom.xml
mono.android_test.library.CustomTextView;obj\Release\net9.0-android\res\layout\upper_lower_custom.xml
Mono.Android_Test.Library.CustomTextView;D:\src\xamarin-android\tests\Mono.Android-Tests\Runtime-Microsoft.Android.Sdk\obj\Release\net9.0-android\lp\6\jl\res\layout\upper_lower_custom.xml
Mono.Android_Test.Library.CustomTextView;obj\Release\net9.0-android\res\layout\upper_lower_custom.xml
Mono.Android_Test.Library.CustomTextView;obj\Release\net9.0-android\res\layout\uppercase_custom.xml

I need the full type name like Mono.Android_Test.Library.CustomTextView, Mono.Android_Test.Library.

Copy link
Contributor

Choose a reason for hiding this comment

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

Using the acw_map.txt file might allow you to map that info back. You will probably need a new file though.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to piece together the information with existing files:

  • customview-map.txt has an entry with a type name and file name like upper_lower_custom.xml
  • <ConvertResourcesCases/> has $(MonoAndroidResDirIntermediate);@(_LibraryResourceDirectories)
  • @(_LibraryResourceDirectories) has OriginalFile = D:\src\xamarin-android\tests\Mono.Android-Tests\Mono.Android-Test.Library\bin\Release\net9.0-android\Mono.Android-Test.Library.NET.aar
  • Mono.Android-Test.Library.NET.aar is created based on AndroidResource files in Mono.Android-Test.Library.NET.dll

So, it doesn't seem like we even have data to map back to the assembly name. I'll keep thinking about this one.

@jonathanpeppers
Copy link
Member Author

Closing this in favor of a different approach:

I'll keep this branch around if it's ever useful in the future.

@jonathanpeppers
Copy link
Member Author

I think this idea is going to work:

@github-actions github-actions bot locked and limited conversation to collaborators Jun 16, 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.

When using TrimMode=link custom AndroidHttpClientHandlerType is not preserved
2 participants