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

[nativeaot] introduce Microsoft.Android.Runtime.NativeAOT.dll #9760

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jonathanpeppers
Copy link
Member

This moves:

  • C# code from samples/NativeAOT to a new assembly, Microsoft.Android.Runtime.NativeAOT.dll.

  • Java code from samples/NativeAOT to Xamarin.Android.Build.Tasks, to be generated at build time.

  • C# code from Java.Interop.Environment.csproj has been trimmed down and moved to Microsoft.Android.Runtime.NativeAOT.dll as well.

Comment on lines +24 to +26
// TODO: pass these in from an application
//["my/MainActivity"] = typeof (MainActivity),
//["my/MainApplication"] = typeof (MainApplication),
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 is the major TODO, preventing this from working...

Need to figure out how we could make the app pass these in.

Copy link
Member

Choose a reason for hiding this comment

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

@jonathanpeppers asked:

short of "implement a C# typemap" is there a quick idea for doing this part?

I think the short answer is "no".

The longer answer is "what's the MVP for type maps?"

Maybe not immediately, but eventually type maps need to be able to access/reference internal types outside the app assembly, so source generators as a technique are out. Plus, source generators run on pre-linked input.

I think the MVP is:

  1. Update NativeAotTypeManager to have, and ensure survives the linker:

    partial class NativeAotTypeManager {
        static IDictionary<string, Type> TypeMappings;
    
        static NativeAotTypeManager()
        {
            InitTypeMappings ();
        }
    
        static void InitTypeMappings ()
        {
            TypeMappings = new Dictionary<string, Type>();
        }
    }
  2. Linker pipeline step (yay!) which processes the linked assemblies, so that it only references types which are actually used.

  3. (2) uses Cecil to find NativeAotTypeManager and replace the body of NativeAotTypeManager.InitTypeMappings() with the equivalent of::

    partial class NativeAotTypeManager {
    
        static void InitTypeMappings ()
        {
            TypeMappings = new Dictionary<string, Type>();
            TypeMappings.Add ("net/dot/jni/ManagedPeer", typeof (Java.Interop.ManagedPeer));
            TypeMappings.Add ("java/lang/Object", typeof (Java.Lang.Object));
            // …
        }
    }

I assume (lol) that most of that should be reasonably straightforward to implement given our existing codebase. We already have linker pipeline steps, it's "reasonable" to figure out the IL version of typeof(T) and Dictionary.Add() calls.

This moves:

* C# code from `samples/NativeAOT` to a new assembly,
  `Microsoft.Android.Runtime.NativeAOT.dll`.

* Java code from `samples/NativeAOT` to `Xamarin.Android.Build.Tasks`,
  to be generated at build time.

* C# code from `Java.Interop.Environment.csproj` has been trimmed down
  and moved to `Microsoft.Android.Runtime.NativeAOT.dll` as well.
@jonathanpeppers jonathanpeppers force-pushed the dev/peppers/microsoft.android.nativeaot branch from 483a7d7 to 6532402 Compare February 5, 2025 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants