android: VS 16.2.0 regression: failed DllImport on the libdl system library.

Steps to Reproduce

  1. Update to the initial VS 16.2.0 release (which includes Xamarin.Android 9.4.0.51).
  2. Create a blank Xamarin.Android application via the appropriate template.
  3. Atop MainActivity.cs, add usings for System and System.Runtime.InteropServices.
  4. To the MainActivity class, add:
        [DllImport("libdl", EntryPoint = "dlopen")]
        private static extern IntPtr DLLOpen(string fileName, int flags);
  1. To MainActivity.OnCreate(Bundle), add a dummy invocation of the above (DLLOpen("foo", 0)).

  2. 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

Most upvoted comments

Any update on this one?

FYI, after an initial test here:

  • This workaround worked for the case where the DllImport declaration, call are in the app itself (per the simple test case in this issue’s description).
  • The workaround did not work for our real use case where the DllImport and 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.

  1. 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.)

  2. Leave the Build Action of the file as None, and set the Copy to Output Directory property to Copy if newer.

  3. Set the file contents to:

    <configuration>
      <dllmap dll="libdl" target="__Internal" />
    </configuration>
    

    (If you already have an application configuration file, you can just add the <dllmap> element to it.)

  4. 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 directory

Another 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:

  1. 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.

  2. 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.)

  3. As before, set the file contents to:

    <configuration>
      <dllmap dll="libdl" target="__Internal" />
    </configuration>
    
  4. 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:

    <PropertyGroup>
      <AfterGenerateAndroidManifest>
        $(AfterGenerateAndroidManifest);
        CopyExtraConfigFiles;
      </AfterGenerateAndroidManifest>
    </PropertyGroup> 
    <Target Name="CopyExtraConfigFiles">
      <CopyIfChanged
          SourceFiles="AndroidLibrary1.dll.config"
          DestinationFiles="$(MonoAndroidIntermediateAssemblyDir)AndroidLibrary1.dll.config" />
    </Target>
    

    Be sure to replace AndroidLibrary1.dll.config in the SourceFiles and DestinationFiles parameters with the file name from steps 1–3.

  5. 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.