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] add sample that runs successfully #9636

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Dec 19, 2024

This is an Android NativeAOT sample that runs successfully.

It currently relies on [InternalsVisibleTo("NativeAOT")] in Mono.Android.dll for things like:

  • Java.Interop.TypeManagerMapDictionaries.JniToManaged

  • Java.Interop.TypeManagerMapDictionaries.ManagedToJni

  • Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.ToToJniName()

  • Android.Runtime.JNIEnvInit.InitializeNativeAot()

There are a couple MSBuild changes still left:

  • $(_ExtraTrimmerArgs) needs to be specified for trimmer warnings to not be displayed 2x. This is the same thing done in xamarin/xamarin-macios.

  • @(TrimmerRootAssembly) needs to be set for illink's "already trimmed" output for ILC. We exclude System.Private.CoreLib.dll from this list.

This is an Android NativeAOT sample that runs successfully.

It currently relies on `[InternalsVisibleTo("NativeAOT")]` in
`Mono.Android.dll` for things like:

* `Java.Interop.TypeManagerMapDictionaries.JniToManaged`

* `Java.Interop.TypeManagerMapDictionaries.ManagedToJni`

* `Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.ToToJniName()`

* `Android.Runtime.JNIEnvInit.InitializeNativeAot()`

There are a couple MSBuild changes still left:

* `$(_ExtraTrimmerArgs)` needs to be specified for trimmer warnings to
  show. This is the same thing done in xamarin/xamarin-macios.

* `@(TrimmerRootAssembly)` needs to be set for illink's "already
  trimmed" output for ILC. We exclude `System.Private.CoreLib.dll`
  from this list.
@@ -49,6 +49,8 @@ This file contains the NativeAOT-specific MSBuild logic for .NET for Android.
-->
<_OriginalSuppressTrimAnalysisWarnings>$(SuppressTrimAnalysisWarnings)</_OriginalSuppressTrimAnalysisWarnings>
<SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
<!-- Ensure ILLink respects the value of SuppressTrimAnalysisWarnings -->
Copy link
Member

Choose a reason for hiding this comment

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

This suggests that <ILLink/> doesn't currently respect $(SuppressTrimAnalysisWarnings). Is there a bug for this?

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 happening because we are running both ILLink and ILC. We want SuppressTrimAnalysisWarnings=false by default so we get warnings from ILC.

But we don't want 2x trimmer warnings, so we have to turn it off and back on for ILC. Unfortunately, $(_ExtraTrimmerArgs) is already set.

Same approach here:

@samhouts samhouts requested a review from Copilot December 19, 2024 18:33

Choose a reason for hiding this comment

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

Copilot reviewed 20 out of 31 changed files in this pull request and generated no comments.

Files not reviewed (11)
  • samples/NativeAOT/AndroidManifest.xml: Language not supported
  • samples/NativeAOT/NativeAOT.csproj: Language not supported
  • samples/NativeAOT/Resources/layout/activity_main.xml: Language not supported
  • samples/NativeAOT/Resources/mipmap-anydpi-v26/appicon.xml: Language not supported
  • samples/NativeAOT/Resources/mipmap-anydpi-v26/appicon_round.xml: Language not supported
  • samples/NativeAOT/Resources/values/ic_launcher_background.xml: Language not supported
  • samples/NativeAOT/Resources/values/strings.xml: Language not supported
  • src/Mono.Android/Properties/AssemblyInfo.cs.in: Language not supported
  • src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.NativeAOT.targets: Language not supported
  • src/Mono.Android/Android.Runtime/JNIEnvInit.cs: Evaluated as low risk
  • samples/NativeAOT/NativeAotTypeManager.cs: Evaluated as low risk
Comments suppressed due to low confidence (1)

samples/NativeAOT/JavaInteropRuntime.cs:34

  • The init method initializes the JreRuntimeOptions and calls JNIEnvInit.InitializeNativeAot(runtime), but there are no tests covering this behavior.
