Skip to content

Support for exempting native libraries from JNI preload#10787

Open
grendello wants to merge 23 commits intomainfrom
dev/grendel/jni-preload-exemption
Open

Support for exempting native libraries from JNI preload#10787
grendello wants to merge 23 commits intomainfrom
dev/grendel/jni-preload-exemption

Conversation

@grendello
Copy link
Contributor

@grendello grendello commented Feb 9, 2026

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>

<_AndroidStripNativeLibraries Condition=" '$(AndroidStripNativeLibraries)' != '' And '$(AndroidStripNativeLibraries)' == 'true' ">true</_AndroidStripNativeLibraries>
<_AndroidStripNativeLibraries Condition=" '$(_AndroidStripNativeLibraries)' != 'true' ">false</_AndroidStripNativeLibraries>

<AndroidIgnoreAllJniPreload Condition=" '$(AndroidIgnoreAllJniPreload)' == '' ">false</AndroidIgnoreAllJniPreload>
Copy link
Member

Choose a reason for hiding this comment

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

How common do you think people would have to do this? Should it be a public, documented property?

Or is it an edge-case, and $(_AndroidIgnoreAllJniPreload) would be fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know, to be honest. The original scenario it addresses appears to be quite unusual, but with the dependency hell that the AndroidX ecosystem is, who knows? I would make it a documented and supported property, just to be safe and not let people without easy choice.

@grendello grendello force-pushed the dev/grendel/jni-preload-exemption branch 4 times, most recently from d399967 to afa6525 Compare February 13, 2026 13:00
@grendello grendello force-pushed the dev/grendel/jni-preload-exemption branch 3 times, most recently from 6df6659 to 2d3b823 Compare February 18, 2026 15:52
@grendello grendello marked this pull request as ready for review February 18, 2026 15:52
Copilot AI review requested due to automatic review settings February 18, 2026 15:52
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds an opt-out mechanism for JNI native library preloading, including per-library exemptions and a global “ignore all” switch, with supporting build/test infrastructure and documentation updates.

Changes:

  • Introduces $(AndroidIgnoreAllJniPreload) and @(AndroidNativeLibraryNoJniPreload) integration into native application config generation.
  • Adds a small “test JNI library” build path to enable preload-related tests.
  • Expands test utilities and adds new tests validating JNI preload inclusion/exclusion behavior.

Reviewed changes

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

Show a summary per file
File Description
src/native/native.targets Adds targets to configure/build a test JNI library as part of runtime build.
src/native/common/test-jni-library/stub.cc Adds minimal JNI_OnLoad stub for preload tests.
src/native/common/test-jni-library/CMakeLists.txt Builds the test JNI shared library and outputs to test directory.
src/native/CMakePresets.json.in Adds XA_TEST_OUTPUT_DIR to presets cache variables.
src/native/CMakeLists.txt Wires BUILD_TEST_JNI_LIBRARY flow and requires XA_TEST_OUTPUT_DIR.
src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets Defines new property and item groups; passes new item lists into generator task.
src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs Adds helper to normalize native library names.
src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfigNativeAssemblyGeneratorCLR.cs Implements ignore/always-preload resolution for JNI preload list generation (CoreCLR path).
src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfigNativeAssemblyGenerator.cs Applies same JNI preload ignore logic (MonoVM path).
src/Xamarin.Android.Build.Tasks/Tasks/GenerateNativeApplicationConfigSources.cs Adds task parameters for preload ignore/always-preload lists.
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/AndroidItem.cs Adds test project 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 Extends parsing helpers to read JNI preload index data from generated native sources.
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest3.cs Adds tests validating JNI preload inclusion/exemption and deduplication.
build-tools/xaprepare/xaprepare/xaprepare.targets Adds placeholder replacement for TestOutputDirectory.
build-tools/xaprepare/xaprepare/Steps/Step_GenerateFiles.cs Plumbs XA_TEST_OUTPUT_DIR into generated CMake presets.
build-tools/xaprepare/xaprepare/Application/Properties.Defaults.cs.in Adds default for TestOutputDirectory.
build-tools/xaprepare/xaprepare/Application/KnownProperties.cs Adds TestOutputDirectory known property.
Documentation/docs-mobile/building-apps/build-properties.md Documents AndroidIgnoreAllJniPreload.
Documentation/docs-mobile/building-apps/build-items.md Documents AndroidNativeLibraryNoJniPreload.

@grendello grendello force-pushed the dev/grendel/jni-preload-exemption branch from 2d3b823 to b597b5f Compare February 19, 2026 08:42
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.

.NET 10 breaks library loading semantics

2 participants

Comments