Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package net.dot.jni.nativeaot;

import android.util.Log;
import android.content.Context;
import mono.NativeLibraryHelper;

public class JavaInteropRuntime {
static {
public static void loadLibrary(Context context) {
Log.d("JavaInteropRuntime", "Loading @MAIN_ASSEMBLY_NAME@.so...");
System.loadLibrary("@MAIN_ASSEMBLY_NAME@");
NativeLibraryHelper.loadLibrary("@MAIN_ASSEMBLY_NAME@", context);
}

private JavaInteropRuntime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void attachInfo(android.content.Context context, android.content.pm.Provi
String cacheDir = context.getCacheDir().getAbsolutePath();

// Initialize .NET runtime
JavaInteropRuntime.loadLibrary(context);
JavaInteropRuntime.init(loader, language, filesDir, cacheDir);
// NOTE: only required for custom applications
ApplicationRegistration.registerApplications();
Expand Down
10 changes: 5 additions & 5 deletions src/java-runtime/java/mono/android/MonoPackageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,19 @@ public static void LoadApplication (Context context)
// below, leading to an error locating the Mono runtime
//
if (BuildConfig.Debug) {
System.loadLibrary ("xamarin-debug-app-helper");
NativeLibraryHelper.loadLibrary ("xamarin-debug-app-helper", runtimePackage, apks);
DebugRuntime.init (apks, runtimeDir, appDirs, haveSplitApks);
} else {
System.loadLibrary("monosgen-2.0");
NativeLibraryHelper.loadLibrary ("monosgen-2.0", runtimePackage, apks);
}
System.loadLibrary("xamarin-app");
NativeLibraryHelper.loadLibrary ("xamarin-app", runtimePackage, apks);

if (!BuildConfig.DotNetRuntime) {
// .net5+ APKs don't contain `libmono-native.so`
System.loadLibrary("mono-native");
NativeLibraryHelper.loadLibrary ("mono-native", runtimePackage, apks);
}

System.loadLibrary("monodroid");
NativeLibraryHelper.loadLibrary ("monodroid", runtimePackage, apks);

Runtime.initInternal (
language,
Expand Down
149 changes: 149 additions & 0 deletions src/java-runtime/java/mono/android/NativeLibraryHelper.java
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 (".");

Comment thread
jonathanpeppers marked this conversation as resolved.
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static void LoadApplication (Context context)
String[] appDirs = new String[] {filesDir, cacheDir, dataDir, codeCacheDir};
boolean haveSplitApks = runtimePackage.splitSourceDirs != null && runtimePackage.splitSourceDirs.length > 0;

System.loadLibrary("monodroid");
NativeLibraryHelper.loadLibrary ("monodroid", runtimePackage, apks);

Runtime.initInternal (
language,
Expand Down
97 changes: 97 additions & 0 deletions tests/MSBuildDeviceIntegration/Tests/NativeLibraryLoadTests.cs
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)}");
}
}
Loading