Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[NativeAOT] improve build logic, part 2 #9631

Merged
merged 1 commit into from
Dec 19, 2024

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Dec 18, 2024

This is another set of changes to Android apps to run under NativeAOT:

  • Default to $(TrimMode)=Full for NativeAOT, this enables trimmer warnings and should be the default mode for NativeAOT.

  • Ensure _PrepareLinking MSBuild target runs at the appropriate time. This was causing Android.App.Activity's GetOnCreate_Landroid_os_Bundle_Handler() to get trimmed away.

  • Default to $(LinkerFlavor)=lld to avoid a java.lang.UnsatisfiedLinkError at runtime.

  • Include libc++_shared.so to avoid a java.lang.UnsatisfiedLinkError at runtime.

  • Emit JavaPeerStyle.JavaInterop1 java stubs for NativeAOT, as it is easier to get working in place of JavaPeerStyle.XAJavaInterop1.

I updated our existing NativeAOT() test to assert for these changes where possible.

This is another set of changes to Android apps to *run* under NativeAOT:

* Default to `$(TrimMode)=Full` for NativeAOT, this enables trimmer
  warnings and should be the default mode for NativeAOT.

* Ensure `_PrepareLinking` MSBuild target runs at the appropriate
  time. This was causing `Android.App.Activity`'s
  `GetOnCreate_Landroid_os_Bundle_Handler()` to get trimmed away.

* Default to `$(LinkerFlavor)=lld` to avoid a
  `java.lang.UnsatisfiedLinkError` at runtime.

* Include `libc++_shared.so` to avoid a
  `java.lang.UnsatisfiedLinkError` at runtime.

* Emit `JavaPeerStyle.JavaInterop1` java stubs for NativeAOT, as it is
  easier to get working in place of `JavaPeerStyle.XAJavaInterop1`.

I updated our existing `NativeAOT()` to assert for these changes where
possible.
@jonathanpeppers
Copy link
Member Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jonathanpeppers
Copy link
Member Author

There is one failure, that is probably network-related:

Mono.Android.NET_Tests, Xamarin.Android.NetTests.AndroidMessageHandlerTests.ServerCertificateCustomValidationCallback_Redirects / Release
Expected: 2
But was:  1

I think we can ignore, as these changes shouldn't affect it.

@jonpryor
Copy link
Member

Draft commit message:

Context: 8d7713071c40766651664de25b53eb15225785d0
Context: https://github.com/dotnet/android/pull/9630

We're exploring how to get .NET for Android apps to build and run
using [Native AOT][0].

Default to `$(TrimMode)=Full` for NativeAOT, this enables trimmer
warnings and should be the default mode for NativeAOT.

Ensure the `_PrepareLinking` MSBuild target runs at the appropriate
time.  Failure to do so was causing
`Android.App.Activity.GetOnCreate_Landroid_os_Bundle_Handler()` to be
trimmed away.

Various fixes to avoid `java.lang.UnsatisfiedLinkError` errors:

  * Set `$(LinkerFlavor)=lld` by default to avoid:

        E AndroidRuntime: java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "__start___modules" referenced by
          "/data/app/~~_ggpMC4foLk_jUUycm0CfA==/net.dot.hellonativeaot-fvszIWroqgweLHYgULxVoQ==/split_config.arm64_v8a.apk!/lib/arm64-v8a/libNativeAOT.so"...

  * Include `libc++_shared.so` within the app, as Native AOT output
    requires C++:

        E AndroidRuntime: java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found: needed by
          /data/app/~~_W0B9EE3hhajnFvCHyUKSg==/net.dot.hellonativeaot-zlXemqHdkbHaLu60oYPVQQ==/lib/arm64/libNativeAOT.so in namespace clns-6

