Skip to content

[java-runtime] Improve native library load diagnostics - #12313

Open
jonathanpeppers wants to merge 3 commits into
mainfrom
jonathanpeppers-implement-issue-5149
Open

[java-runtime] Improve native library load diagnostics#12313
jonathanpeppers wants to merge 3 commits into
mainfrom
jonathanpeppers-implement-issue-5149

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Member

Pull Request
title and
description
should follow the
commit-messages.md workflow documentation, and in particular should include:

  • Useful description of why the change is necessary.
  • Links to issues fixed
  • Unit tests

Description

Native library startup failures currently provide too little information to diagnose corrupt, incomplete, or incorrectly split installations.

Route MonoVM, CoreCLR, and NativeAOT startup library loads through a shared helper. When loading fails, the resulting exception and logcat entry identify the requested library, supported ABIs, extracted native-library state, APK and split contents, and the original linker error. If the library is absent from all inspected locations, the message explains that reinstalling may repair a corrupt installation.

Add an MSBuildDeviceIntegration test which removes the real startup library from CoreCLR and NativeAOT APKs, verifies that it is absent from the package, and asserts the improved diagnostic in logcat.

Testing

  • src/java-runtime/java-runtime.csproj builds successfully for all runtime JAR variants.
  • The generated NativeAOT Java bootstrap compiles against java_runtime_trimmable.jar.
  • The new device test was not run locally because the local .NET for Android SDK was unavailable and the full repository build was skipped.

Fixes #5149

Native library startup failures currently identify only that loading failed,
which leaves crash reports without enough information to diagnose corrupt or
incomplete installations.

Route MonoVM, CoreCLR, and NativeAOT startup loads through a shared helper.
When loading fails, report the requested library, supported ABIs, extracted
file state, APK and split contents, and the original linker error.  Suggest
reinstallation when the library is absent from every inspected location.

Add device coverage which deliberately removes the startup library from
CoreCLR and NativeAOT packages and verifies the resulting diagnostic.

Fixes #5149

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 8dffd648-59fe-4171-b126-1597a61cd438
Copilot AI review requested due to automatic review settings August 7, 2026 17:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR improves startup diagnostics when the Android runtime fails to load required native libraries (MonoVM/CoreCLR/NativeAOT), making UnsatisfiedLinkError failures actionable by emitting a detailed logcat message (library name, ABI info, nativeLibraryDir state, APK/split contents, and original linker error). It also adds a device integration test that validates the improved diagnostic output by intentionally removing the startup library from built APKs.

Changes:

  • Introduces a shared Java helper (NativeLibraryHelper) to wrap System.loadLibrary() and generate rich diagnostics on failure.
  • Routes MonoVM/CoreCLR runtime library loads (and NativeAOT bootstrap load) through the shared helper.
  • Adds an MSBuildDeviceIntegration test that removes the expected startup library from APKs and asserts the diagnostic appears in logcat.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/MSBuildDeviceIntegration/Tests/NativeLibraryLoadTests.cs New device test validating the improved native-library load diagnostic in logcat.
src/Xamarin.Android.Build.Tasks/Resources/NativeAotRuntimeProvider.java Ensures NativeAOT startup explicitly loads the native library via the shared helper path.
src/Xamarin.Android.Build.Tasks/Resources/JavaInteropRuntime.java Replaces static library loading with an explicit loadLibrary(Context) that uses NativeLibraryHelper.
src/java-runtime/java/mono/android/NativeLibraryHelper.java New shared helper that wraps native library loading and builds detailed failure diagnostics.
src/java-runtime/java/mono/android/MonoPackageManager.java Routes MonoVM startup library loads through NativeLibraryHelper for improved error reporting.
src/java-runtime/java/mono/android/clr/MonoPackageManager.java Routes CoreCLR startup library load through NativeLibraryHelper for improved error reporting.
Suppressed comments (1)

src/java-runtime/java/mono/android/NativeLibraryHelper.java:124

  • ❌ error: appendApkDiagnostics also iterates Build.SUPPORTED_ABIS, which can throw on API < 21 and prevent the diagnostic from being generated. Use the same ABI fallback logic here as well so the error reporting works on older Android versions.
				for (String abi : Build.SUPPORTED_ABIS) {

Comment thread src/java-runtime/java/mono/android/NativeLibraryHelper.java
Comment thread tests/MSBuildDeviceIntegration/Tests/NativeLibraryLoadTests.cs Outdated
jonathanpeppers and others added 2 commits August 7, 2026 13:32
Follow the convention for newly added C# files.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 8dffd648-59fe-4171-b126-1597a61cd438
The NativeAOT application shared library already has the lib prefix in its
FrameworkNativeLibrary identity.  Match that filename so the failure test
actually removes the library from the APK.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 8dffd648-59fe-4171-b126-1597a61cd438
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.

Provide better error reporting around UnsatisfiedLinkError

2 participants