[NativeAOT] Fix application linking with API level > 21#10812
[NativeAOT] Fix application linking with API level > 21#10812
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the .NET for Android NativeAOT build/link pipeline so that non-Mono (NativeAOT/CoreCLR) builds consistently use the same Android API level that the runtime pack was built with, avoiding missing native symbols at link time (especially when targeting API > 21).
Changes:
- Add “non-mono” NDK API level placeholders and propagate them into generated MSBuild props/projitems.
- Update NativeAOT linking to use the NDK’s
clang++via full path and pass an explicit--target=${ARCH}-linux-android${API}linker argument. - Split minimum-supported API metadata for arm64/x86_64 based on runtime flavor.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props.in | Adds non-mono API level properties and runtime-conditional minimum API items (currently with issues). |
| src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets | Wires new placeholder replacements into Common.props generation. |
| src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.NativeAOT.targets | Uses NDK clang++ full path and adds --target=... to linker args. |
| build-tools/xaprepare/xaprepare/Steps/Step_GenerateFiles.cs | Adds new placeholder values for non-mono API levels when generating Ndk.projitems. |
| build-tools/scripts/Ndk.projitems.in | Introduces properties/metadata for non-mono API levels for arm64/x64. |
src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props.in
Outdated
Show resolved
Hide resolved
src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props.in
Outdated
Show resolved
Hide resolved
src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props.in
Outdated
Show resolved
Hide resolved
...in.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.NativeAOT.targets
Outdated
Show resolved
Hide resolved
jonathanpeppers
left a comment
There was a problem hiding this comment.
There are some errors building NativeAOT projects like:
gcc : error : unrecognized command-line option '--target=x86_64-linux-android21' [C:\a\_work\1\a\TestRelease\02-13_14.27.07\temp\BuildApplicationAndCleanTrueapkNativeAOT\UnnamedProject.csproj]
gcc : error : unrecognized command-line option '--target=x86_64-linux-android24' [C:\a\_work\1\a\TestRelease\02-13_14.27.07\temp\BuildApplicationAndCleanTrueapkNativeAOT\UnnamedProject.csproj]
C:\Users\cloudtest\.nuget\packages\microsoft.dotnet.ilcompiler\11.0.0-preview.1.26076.102\build\Microsoft.NETCore.Native.targets(391,5): error MSB3073: The command ""gcc" "obj\Release\android-x64\native\UnnamedProject.o" -o "bin\Release\android-x64\native\UnnamedProject.so" -Wl,--version-script="obj\Release\android-x64\native\UnnamedProject.exports" -Wl,--export-dynamic -gz=zlib -fuse-ld=lld C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libSystem.Native.a C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libSystem.Globalization.Native.a C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libSystem.IO.Compression.Native.a C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libSystem.Security.Cryptography.Native.Android.a C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libbootstrapperdll.o C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libRuntime.WorkstationGC.a C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libeventpipe-disabled.a C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libRuntime.VxsortDisabled.a C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libstandalonegc-disabled.a C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libaotminipal.a C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libbrotlienc.a C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libbrotlidec.a C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libbrotlicommon.a C:\Users\cloudtest\.nuget\packages\microsoft.netcore.app.runtime.nativeaot.android-x64\11.0.0-preview.1.26076.102/runtimes/android-x64/\native\libzstd.a --target=x86_64-linux-android21 -g -Wl,--build-id=sha1 -Wl,--as-needed -Wl,-e,0x0 -pthread -ldl -lz -llog -lm -shared -Wl,-z,relro -Wl,-z,now -Wl,--eh-frame-hdr -Wl,-z,max-page-size=16384 --target=x86_64-linux-android24 "-Wl,-soname,libUnnamedProject.so" -Wl,--error-unresolved-symbols -Wl,--no-undefined "C:\a\_work\1\s\bin\Release\dotnet\packs\Microsoft.Android.Runtime.NativeAOT.36.android-x64\36.1.99-ci.dev-grendel-naot-link-api-level.147\runtimes\android-x64\native\libnaot-android.release-static-release.a" "C:\Android\android-sdk\ndk\29.0.14206865\toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/x86_64-linux-android/libc++_static.a" "C:\Android\android-sdk\ndk\29.0.14206865\toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/x86_64-linux-android/libc++abi.a" obj\Release\android-x64\android\jni_init_funcs.x86_64.o obj\Release\android-x64\android\environment.x86_64.o -Wl,--discard-all -Wl,--gc-sections" exited with code 1. [C:\a\_work\1\a\TestRelease\02-13_14.27.07\temp\BuildApplicationAndCleanTrueapkNativeAOT\UnnamedProject.csproj]
This is odd, it works fine locally. I'll investigate. |
72b35dc to
3381021
Compare
|
So the reason for NAOT tests failing on Windows was that the |
Context: bef768d
Context: dotnet/runtime#124461
Follow up to bef768d which puts the non-mono builds API level in
several more locations throughout the source. This is necessary for
NativeAOT builds to correctly link as they must use the same API
level
.NET for Androidwas built with. Failure to do so may resultin missing native symbols when linking.
NativeAOT builds now use full path to the linker (in our case
clang++)and pass the
--target=${ARCH}-linux-android${API}argument to it, justlike the toolchain wrapper scripts in the NDK do. Without the parameter,
the lowest supported API level will be used (currently 21).