Emit `JavaPeerStyle.JavaInterop1` java stubs for NativeAOT, as it is
easier to get working in place of `JavaPeerStyle.XAJavaInterop1`.
Specifically, we need to be able to avoid P/Invokes related to
typemaps/etc.  XAJavaInterop1 hits `JNIEnvInit.RegisterJniNatives()`,
which is full of P/Invokes such as `TypeManager.GetClassName()`,
while `JavaInterop1` hits `ManagedPeer.RegisterNativeMembers()` which
goes through `JniRuntime` abstractions, allowing for a P/Invoke-free
code path.

I updated `BuildTest2.NativeAOT()` to assert for these changes where
possible.

[0]: https://learn.microsoft.com/dotnet/core/deploying/native-aot/

@jonpryor jonpryor merged commit 5f4d223 into main Dec 19, 2024
58 checks passed
@jonpryor jonpryor deleted the dev/peppers/nativeaot/build/part2 branch December 19, 2024 15:27
grendello added a commit that referenced this pull request Jan 7, 2025
* main: (25 commits)
  [CI] Break "Linux Tests" into 2 parallel jobs. (#9642)
  Fix `WorkloadDependencies.proj` build. (#9648)
  [CI] Set "WearOS Tests" parallelization to 2 agents. (#9639)
  [CI] Break "Package Tests" into 2 parallel jobs. (#9638)
  Bump to DevDiv/android-platform-support@3b4e16f1 (#9632)
  [NativeAOT] improve build logic, part 2 (#9631)
  Bump to dotnet/java-interop@2c06b3c2 (#9633)
  [NativeAOT] improve build logic, part 1 (#9614)
  [build] Generate `WorkloadDependencies.json` (#9613)
  [monodroid] remove `monodroid_get_log_categories()` (#9625)
  [monodroid] remove `_monodroid_get_identity_hash_code` (#9622)
  Bump to dotnet/java-interop@f800ea52 (#9607)
  [XABT] Break BuildApk into individual tasks for each content type. (#9612)
  [Mono.Android] Bind Android API-Baklava DP1 (#9594)
  [Xamarin.Android.Build.Tasks] Extract `BuildArchive` from `BuildApk` (#9556)
  [NativeAOT] MSBuild-related logic to get projects to build (#9583)
  [build] remove remnants of `OpenTK-1.0.dll` (#9610)
  [build] remove `Xamarin.Android.CSharp.targets` (#9609)
  [build] runtime "flavors" part 2 (#9598)
  Bump com.android.tools.build:manifest-merger to 31.7.3 (#9600)
  ...
grendello added a commit that referenced this pull request Jan 7, 2025
* dev/grendel/use-libc++: (25 commits)
  [CI] Break "Linux Tests" into 2 parallel jobs. (#9642)
  Fix `WorkloadDependencies.proj` build. (#9648)
  [CI] Set "WearOS Tests" parallelization to 2 agents. (#9639)
  [CI] Break "Package Tests" into 2 parallel jobs. (#9638)
  Bump to DevDiv/android-platform-support@3b4e16f1 (#9632)
  [NativeAOT] improve build logic, part 2 (#9631)
  Bump to dotnet/java-interop@2c06b3c2 (#9633)
  [NativeAOT] improve build logic, part 1 (#9614)
  [build] Generate `WorkloadDependencies.json` (#9613)
  [monodroid] remove `monodroid_get_log_categories()` (#9625)
  [monodroid] remove `_monodroid_get_identity_hash_code` (#9622)
  Bump to dotnet/java-interop@f800ea52 (#9607)
  [XABT] Break BuildApk into individual tasks for each content type. (#9612)
  [Mono.Android] Bind Android API-Baklava DP1 (#9594)
  [Xamarin.Android.Build.Tasks] Extract `BuildArchive` from `BuildApk` (#9556)
  [NativeAOT] MSBuild-related logic to get projects to build (#9583)
  [build] remove remnants of `OpenTK-1.0.dll` (#9610)
  [build] remove `Xamarin.Android.CSharp.targets` (#9609)
  [build] runtime "flavors" part 2 (#9598)
  Bump com.android.tools.build:manifest-merger to 31.7.3 (#9600)
  ...
@github-actions github-actions bot locked and limited conversation to collaborators Jan 19, 2025
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants