android: VS 16.2.0 regression: failed DllImport on the libdl system library.
Steps to Reproduce
- Update to the initial VS 16.2.0 release (which includes Xamarin.Android 9.4.0.51).
- Create a blank Xamarin.Android application via the appropriate template.
- Atop MainActivity.cs, add usings for
SystemandSystem.Runtime.InteropServices. - To the MainActivity class, add:
[DllImport("libdl", EntryPoint = "dlopen")]
private static extern IntPtr DLLOpen(string fileName, int flags);
-
To MainActivity.OnCreate(Bundle), add a dummy invocation of the above (
DLLOpen("foo", 0)). -
Build and run.
Expected Behavior
Proper lookup / load of the library, appropriate output pane notification (e.g., the logging reports a series of load errors in search path locations followed by a DllImport loaded library '/system/lib/libdl.so' success indication.
Actual Behavior
The application throws System.DllNotFoundException for libdl and the output logging shows suspicious E/Linker errors:
07-24 18:03:43.058 D/Mono (20858): DllImport attempting to load: 'libdl'.
07-24 18:03:43.067 D/Mono (20858): DllImport error loading library '/storage/emulated/0/Android/data/com.companyname.dllimporttest/files/.__override__/libdl': '(null)'.
07-24 18:03:43.073 D/Mono (20858): DllImport error loading library '/storage/emulated/0/Android/data/com.companyname.dllimporttest/files/.__override__/libdl.so': '(null)'.
07-24 18:03:43.080 D/Mono (20858): DllImport error loading library '/storage/emulated/0/Android/data/com.companyname.dllimporttest/files/.__override__/libdl.so': '(null)'.
07-24 18:03:43.082 D/Mono (20858): DllImport error loading library '/system/lib/libdl': '(null)'.
07-24 18:03:43.086 E/linker (20858): library "/system/lib/libdl.so" ("/system/lib/libdl.so") needed or dlopened by "/data/app/Mono.Android.DebugRuntime-1/lib/arm/libmonosgen-32bit-2.0.so" is not accessible for the namespace: [name="classloader-namespace", ld_library_paths="", default_library_paths="/data/app/com.companyname.dllimporttest-1/lib/arm:/data/app/com.companyname.dllimporttest-1/base.apk!/lib/armeabi-v7a", permitted_paths="/data:/mnt/expand:/data/data/com.companyname.dllimporttest"]
07-24 18:03:43.090 E/linker (20858): library "/system/lib/libdl.so" ("/system/lib/libdl.so") needed or dlopened by "/data/app/com.companyname.dllimporttest-1/lib/arm/libmonodroid.so" is not accessible for the namespace: [name="classloader-namespace", ld_library_paths="", default_library_paths="/data/app/com.companyname.dllimporttest-1/lib/arm:/data/app/com.companyname.dllimporttest-1/base.apk!/lib/armeabi-v7a", permitted_paths="/data:/mnt/expand:/data/data/com.companyname.dllimporttest"]
07-24 18:03:43.092 E/linker (20858): library "/system/lib/libdl.so" ("/system/lib/libdl.so") needed or dlopened by "/data/app/com.companyname.dllimporttest-1/lib/arm/libmonodroid.so" is not accessible for the namespace: [name="classloader-namespace", ld_library_paths="", default_library_paths="/data/app/com.companyname.dllimporttest-1/lib/arm:/data/app/com.companyname.dllimporttest-1/base.apk!/lib/armeabi-v7a", permitted_paths="/data:/mnt/expand:/data/data/com.companyname.dllimporttest"]
07-24 18:03:43.095 E/linker (20858): library "/system/lib/libdl.so" ("/system/lib/libdl.so") needed or dlopened by "/data/app/com.companyname.dllimporttest-1/lib/arm/libmonodroid.so" is not accessible for the namespace: [name="classloader-namespace", ld_library_paths="", default_library_paths="/data/app/com.companyname.dllimporttest-1/lib/arm:/data/app/com.companyname.dllimporttest-1/base.apk!/lib/armeabi-v7a", permitted_paths="/data:/mnt/expand:/data/data/com.companyname.dllimporttest"]
07-24 18:03:43.098 E/linker (20858): library "/system/lib/libdl.so" ("/system/lib/libdl.so") needed or dlopened by "/data/app/com.companyname.dllimporttest-1/lib/arm/libmonodroid.so" is not accessible for the namespace: [name="classloader-namespace", ld_library_paths="", default_library_paths="/data/app/com.companyname.dllimporttest-1/lib/arm:/data/app/com.companyname.dllimporttest-1/base.apk!/lib/armeabi-v7a", permitted_paths="/data:/mnt/expand:/data/data/com.companyname.dllimporttest"]
07-24 18:03:43.100 D/Mono (20858): DllImport error loading library '/system/lib/libdl.so': 'dlopen failed: library "/system/lib/libdl.so" needed or dlopened by "/data/app/com.companyname.dllimporttest-1/lib/arm/libmonodroid.so" is not accessible for the namespace "classloader-namespace"'.
07-24 18:03:43.102 E/linker (20858): library "/system/lib/libdl.so" ("/system/lib/libdl.so") needed or dlopened by "/data/app/Mono.Android.DebugRuntime-1/lib/arm/libmonosgen-32bit-2.0.so" is not accessible for the namespace: [name="classloader-namespace", ld_library_paths="", default_library_paths="/data/app/com.companyname.dllimporttest-1/lib/arm:/data/app/com.companyname.dllimporttest-1/base.apk!/lib/armeabi-v7a", permitted_paths="/data:/mnt/expand:/data/data/com.companyname.dllimporttest"]
07-24 18:03:43.105 E/linker (20858): library "/system/lib/libdl.so" ("/system/lib/libdl.so") needed or dlopened by "/data/app/com.companyname.dllimporttest-1/lib/arm/libmonodroid.so" is not accessible for the namespace: [name="classloader-namespace", ld_library_paths="", default_library_paths="/data/app/com.companyname.dllimporttest-1/lib/arm:/data/app/com.companyname.dllimporttest-1/base.apk!/lib/armeabi-v7a", permitted_paths="/data:/mnt/expand:/data/data/com.companyname.dllimporttest"]
07-24 18:03:43.107 E/linker (20858): library "/system/lib/libdl.so" ("/system/lib/libdl.so") needed or dlopened by "/data/app/com.companyname.dllimporttest-1/lib/arm/libmonodroid.so" is not accessible for the namespace: [name="classloader-namespace", ld_library_paths="", default_library_paths="/data/app/com.companyname.dllimporttest-1/lib/arm:/data/app/com.companyname.dllimporttest-1/base.apk!/lib/armeabi-v7a", permitted_paths="/data:/mnt/expand:/data/data/com.companyname.dllimporttest"]
07-24 18:03:43.109 E/linker (20858): library "/system/lib/libdl.so" ("/system/lib/libdl.so") needed or dlopened by "/data/app/com.companyname.dllimporttest-1/lib/arm/libmonodroid.so" is not accessible for the namespace: [name="classloader-namespace", ld_library_paths="", default_library_paths="/data/app/com.companyname.dllimporttest-1/lib/arm:/data/app/com.companyname.dllimporttest-1/base.apk!/lib/armeabi-v7a", permitted_paths="/data:/mnt/expand:/data/data/com.companyname.dllimporttest"]
07-24 18:03:43.111 E/linker (20858): library "/system/lib/libdl.so" ("/system/lib/libdl.so") needed or dlopened by "/data/app/com.companyname.dllimporttest-1/lib/arm/libmonodroid.so" is not accessible for the namespace: [name="classloader-namespace", ld_library_paths="", default_library_paths="/data/app/com.companyname.dllimporttest-1/lib/arm:/data/app/com.companyname.dllimporttest-1/base.apk!/lib/armeabi-v7a", permitted_paths="/data:/mnt/expand:/data/data/com.companyname.dllimporttest"]
07-24 18:03:43.111 D/Mono (20858): DllImport error loading library '/system/lib/libdl.so': 'dlopen failed: library "/system/lib/libdl.so" needed or dlopened by "/data/app/com.companyname.dllimporttest-1/lib/arm/libmonodroid.so" is not accessible for the namespace "classloader-namespace"'.
07-24 18:03:43.112 D/Mono (20858): DllImport error loading library 'libdl': '(null)'.
07-24 18:03:43.113 D/Mono (20858): DllImport error loading library 'libdl.so': '(null)'.
07-24 18:03:43.114 D/Mono (20858): DllImport error loading library 'libdl.so': '(null)'.
07-24 18:03:43.115 D/Mono (20858): DllImport error loading library 'libdl': '(null)'.
07-24 18:03:43.115 W/Mono (20858): DllImport unable to load library '(null)'.
Loaded assembly: System.Runtime.Serialization.dll [External]
**System.DllNotFoundException:** 'libdl'
Version Information
Xamarin.Android SDK 9.4.0.51 (d16-2/9fa7775) Xamarin.Android Reference Assemblies and MSBuild support. Mono: mono/mono/2019-02@e6f5369c2d2 Java.Interop: xamarin/java.interop/d16-2@d64ada5 LibZipSharp: grendello/LibZipSharp/d16-2@caa0c74 LibZip: nih-at/libzip/rel-1-5-1@b95cf3f ProGuard: xamarin/proguard/master@905836d SQLite: xamarin/sqlite/3.27.1@8212a2d Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-2@6f6c969
VS bug #972238
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 4
- Comments: 25 (8 by maintainers)
Commits related to this issue
- [android] mono_dl_open_file: use g_file_test only on absolute paths The intention of calling `g_file_test (file, G_FILE_TEST_EXISTS)` where file is the name of a shared library we want to open is to ... — committed to lambdageek/mono by lambdageek 7 years ago
- [android] mono_dl_open_file: use g_file_test only on absolute paths The intention of calling `g_file_test (file, G_FILE_TEST_EXISTS)` where file is the name of a shared library we want to open is to ... — committed to monojenkins/mono by lambdageek 7 years ago
- [android] mono_dl_open_file: use g_file_test only on absolute paths The intention of calling `g_file_test (file, G_FILE_TEST_EXISTS)` where file is the name of a shared library we want to open is to ... — committed to monojenkins/mono by lambdageek 7 years ago
- [android] mono_dl_open_file: use g_file_test only on absolute paths (#16387) The intention of calling `g_file_test (file, G_FILE_TEST_EXISTS)` where file is the name of a shared library we want to o... — committed to mono/mono by lambdageek 7 years ago
- [android] mono_dl_open_file: use g_file_test only on absolute paths (#16392) The intention of calling `g_file_test (file, G_FILE_TEST_EXISTS)` where file is the name of a shared library we want to o... — committed to mono/mono by monojenkins 7 years ago
- [android] mono_dl_open_file: use g_file_test only on absolute paths (#16393) The intention of calling `g_file_test (file, G_FILE_TEST_EXISTS)` where file is the name of a shared library we want to o... — committed to mono/mono by monojenkins 7 years ago
- Bump to mono/mono/2019-06@6434153d Changes: https://github.com/mono/mono/compare/cc0632e4f3252554b274ab49a6d67b1d14fa4e18...6434153d8b7d20b96e510e43031c5968d4042dda Fixes: https://github.com/xamarin... — committed to dotnet/android by jonpryor 7 years ago
- Bump to mono/mono/2019-06@6434153d Changes: https://github.com/mono/mono/compare/cc0632e4f3252554b274ab49a6d67b1d14fa4e18...6434153d8b7d20b96e510e43031c5968d4042dda Fixes: https://github.com/xamarin... — committed to brendanzagaeski/xamarin-android by brendanzagaeski 7 years ago
- Bump to mono/mono/2019-06@6434153d (#3540) Changes: https://github.com/mono/mono/compare/cc0632e4f3252554b274ab49a6d67b1d14fa4e18...6434153d8b7d20b96e510e43031c5968d4042dda Fixes: https://github.c... — committed to dotnet/android by brendanzagaeski 7 years ago
- [android] mono_dl_open_file: use g_file_test only on absolute paths (mono/mono#16387) The intention of calling `g_file_test (file, G_FILE_TEST_EXISTS)` where file is the name of a shared library we ... — committed to ManickaP/runtime by lambdageek 7 years ago
Any update on this one?
FYI, after an initial test here:
DllImportdeclaration, call are in the app itself (per the simple test case in this issue’s description).DllImportand call are in a third party library employed by the application.Investigation update from the team about a temporary workaround:
A temporary workaround has been identified for the scenario of using
dlopen()from libdl.Add an application configuration file to the Android app project. (Add a new XML file to the Android app project that matches the assembly with a .dll.config extension. For example, if the assembly name is AndroidApp1, name the file AndroidApp1.dll.config.)
Leave the Build Action of the file as None, and set the Copy to Output Directory property to Copy if newer.
Set the file contents to:
(If you already have an application configuration file, you can just add the
<dllmap>element to it.)Clean and rebuild the project.
Other investigation notes:
The commit that introduced the change in behavior has been identified as https://github.com/mono/mono/commit/1be343e3a9b9adcfe12dd37abf04abf03e124963. The team is moving ahead to correct the change in behavior.
@ravinderjangra, when you get a chance, if you could submit a new issue to describe what you’re seeing so the team can take a look, that would be perfect. Thanks for your patience!
Thanks! Adding the config file to the the project we controlled worked,
One thing to watch out for: The example config file here was for “libdl”, but our DllImport attribute was originally for “libdl.so”. I had to change our DllImport to just “libdl” to match the config file before everything started working.
Ooh shoot. Thanks for checking that! By default, the Xamarin.Android build system only copies configuration that it finds in the same directory as the resolved path of the corresponding assembly.
One approach for a prebuilt library would be to copy a library configuration file alongside the resolved location of the library. For example, for a
<PackageReference>style NuGet package, you could put a library configuration file in the appropriate lib\* subdirectory under the %USERPROFILE%\.nuget cache directoryAnother option that might be more convenient is to use a custom target to extend the Xamarin.Android build system to copy the library configuration file from the app project during the build:
Add a configuration file to the Android app project that matches the assembly name of the library that has the
DllImport. So for example, if the library is AndroidLibrary1, name the file AndroidLibrary1.dll.config.In this case, it’s fine to leave the Build Action as None and Copy to Output Directory as Do not copy. (The custom target in step 4 will take care of the copying instead.)
As before, set the file contents to:
Open the Android app project .csproj file in a text editor, and add the following custom target to the end of the file, just before the closing
</Project>tag:Be sure to replace AndroidLibrary1.dll.config in the
SourceFilesandDestinationFilesparameters with the file name from steps 1–3.Clean and rebuild the project.
One caution about this approach is that it will overwrite the existing configuration file for the library if the library comes with one.