Skip to content

.NET 10 breaks library loading semantics #10617

@bengt-GS

Description

@bengt-GS

Android framework version

net10.0-android (Preview)

Affected platform version

.NET 10 RC 2

Description

Hello! We have identified an issue with how .NET loads native libraries, which we believe was introduced since .NET 10. From the release notes we believe "Use Android shared library loader for JNI libraries" (#10376) is the culprit.

Our understanding is that this commit will preload all libraries that define a JNI_OnLoad.

However, this changes the semantics of the code drastically. The example below illustrates this by loading a different library based on an external condition. Note that there are other cases as well but generally this change means that the libraries are always loaded, regardless of whether at that point they should be loaded, or whether the Java code loads them at all.

The change introduced by the aforementioned commit will break this mechanism. I have attached samples that is a stripped down version of a case where we ran into issues due to this change:

  • An android SDK that provides a 'returnTrue()' method (implemented in native). If the correct native library is loaded, this method will return true.
  • A sample android app, written in Java that shows returnTrue() does indeed return true.
  • Bindings for the SDK + a Maui app that uses said bindings. A button can be pressed in the sample which will show that the wrong variant is loaded (as returnTrue() returns false).

Steps to Reproduce

Here are the aforementioned samples:

  1. ExampleNativeLibrary.zip
  2. JavaApp.zip
  3. ExampleLibraryBinding.zip
  4. MauiMainApp.zip
  • Running 'JavaApp' will result in "True? true" being printed in the logcat.
  • Running the 'MauiMainApp' sample will result in "True? false" being printed in the logcat.

This illustrates one of the ways preloading native libraries can result in unexpected behavior.

Did you find any workaround?

No response

Relevant log output

Metadata

Metadata

Assignees

Labels

Area: App RuntimeIssues in `libmonodroid.so`.needs-triageIssues that need to be assigned.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions