-
Notifications
You must be signed in to change notification settings - Fork 565
[Draft][Proposal] Trimmable Type Map #10757
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
base: main
Are you sure you want to change the base?
Conversation
|
|
||
| - Debug and Release builds using CoreCLR or NativeAOT runtime | ||
| - All Java peer types: user classes, SDK bindings, interfaces with invokers | ||
| - `[Register]` and `[Export]` attribute methods |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that we are already considering [Export] not trimmer safe at all -- it currently emits trimmer warnings.
It isn't a required feature for Android, as you can just make an interface instead, and the entire API will be strongly typed. [Export] is basically C# -> strings -> generate a Java class/method with your string.
An example of removing [Export]:
So, we could consider improving [Export] future, out of scope.
|
I think this looks great, and I don't see any major problems for macios either! |
| **Key Insight:** In the legacy system, most types are only preserved if they're referenced by user code. The legacy `MarkJavaObjects` only unconditionally marks: | ||
| 1. Types with `[Activity]`, `[Service]`, `[BroadcastReceiver]`, `[ContentProvider]`, `[Application]`, `[Instrumentation]` attributes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you could technically, not use these attributes and put them in your AndroidManifest.xml file manually.
That means that AndroidManifest.xml can define additional "roots" other than the application assembly. I don't think people would commonly do this, but maybe we should mention it's not trimmer safe -- they'd need to preserve the type another way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already scan layouts and root the types that appear in those XML files. We can easily scan AndroidManifest.xml as well.
This specification defines the architecture for enabling Java-to-.NET interoperability in .NET Android applications using the .NET Type Mapping API. The design is fully compatible with Native AOT and trimming.
Expands on dotnet/runtime#120121
Proof of concept
See https://github.com/dotnet/android/compare/dev/simonrozsival/trimmable-typemap
Core idea
Each Java peer type is registered using assembly-level attributes:
The
*_Proxytypes are generated attribute classes which is applied to itself:Why this works:
GetCustomAttribute<T>()instantiates attributes in an trimming and AOT-safe mannertrimTargetparameter ensures the mapping is preserved when the target type survives trimming/cc @jtschuster
Related