static void init (IntPtr jnienv, IntPtr klass)
@dotnet dotnet deleted a comment from azure-pipelines bot Dec 19, 2024
@dotnet dotnet deleted a comment from azure-pipelines bot Dec 19, 2024
@dotnet dotnet deleted a comment from azure-pipelines bot Dec 19, 2024
@dotnet dotnet deleted a comment from azure-pipelines bot Dec 19, 2024
@dotnet dotnet deleted a comment from azure-pipelines bot Dec 20, 2024
@dotnet dotnet deleted a comment from azure-pipelines bot Dec 20, 2024
jonpryor added a commit that referenced this pull request Dec 20, 2024
Context: xamarin/monodroid@e318861
Context: 130905e
Context: de04316
Context: #9636

In the beginning there was Mono for Android, which had a set of
`Mono.Android.dll` assemblies (one per supported API level), each of
which contained "duplicated" binding logic: each API level had its
own `Java.Lang.Object`, `Android.Runtime.JNIEnv`, etc.

dotnet/java-interop started, in part, as a way to "split out" the
core integration logic, so that it *wouldn't* need to be duplicated
across every assembly.  As part of this, it introduced its own core
abstractions, notably `Java.Interop.IJavaPeerable` and
`Java.Interop.JavaObject`.

When dotnet/java-interop was first introduced into Xamarin.Android,
with xamarin/monodroid@e318861e, the integration was incomplete.
Integration continued with 130905e, allowing unit tests within
`Java.Interop-Tests.dll` to run within Xamarin.Android and
construction of instances of e.g. `JavaInt32Array`, but one large
piece of integration remained:

Moving GC bridge code *out* of `Java.Lang.Object`, and instead
relying on `Java.Interop.JavaObject`, turning this:

	namespace Java.Lang {
	    public partial class Object : System.Object, IJavaPeerable /* … */ {
	    }
	}

into this:

	namespace Java.Lang {
	    public partial class Object : Java.Interop.JavaObject, IJavaPeerable /* … */ {
	    }
	}

*Why*?  In part because @jonpryor has wanted to do this for literal
years at this point, but also in part because of #9636
and related efforts to use Native AOT, which involves avoiding /
bypassing `DllImportAttribute` invocations (for now, everything
touched by Native AOT becomes a single `.so` binary, which we don't
know the name of).  Avoiding P/Invoke means *embracing* and extending
existing Java.Interop constructs (e.g. de04316).

In addition to altering the base types of `Java.Lang.Object` and
`Java.Lang.Throwable`:

  * Remove `handle` and related fields from `Java.Lang.Object` and
    `Java.Lang.Throwable`.

  * Update `PreserveLists/Mono.Android.xml` so that the removed
    fields are note preserved.

  * Rename `JNIenvInit.AndroidValueManager` to
    `JNIEnvInit.ValueManager`, and change its type to
    `JniRuntime.JniValueManager`.  This is to help "force" usage of
    `JnIRuntime.JniValueManager` in more places, as we can't
    currently use `AndroidValueManager` in Native AOT (P/Invokes!).

  * Cleanup: Remove `JNIEnv.Exit()` and related code.  These were
    used by the Android Designer, which is no longer supported.

  * Update (`internal`) interface `IJavaObjectEx` to remove
    constructs present on `IJavaPeerable.`

Known issues:

  * `Java.Lang.Throwable(IntPtr, JniHandleOwnership)` invocation
    will result in construction of an "extra" `java.lang.Throwable`
    instance on the Java side, which will be immediately discarded.
    This is because it uses the `JavaException(string, Exception)`
    constructor, which implicitly creates a Java peer.

    We may need dotnet/java-interop changes to better address this.
@dotnet dotnet deleted a comment from azure-pipelines bot Dec 20, 2024
@jonathanpeppers
Copy link
Member Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

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