Bug 4462 - DllNotFoundException when trying to use native library.
Summary: DllNotFoundException when trying to use native library.
Status: RESOLVED INVALID
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.0
Hardware: PC Mac OS
: High major
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-04-16 10:22 UTC by Jonathan Clement
Modified: 2012-04-17 17:20 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Repro (435.46 KB, application/zip)
2012-04-16 10:22 UTC, Jonathan Clement
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description Jonathan Clement 2012-04-16 10:22:15 UTC
Created attachment 1683 [details]
Repro

I'm trying to include a standard sqlite library in my application in order to support different Android versions.

For exemple, Android 2.1's sqlite version does not include function like sqlite3_config, sqlite3_next_stmt.

I added an ARM libsqlitecustom.so (extracted from my Android 2.3 device) in my project under lib/armeabi. 

I set the libsqlitecustom.so to AndroidNativeLibrary.

If I call :

[DllImport("libsqlitecustom", EntryPoint = "sqlite3_config")] 
public static extern Result Config (ConfigOption option); 

I always get a DllNotFoundException. I've tried with "libsqlitecustom.so", "sqlitecustom", "sqlitecustom.so", "lib/armeabi/libsqlitecustom" etc 

How to reproduce:

Compile and start the repro project and you'll get the DllNotFoundException.

I get this exception on all my devices (Android 2.1, 2.2, 2.3).
Comment 1 Jonathan Pryor 2012-04-17 17:09:12 UTC
How are you building libsqlitecustom.so? I don't think that it's valid. I say this for two reasons:

1. If I enable Mono's native library logging:

    # http://docs.xamarin.com/android/advanced_topics/diagnostics
    adb shell setprop debug.mono.env MONO_LOG_LEVEL=info

I get the following on `adb logcat`:

> D/Mono    (18754): DllImport attempting to load: 'libsqlitecustom'.
> D/Mono    (18754): DllImport loading library: '/data/data/BundleNativeLibrary.BundleNativeLibrary/files/.__override__/libsqlitecustom'.
> D/Mono    (18754): DllImport error loading library 'Cannot load library: load_library[1104]: Library '/data/data/BundleNativeLibrary.BundleNativeLibrary/lib//data/data/BundleNativeLibrary.BundleNativeLibrary/files/.__override__/libsqlitecustom' not found'.
> D/Mono    (18754): DllImport loading library: '/data/data/BundleNativeLibrary.BundleNativeLibrary/files/.__override__/libsqlitecustom.so'.
> D/Mono    (18754): DllImport error loading library 'Cannot load library: load_library[1104]: Library '/data/data/BundleNativeLibrary.BundleNativeLibrary/lib//data/data/BundleNativeLibrary.BundleNativeLibrary/files/.__override__/libsqlitecustom.so' not found'.
> D/Mono    (18754): DllImport loading location: 'libsqlitecustom'.
> D/Mono    (18754): DllImport error loading library: 'Cannot load library: load_library[1104]: Library '/data/data/BundleNativeLibrary.BundleNativeLibrary/lib/libsqlitecustom' not found'.
> D/Mono    (18754): DllImport loading location: 'libsqlitecustom.so'.
> D/Mono    (18754): DllImport error loading library: 'Cannot load library: reserve_mem_region[831]: OOPS:    67      prelinked library 'libsqlitecustom.so' mapped at 0x48000000, not at 0xa8b00000'.

We probe several different files and directories, but the valie one -- libsqlitecustom.so -- fails:

  prelinked library 'libsqlitecustom.so' mapped at 0x48000000, not at 0xa8b00000

2. So sanity-check the .so (this isn't perfect, but it's a useful guideline):

    $ANDROID_NDK_PATH/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86/bin/arm-linux-androideabi-ld
      --unresolved-symbols=report-all
      -L$ANDROID_NDK_PATH/platforms/android-9/arch-arm/usr/lib
      -lm -lc -lz -ldl -llog
      lib/armeabi/libsqlitecustom.so

(Change/update the -lfoo lines to list every native library that libsqlitecustom.so was linked against.)

The above command lists 33 unresolved symbols, e.g.

    lib/armeabi/libsqlitecustom.so: undefined reference to `utf32_at'

You need to use the NDK toolchain to build libsqlitecustom.so, and only link against native libraries that are included with the NDK or themselves linked against the NDK.
Comment 2 Jonathan Clement 2012-04-17 17:20:28 UTC
I extracted the libsqlite.so from my device (adb pull /system/lib/libsqlite.so) and then I tried to load it in my application as a AndroidNativeLib (on the same device).

I though that the library would be ok, but maybe I should build my own libsqlite.so. 

Thank for the advice.