Skip to content

Hook up more integrations to NativeAOT with Android RIDs #10402

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

jkoritzinsky
Copy link
Member

Try hooking up the Android crypto stack, GC integration, and logging to NativeAOT correctly.

cc: @filipnavara @jtschuster

@jonathanpeppers
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

os-bridge.cc
runtime-util.cc
typemap.cc
xamarin_getifaddrs.cc
Copy link
Member

@filipnavara filipnavara Aug 12, 2025

Choose a reason for hiding this comment

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

This will produce conflict with #10385. Not a big deal, just wanted to avoid dragging unused code into newly supported runtimes.


<!-- The list of hacks below should go away when we have NativeAOT.android-* packs -->
<!-- HACK: we are android-arm64, so this is required for the right path(s) to be found -->
<_OriginalRuntimeIdentifier>$(RuntimeIdentifier)</_OriginalRuntimeIdentifier>
Copy link
Member

Choose a reason for hiding this comment

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

There are some usages of _OriginalRuntimeIdentifier below that need to be updated.

@@ -122,16 +110,13 @@ This file contains the NativeAOT-specific MSBuild logic for .NET for Android.
<ResolvedFileToPublish Include="$(_NdkSysrootDir)libc++_shared.so" RuntimeIdentifier="$(_OriginalRuntimeIdentifier)" />
Copy link
Member

Choose a reason for hiding this comment

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

This line and the one above need to use RuntimeIdentifier instead of _OriginalRuntimeIdentifier.

@filipnavara
Copy link
Member

filipnavara commented Aug 12, 2025

I'll need to dig a bit deeper but the subset linking of the native lib is still not quite correct:

08-12 10:30:01.122 26235 26235 E AndroidRuntime: java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "_monodroid_detect_cpu_and_architecture" referenced by "/data/app/~~grvY0oxLa6M-yS3DDLG0hg==/com.emclient.mailclient-XUnDyFyf6XZEZfGtl53aBg==/lib/x86_64/libMailClient.Mobile.Android.so"...
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at java.lang.Runtime.loadLibrary0(Runtime.java:1090)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at java.lang.Runtime.loadLibrary0(Runtime.java:1012)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at java.lang.System.loadLibrary(System.java:1765)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at net.dot.jni.nativeaot.JavaInteropRuntime.<clinit>(JavaInteropRuntime.java:8)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at net.dot.jni.nativeaot.NativeAotRuntimeProvider.attachInfo(NativeAotRuntimeProvider.java:43)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at android.app.ActivityThread.installProvider(ActivityThread.java:8642)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at android.app.ActivityThread.installContentProviders(ActivityThread.java:8157)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7814)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2546)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:110)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at android.os.Looper.loopOnce(Looper.java:248)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:338)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:9067)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:593)
08-12 10:30:01.122 26235 26235 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:932)

...and even if it tried to link to the native library we are still missing some bits:

    ld.lld : error : undefined hidden symbol: xamarin::android::Helpers::abort_application(_LogCategories, char const*, bool, std::__ndk1::source_location)

UPD: Nvm, I was missing xa-shared-bits in the second attempt.

@filipnavara
Copy link
Member

filipnavara commented Aug 12, 2025

I have a heavily hacked up build locally that was able to pass startup of our app:

image

Aside from all the comments above and solving general linking breakage / undefined symbols, we will need to embrace GCUserPeerable because NativeAOT builds use different interop generation mode (XAJavaInterop1 vs JavaInterop1).

The garbage collection part seems to work but for some reason it's triggered way too often to the point of feeling like running all the time. It will need to be investigated, the startup takes literally minute[s] because of all the triggered collections. (UPD: Could be It is just uninitialized value for GREF threshold).

@filipnavara
Copy link
Member

Here's a rough commit with the changes I have locally to get this working: filipnavara@4a59c50

It still needs a lot of cleanup. We also need updated runtime packs to get it working end-to-end.

@jonathanpeppers
Copy link
Member

@jkoritzinsky FYI the latest Maestro bump, it seems like we need to make changes like:

This allows the NativeAOT runtime packs (with proper Android RIDs!) to work.

Do we need to add the two Android RIDs to this line:

That made things work for me locally, but CI is ongoing.

@jonathanpeppers
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@akoeplinger
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jkoritzinsky
Copy link
Member Author

I'm looking at the NAOT failures and it looks like it can't find the native files in the package even though they are present in the NativeAOT runtime package.

I did notice that the runtime package is built with a -ci version and the test expects it to have a stable version. Is that expected?

@jonathanpeppers
Copy link
Member

Ok, the data/RuntimeList.xml file looks OK:

<FileList TargetFrameworkIdentifier=".NETStandard" TargetFrameworkVersion="2.0" FrameworkName="Microsoft.Android.Runtimes" Name=".NET 10.0 - Microsoft.Android.Runtimes">
  <File Type="Managed" Path="runtimes/android-arm64/lib/net10.0/Microsoft.Android.Runtime.NativeAOT.dll" AssemblyName="Microsoft.Android.Runtime.NativeAOT" PublicKeyToken="84e04ff9cfb79065" AssemblyVersion="1.0.0.0" FileVersion="1.0.0.0" Profile="Android" />
  <File Type="Native" Path="runtimes/android-arm64/native/libnet-android-nativeaot-debug.a" FileVersion="0.0.0.0" Profile="Android" />
  <File Type="Native" Path="runtimes/android-arm64/native/libnet-android-nativeaot-release.a" FileVersion="0.0.0.0" Profile="Android" />
  <File Type="Native" Path="runtimes/android-arm64/native/libxa-shared-bits-debug.a" FileVersion="0.0.0.0" Profile="Android" />
  <File Type="Native" Path="runtimes/android-arm64/native/libxa-shared-bits-release.a" FileVersion="0.0.0.0" Profile="Android" />
</FileList>

But then I noticed:

image

The arm and x86 packs are smaller, so they are missing the new files.

But we should probably just stop producing the arm and x86 packs completely, and then see if some of the failing tests were targeting x86 or something.

@jonathanpeppers
Copy link
Member

jonathanpeppers commented Aug 20, 2025

I tried to push a change (says I can't push to jkoritzinsky/dotnet-android), but I sent a different PR for main:

@jkoritzinsky
Copy link
Member Author

You should be able to push to my fork (I have the "allow edits by maintainers" checkbox checked), weird. Thanks for looking into this for me.

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.

5 participants