Repository navigation
[java-runtime] Improve native library load diagnostics - #12313
Conversation
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 <[email protected]> Copilot-Session: 8dffd648-59fe-4171-b126-1597a61cd438
There was a problem hiding this comment.
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 wrapSystem.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:
appendApkDiagnosticsalso iteratesBuild.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) {
Follow the convention for newly added C# files. Co-authored-by: Copilot App <[email protected]> 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 <[email protected]> Copilot-Session: 8dffd648-59fe-4171-b126-1597a61cd438
Co-authored-by: Copilot App <[email protected]> Copilot-Session: 8dffd648-59fe-4171-b126-1597a61cd438
|
/review |
|
✅ Android PR Reviewer completed successfully! |
There was a problem hiding this comment.
⚠️ Needs Changes
Findings: 0 errors · 1 warning · 0 suggestions
The shared helper provides useful ABI, extracted-directory, APK/split, and original-linker diagnostics across MonoVM, CoreCLR, and NativeAOT. The new device coverage exercises both affected non-Mono runtimes, but it should clear buffered logcat first so stale output cannot satisfy the regression assertion without launching the current build.
CI: ✅ All 44 checks passed, including the dotnet-android Azure DevOps build and emulator lanes.
Generated by Android PR Reviewer for #12313 · gpt56 · 130.2 AIC · ⌖ 10.1 AIC · ⊞ 25.3K
Comment /review to run again
Prevent buffered diagnostics from satisfying the assertion before the newly installed application is launched. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 8dffd648-59fe-4171-b126-1597a61cd438
|
@dalexsoto review |
dalexsoto
left a comment
There was a problem hiding this comment.
Reviewed the current changes. The shared native-library loader preserves the original linker failure while adding useful ABI, extraction, APK, and split diagnostics across MonoVM, CoreCLR, and NativeAOT; cleanup and device coverage look correct. No blocking issues found.
Pull Request
title and
description
should follow the
commit-messages.mdworkflow documentation, and in particular should include: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.csprojbuilds successfully for all runtime JAR variants.java_runtime_trimmable.jar.Fixes #5149