[release/10.0.1xx] Support for exempting native libraries from JNI preload#10879
Merged
jonathanpeppers merged 6 commits intorelease/10.0.1xxfrom Mar 5, 2026
Merged
Conversation
Fixes: #10617 Context: cba39dc cba39dc introduced support for preloading of JNI native libraries at application startup. However, it appears that in some scenarios this behavior isn't desired. This PR introduces a mechanism which allows exempting some or all (with exception of the BCL libraries) libraries from the preload mechanism. In order to not preload any JNI libraries it's now possible to set the `$(AndroidIgnoreAllJniPreload)` MSBuild property to `true`. It is also possible to exempt individual libraries from preload by adding their name to the `AndroidNativeLibraryNoJniPreload` MSBuild item group, for instance: <ItemGroup> <AndroidNativeLibraryNoJniPreload Include="libMyLibrary.so" /> </ItemGroup>
grendello
approved these changes
Mar 3, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an opt-out mechanism for the JNI native-library preload behavior introduced for .NET Android startup, allowing projects to exempt specific native libraries (or all of them) from being preloaded while still preserving required framework defaults.
Changes:
- Introduces
$(AndroidIgnoreAllJniPreload)and@(AndroidNativeLibraryNoJniPreload)to control JNI preload behavior from MSBuild. - Extends native application config generation to honor “always preload” and “never preload” library lists.
- Adds a small native “test JNI library” plus new build/test infrastructure and NUnit tests validating preload behavior.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| src/native/native.targets | Builds a new test JNI library as part of native runtime build orchestration. |
| src/native/common/test-jni-library/stub.cc | Adds a minimal JNI_OnLoad implementation for preload testing. |
| src/native/common/test-jni-library/CMakeLists.txt | CMake rules to build and place the test JNI library in test output. |
| src/native/CMakePresets.json.in | Adds XA_TEST_OUTPUT_DIR to CMake cache variables. |
| src/native/CMakeLists.txt | Requires XA_TEST_OUTPUT_DIR and wires the test JNI library subdirectory. |
| src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets | Adds MSBuild property + item groups and passes new inputs to config generation. |
| src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs | Adds helpers for minimum API lookup and normalizing native library names. |
| src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfigNativeAssemblyGeneratorCLR.cs | Implements “ignore/always preload” decision logic for CoreCLR config generation. |
| src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfigNativeAssemblyGenerator.cs | Implements the same “ignore/always preload” decision logic for MonoVM config generation. |
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/AndroidItem.cs | Adds a ProjectTools item wrapper for AndroidNativeLibraryNoJniPreload. |
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/AndroidBuildActions.cs | Adds build action constant for AndroidNativeLibraryNoJniPreload. |
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/EnvironmentHelper.cs | Adds parsing/validation helpers for JNI preload index data from generated native sources. |
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest3.cs | New NUnit coverage for JNI preload inclusion/exclusion scenarios. |
| src/Xamarin.Android.Build.Tasks/Tasks/GenerateNativeApplicationConfigSources.cs | Wires MSBuild inputs into the native config generator. |
| build-tools/xaprepare/xaprepare/xaprepare.targets | Adds placeholder replacement for @TestOutputDirectory@. |
| build-tools/xaprepare/xaprepare/Steps/Step_GenerateFiles.cs | Plumbs XA_TEST_OUTPUT_DIR placeholder into generated CMake presets. |
| build-tools/xaprepare/xaprepare/Application/Properties.Defaults.cs.in | Adds KnownProperties.TestOutputDirectory default wiring. |
| build-tools/xaprepare/xaprepare/Application/KnownProperties.cs | Adds TestOutputDirectory to known xaprepare properties. |
| Documentation/docs-mobile/building-apps/build-properties.md | Documents AndroidIgnoreAllJniPreload. |
| Documentation/docs-mobile/building-apps/build-items.md | Documents AndroidNativeLibraryNoJniPreload. |
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest3.cs
Show resolved
Hide resolved
src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfigNativeAssemblyGeneratorCLR.cs
Outdated
Show resolved
Hide resolved
src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfigNativeAssemblyGenerator.cs
Outdated
Show resolved
Hide resolved
src/Xamarin.Android.Build.Tasks/Tasks/GenerateNativeApplicationConfigSources.cs
Outdated
Show resolved
Hide resolved
src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfigNativeAssemblyGeneratorCLR.cs
Outdated
Show resolved
Hide resolved
...Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/EnvironmentHelper.cs
Show resolved
Hide resolved
Co-authored-by: Copilot <[email protected]>
…elNonMono don't exist on .NET 10
Copilot AI
added a commit
that referenced
this pull request
Mar 3, 2026
- Fix grammar in build-items.md: "on individual basis" → "on an individual basis" - Fix grammar in build-properties.md: "allows to effectively disable this behavior" → "allows you to effectively disable it" - Fix typo NativeLibrarysAlwaysJniPreload → NativeLibrariesAlwaysJniPreload Co-authored-by: jonathanpeppers <[email protected]>
Member
Author
|
This has test failures like: |
jonathanpeppers
added a commit
that referenced
this pull request
Mar 4, 2026
Context: #10879 (review) * Fix typos and documentation wording from PR #10879 review * Fix grammar in build-items.md: "on individual basis" → "on an individual basis" * Fix grammar in build-properties.md: "allows to effectively disable this behavior" → "allows you to effectively disable it" * Fix typo NativeLibrarysAlwaysJniPreload → NativeLibrariesAlwaysJniPreload Co-authored-by: Jonathan Peppers <[email protected]>
Context: #10879 (review) * Fix typos and documentation wording from PR #10879 review * Fix grammar in build-items.md: "on individual basis" → "on an individual basis" * Fix grammar in build-properties.md: "allows to effectively disable this behavior" → "allows you to effectively disable it" * Fix typo NativeLibrarysAlwaysJniPreload → NativeLibrariesAlwaysJniPreload Co-authored-by: Jonathan Peppers <[email protected]>
EnvironmentHelper.ReadApplicationConfig only supports the MonoVM application_config struct layout (27 fields). The CoreCLR struct (ApplicationConfigCLR) has a different layout (20 fields, fewer bools), causing field type mismatches when tests run with CoreCLR on this branch. Co-authored-by: Copilot <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of: #10787
Fixes: #10617
Context: cba39dc
cba39dc introduced support for preloading of JNI native libraries at application startup. However, it appears that in some scenarios this behavior isn't desired.
This PR introduces a mechanism which allows exempting some or all (with exception of the BCL libraries) libraries from the preload mechanism.
In order to not preload any JNI libraries it's now possible to set the
$(AndroidIgnoreAllJniPreload)MSBuild property totrue.It is also possible to exempt individual libraries from preload by adding their name to the
AndroidNativeLibraryNoJniPreloadMSBuild item group, for instance: