Using a java library and a native library (.jar and .so) -> 'Java.Lang.UnsatisfiedLinkError'

GaborGabor USMember
edited March 2013 in Xamarin.Android

Hi guys, I'm new to Xamarin and I have a serious namespace(?) problem using a java library and native library.

My target is to create a "3rd" party SDK from one of our project (and use this from a MonoAndroid testproject).
It was written in Eclipse (java and C++). The SDK should contains a jar file (from the java source) a .so file (compiled c and c++ code) lots of resources and some assets.
So I set the "Is Library" flag in Eclipse, compiled, and made a zip file fom asset, bin, res and libs folders.

With this tutorial, I made the binding project:
Added the zip, set metadata.xml to fix compilation errors, so it's compiling succesfully.

After that, I created a very simple testproject in XamarinStudio to test the bindig project. It's only a new class and a StartActivity to start an activity from the java. Added references (the bindig project and, set the same target framework....)

My first problem was a "java.lang.classnodeffounderror: "$layout" when the activity try to create the view (when the java code try to use it's resource). It was strange, that all of the resources from my SDK are in the testproject namespace. I fixed this issue in the java code with change the "R.Layout.mainpage" to this piece of code:
"getContext().getResources().getIdentifier("mainpage", "layout", getContext().getPackageName())".
This fix is from here:
Well it is compiling and working, but I don't think this is a proper solution for this issue....

The next issuie is more seroius and I could not solve: when a java code tries to call a native function I got a "UnsatisfiedLinkError".
Call stack:

_Instance   {Java.Lang.UnsatisfiedLinkError: Exception of type 'Java.Lang.UnsatisfiedLinkError' was thrown.
at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00024] in /Users/builder/data/lanes/monodroid-lion-bs1/5976366b/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:616
at Com.Logmein.Rescue.Android.BaseActivityProxy.OnCreate (Android.OS.Bundle) <IL 0x0009d, 0x003eb>

  --- End of managed exception stack trace ---
java.lang.UnsatisfiedLinkError: Native method not found:;)Z
    at Method)

I found this letter:
and I checked the problems: the .so file is in the apk and loaded succesfully with System.LoadLibrary(), and all arm architecture (armeabi and armeabi-v7a) available. Also, in the .so files this line can be found: "Java_com_logmein_rescue_android_nativeif_NativeInterface_LMIInit"

I can call functions from C# using .so file with [dllimport()...].

  • I tried to put the binding project and the testproject into the same solution - no luck.
  • I tried to put the .so files into the testproject, the binding project and inside the zip - no luck.
  • In Eclipse I can create the testproject (depends on my SDK project) witch is start and running normaly.
  • I downloaded the actionbarSherlock example and I "recreated" it from scratch using the sources and the zip file (to check my workflow) and it is working.
  • I compared the actionbarSherlock with my projects settings, did not found anything.

Can anyone help with this? Is this a namespace issue or something else?


  • AtsushiEnomotoAtsushiEnomoto JPMember, Xamarin Team Xamurai

    Current stable Xamarin.Android does not support embedding AndroidNativeLibrary (.so files) into a binding library dll (which is why we have some outdated version of ABS in our monodroid-samples). It should be supported (only) in the latest alpha version.

    For Resource classes You most likely need this workaround for ID mismatch issue. I'm not particularly sure if it works for you though, but it is required regardless of that.

    (To make things sure we need your project to see what exactly happens.)

    I had no idea for a while what you meant by "namespace issue", maybe the one this post mentioned? No, it is only about earlier versions than 4.2.5. This is fairly old to read as a reference.

    On your UnsatisfiedLinkError, I wonder if your packaging of .so files is really correct. You most likely need to add your .so files in the application project as AndroidNativeLibrary and they have to be in correct folder (e.g. libs/armeabi-v7a/ Or try the latest alpha that supports EmbeddedNativeLibrary in jar binding projects.

  • GaborGabor USMember

    Thanks for the fast response!

    The resource workaround is working like a charm :)
    Using "alpha channel Xamarin 4.7" my .so file is linking as it should! The .jar can call functions from the .so file! :)

    Thank you, Eno!

  • AnujKapoorAnujKapoor USMember

    Thanks Eno, this helped a lot

  • DadyaDadya INMember

    Hello, I got error Java.Lang.UnsatisfiedLinkError: Couldn't load hjh-gpio from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/BarcodeScanningApp.Droid-1.apk"],nativeLibraryDirectories=[/data/app-lib/BarcodeScanningApp.Droid-1, /vendor/lib, /system/lib]]]: findLibrary returned null.I didnt get this file while add so file in my project under libs->armeabi->so files and set build action androidNativeLibrary in xamarin android cross platform application. Please give solution sir.

  • AlanClarkAlanClark USForum Administrator, Xamarin Team Xamurai

    @Dadya Since the issue in this thread was almost 4 yrs old I would advise creating a new thread for your issue. This will ensure that old / fixed issues are not confused with the issue you currently have. Please feel free to create a new thread. Thanks!

Sign In or Register to comment.