-
Notifications
You must be signed in to change notification settings - Fork 577
[java-runtime] Improve native library load diagnostics #12313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jonathanpeppers
wants to merge
3
commits into
main
Choose a base branch
from
jonathanpeppers-implement-issue-5149
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 4 additions & 2 deletions
6
src/Xamarin.Android.Build.Tasks/Resources/JavaInteropRuntime.java
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
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
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
149 changes: 149 additions & 0 deletions
149
src/java-runtime/java/mono/android/NativeLibraryHelper.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| package mono; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.zip.ZipEntry; | ||
| import java.util.zip.ZipFile; | ||
| import android.content.Context; | ||
| import android.content.pm.ApplicationInfo; | ||
| import android.os.Build; | ||
| import android.util.Log; | ||
|
|
||
| public final class NativeLibraryHelper { | ||
| static final String TAG = "monodroid"; | ||
|
|
||
| private NativeLibraryHelper () | ||
| { | ||
| } | ||
|
|
||
| public static void loadLibrary (String libraryName, Context context) | ||
| { | ||
| ApplicationInfo applicationInfo = context.getApplicationInfo (); | ||
| String[] splitApks = applicationInfo.splitSourceDirs; | ||
| String[] apks; | ||
| if (splitApks != null && splitApks.length > 0) { | ||
| apks = new String [splitApks.length + 1]; | ||
| apks [0] = applicationInfo.sourceDir; | ||
| System.arraycopy (splitApks, 0, apks, 1, splitApks.length); | ||
| } else { | ||
| apks = new String [] { applicationInfo.sourceDir }; | ||
| } | ||
|
|
||
| loadLibrary (libraryName, applicationInfo, apks); | ||
| } | ||
|
|
||
| static void loadLibrary (String libraryName, ApplicationInfo applicationInfo, String[] apks) | ||
| { | ||
| try { | ||
| System.loadLibrary (libraryName); | ||
| } catch (UnsatisfiedLinkError cause) { | ||
| String diagnosticMessage = getDiagnosticMessage (libraryName, applicationInfo, apks, cause); | ||
| Log.e (TAG, diagnosticMessage, cause); | ||
|
|
||
| UnsatisfiedLinkError error = new UnsatisfiedLinkError (diagnosticMessage); | ||
| error.initCause (cause); | ||
| throw error; | ||
| } catch (SecurityException cause) { | ||
| String diagnosticMessage = getDiagnosticMessage (libraryName, applicationInfo, apks, cause); | ||
| Log.e (TAG, diagnosticMessage, cause); | ||
| throw new SecurityException (diagnosticMessage, cause); | ||
| } | ||
| } | ||
|
|
||
| static String getDiagnosticMessage (String libraryName, ApplicationInfo applicationInfo, String[] apks, Throwable cause) | ||
| { | ||
| String mappedLibraryName = System.mapLibraryName (libraryName); | ||
| StringBuilder message = new StringBuilder (); | ||
| message.append ("Failed to load native library '").append (mappedLibraryName).append ("'."); | ||
| message.append (" Supported ABIs: ").append (Arrays.toString (Build.SUPPORTED_ABIS)).append ("."); | ||
|
|
||
| boolean foundLibrary = appendNativeLibraryDirectoryDiagnostics (message, applicationInfo.nativeLibraryDir, mappedLibraryName); | ||
| foundLibrary |= appendApkDiagnostics (message, apks, mappedLibraryName); | ||
|
|
||
| if (!foundLibrary) { | ||
| message.append (" The library was not found in the native library directory or any application APK that could be inspected."); | ||
| message.append (" The application installation may be corrupt; reinstalling the application may fix this error."); | ||
| } | ||
|
|
||
| String causeMessage = cause.getMessage (); | ||
| if (causeMessage != null && causeMessage.length () > 0) { | ||
| message.append (" Original error: ").append (causeMessage); | ||
| } | ||
|
|
||
| return message.toString (); | ||
| } | ||
|
|
||
| static boolean appendNativeLibraryDirectoryDiagnostics (StringBuilder message, String nativeLibraryDir, String mappedLibraryName) | ||
| { | ||
| message.append (" Native library directory: "); | ||
| if (nativeLibraryDir == null) { | ||
| message.append ("<unknown>."); | ||
| return false; | ||
| } | ||
|
|
||
| File directory = new File (nativeLibraryDir); | ||
| File library = new File (directory, mappedLibraryName); | ||
| boolean libraryExists = library.isFile (); | ||
| message.append ('\'').append (nativeLibraryDir).append ('\''); | ||
| message.append (" (directory exists: ").append (directory.isDirectory ()); | ||
| message.append (", library exists: ").append (libraryExists); | ||
| if (libraryExists) { | ||
| message.append (", library size: ").append (library.length ()); | ||
| message.append (", library readable: ").append (library.canRead ()); | ||
| } | ||
| message.append (")."); | ||
| return libraryExists; | ||
| } | ||
|
|
||
| static boolean appendApkDiagnostics (StringBuilder message, String[] apks, String mappedLibraryName) | ||
| { | ||
| boolean foundLibrary = false; | ||
| message.append (" APKs:"); | ||
| if (apks == null || apks.length == 0) { | ||
| message.append (" <none>."); | ||
| return false; | ||
| } | ||
|
|
||
| for (String apk : apks) { | ||
| message.append (" '").append (apk).append ("'"); | ||
| if (apk == null) { | ||
| message.append (" (invalid path);"); | ||
| continue; | ||
| } | ||
|
|
||
| File apkFile = new File (apk); | ||
| if (!apkFile.isFile ()) { | ||
| message.append (" (file exists: false);"); | ||
| continue; | ||
| } | ||
|
|
||
| ArrayList<String> entries = new ArrayList<String> (); | ||
| try (ZipFile zip = new ZipFile (apkFile)) { | ||
| for (String abi : Build.SUPPORTED_ABIS) { | ||
| String entryName = "lib/" + abi + "/" + mappedLibraryName; | ||
| ZipEntry entry = zip.getEntry (entryName); | ||
| if (entry == null) | ||
| continue; | ||
|
|
||
| foundLibrary = true; | ||
| String storage = entry.getMethod () == ZipEntry.STORED ? "stored" : "compressed"; | ||
| entries.add (abi + ", " + storage + ", size " + entry.getSize ()); | ||
| } | ||
| if (entries.size () == 0) | ||
| message.append (" (contains no matching native libraries);"); | ||
| else | ||
| message.append (" (contains: ").append (entries).append (");"); | ||
| } catch (IOException | SecurityException e) { | ||
| message.append (" (could not inspect: ").append (e.getClass ().getSimpleName ()); | ||
| String errorMessage = e.getMessage (); | ||
| if (errorMessage != null && errorMessage.length () > 0) | ||
| message.append (": ").append (errorMessage); | ||
| message.append (");"); | ||
| } | ||
| } | ||
|
|
||
| return foundLibrary; | ||
| } | ||
| } | ||
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
97 changes: 97 additions & 0 deletions
97
tests/MSBuildDeviceIntegration/Tests/NativeLibraryLoadTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
|
|
||
| using NUnit.Framework; | ||
| using Xamarin.Android.Tasks; | ||
| using Xamarin.ProjectTools; | ||
|
|
||
| namespace Xamarin.Android.Build.Tests; | ||
|
|
||
| [TestFixture] | ||
| [Category ("UsesDevice")] | ||
| public class NativeLibraryLoadTests : DeviceTest | ||
| { | ||
| [Test] | ||
| public void MissingNativeLibraryHasUsefulErrorMessage ([Values (AndroidRuntime.CoreCLR, AndroidRuntime.NativeAOT)] AndroidRuntime runtime) | ||
| { | ||
| bool isRelease = runtime == AndroidRuntime.NativeAOT; | ||
| if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) { | ||
| return; | ||
| } | ||
|
|
||
| var proj = new XamarinAndroidApplicationProject ( | ||
| packageName: PackageUtils.MakePackageName (runtime, "missingnativelibrary") | ||
| ) { | ||
| IsRelease = isRelease, | ||
| ProjectName = "MissingNativeLibrary", | ||
| }; | ||
| proj.SetRuntime (runtime); | ||
| proj.SetRuntimeIdentifiers (new [] { DeviceAbi }); | ||
| proj.SetDefaultTargetDevice (); | ||
|
|
||
| string libraryName; | ||
| string removeLibraryItems; | ||
| if (runtime == AndroidRuntime.NativeAOT) { | ||
| libraryName = $"lib{proj.ProjectName}.so"; | ||
| removeLibraryItems = $@" | ||
| <FrameworkNativeLibrary Remove=""@(FrameworkNativeLibrary)"" | ||
| Condition="" '%(FrameworkNativeLibrary.ArchiveFileName)' == '{libraryName}' or '%(FrameworkNativeLibrary.FileName)%(FrameworkNativeLibrary.Extension)' == '{libraryName}' "" /> | ||
| <_ApplicationSharedLibrary Remove=""@(_ApplicationSharedLibrary)"" | ||
| Condition="" '%(_ApplicationSharedLibrary.ArchiveFileName)' == '{libraryName}' or '%(_ApplicationSharedLibrary.FileName)%(_ApplicationSharedLibrary.Extension)' == '{libraryName}' "" />"; | ||
| } else { | ||
| libraryName = "libmonodroid.so"; | ||
| removeLibraryItems = @" | ||
| <FrameworkNativeLibrary Remove=""@(FrameworkNativeLibrary)"" | ||
| Condition="" '%(FrameworkNativeLibrary.ArchiveFileName)' == 'libmonodroid.so' or '%(FrameworkNativeLibrary.FileName)%(FrameworkNativeLibrary.Extension)' == 'libmonodroid.so' "" /> | ||
| <_ApplicationSharedLibrary Remove=""@(_ApplicationSharedLibrary)"" | ||
| Condition="" '%(_ApplicationSharedLibrary.ArchiveFileName)' == 'libmonodroid.so' or '%(_ApplicationSharedLibrary.FileName)%(_ApplicationSharedLibrary.Extension)' == 'libmonodroid.so' "" />"; | ||
| } | ||
|
|
||
| proj.Imports.Add (new Import (() => "Directory.Build.targets") { | ||
| TextContent = () => $""" | ||
| <Project> | ||
| <Target Name="_RemoveNativeLibraryForTest" BeforeTargets="_BuildApkEmbed"> | ||
| <ItemGroup> | ||
| {removeLibraryItems} | ||
| </ItemGroup> | ||
| </Target> | ||
| </Project> | ||
| """ | ||
| }); | ||
|
|
||
| using var builder = CreateApkBuilder (); | ||
| Assert.IsTrue (builder.Install (proj), "Project should have installed."); | ||
|
|
||
| string outputDirectory = Path.Combine (Root, builder.ProjectDirectory, proj.OutputPath); | ||
| string[] apks = Directory.GetFiles (outputDirectory, $"{proj.PackageName}-Signed.apk", SearchOption.AllDirectories); | ||
| Assert.IsNotEmpty (apks, "The signed APK should exist."); | ||
| using (var apk = ZipHelper.OpenZip (apks [0])) { | ||
| Assert.IsFalse (apk.ContainsEntry ($"lib/{DeviceAbi}/{libraryName}"), | ||
| $"{libraryName} should have been removed from the APK."); | ||
| } | ||
|
|
||
| var expectedMessages = new HashSet<string> { | ||
| $"Failed to load native library '{libraryName}'.", | ||
| "Supported ABIs:", | ||
| "Native library directory:", | ||
| "library exists: false", | ||
| "APKs:", | ||
| "contains no matching native libraries", | ||
| "The application installation may be corrupt; reinstalling the application may fix this error.", | ||
| }; | ||
| string logcatPath = Path.Combine (Root, builder.ProjectDirectory, "native-library-load.log"); | ||
| bool foundDiagnostic = MonitorAdbLogcat ( | ||
| line => { | ||
| expectedMessages.RemoveWhere (message => line.Contains (message, StringComparison.Ordinal)); | ||
| return expectedMessages.Count == 0; | ||
| }, | ||
| logcatPath, | ||
| timeout: 45, | ||
| onMonitoringStarted: () => AdbStartActivity ($"{proj.PackageName}/{proj.JavaPackageName}.MainActivity") | ||
| ); | ||
|
|
||
| Assert.IsTrue (foundDiagnostic, | ||
| $"The native library diagnostic was incomplete. Missing: {string.Join (", ", expectedMessages)}"); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.