Java Question UnsatisfiedLinkError

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hi

I'm having an java.lang.UnsatisfiedLinkError: openport when calling the following method:

B4X:
public int OpenSerialPort(String port,int baudrate) {
      return getObject().OpenSerialPort(port, baudrate);
      
   }

B4X:
/**
    * Initializes the Fingerprint.
    * Make sure to have the .so file in your assets folder!
    * The .so file will be copied to the internal folder if it's not there.
    * @throws IOException 
    */
   public void Initialize(final BA ba) throws IOException {
      this.ba = ba;
      
      //Copies the file to the internal folder.
      if (!File.Exists(File.getDirInternal(), "libserial_port.so")) {
         File.Copy(File.getDirAssets(), "libserial_port.so", File.getDirInternal(), "libserial_port.so");
         Log.i(TAG, "Library copied to Internal Folder.");
      }
      
      //Load the library
      try {
         System.load(File.Combine(File.getDirInternal(), "libserial_port.so"));
         Log.i(TAG, "Library loaded!");
      } catch (Exception e) {
         Log.i(TAG, "Cannot load library: " + e.getMessage());
      }
      
      //Initialize the library
      setObject(new fingerprint_native());
      
      
   }


The object fingerprint class contains:


B4X:
static {
      System.loadLibrary("serial_port");
   }

public int OpenSerialPort(String port,int baudrate)
   {
      if(fd >= 0)
      {
         closeport(fd);
      }
      fd = openport(port,baudrate);
      if(fd < 0)
      {
         return -1;
      }
      return 0;
   }


Is there a way i'm not loading my library correctly?


Regards,
Tomas
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hi Erel,

I'm loading the library with the initialize method (first copy it to the internal folder, and then loading it there.)
I added the .so file to the file assets folder.

With the link you suggest me to:
- Make a "lib" folder in the java project (eclipse) with the .so file

B4X:
System.loadLibrary("serial_port"); 'Name of the lib is libserial_port.so

- Can i call this in the initialize method?
- What about the armeabi folders? is it like
1. lib/armeabi/libserial_port.so
2. lib/libserial_port.so

EDIT:

I'm having the exact same error with System.loadLibrary("serial_port");

** Activity (main) Create, isFirst = true **


Library loaded!


** Activity (main) Resume **


Opening serial port: dev/eser1, baudrate: 9600


main_btnfingerprint_click (java line: 339)


java.lang.UnsatisfiedLinkError: openport


at com.printer.example.fingerprint_native.openport(Native Method)
at com.printer.example.fingerprint_native.OpenSerialPort(fingerprint_native.java:13)
at com.rootsoft.printer.RSFingerPrint.OpenSerialPort(RSFingerPrint.java:71)
at com.rootsoft.rsprinter.main._btnfingerprint_click(main.java:339)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:63)
at android.view.View.performClick(View.java:2552)
at android.view.View$PerformClick.run(View.java:9229)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
at dalvik.system.NativeStart.main(Native Method)

Regards,
Tomas
 
Last edited:

warwound

Expert
Licensed User
Longtime User
B4X:
System.loadLibrary("serial_port"); 'Name of the lib is libserial_port.so

- Can i call this in the initialize method?

Best solution is a static class constructor, something like:

B4X:
public class MyClass{
  static {
    System.loadLibrary("serial_port");
  }

  // rest of the class here
  
  public native int method1(){
    // do something
  }
}

Where MyClass is the java class that wraps the native C/C++ methods in libserial_port.so.

https://www.google.co.uk/search?q=java+static+constructor+ndk&ie=UTF-8&oe=UTF-8

Martin.
 

warwound

Expert
Licensed User
Longtime User
Have you changed the package name of the class that is the interface between the C and java?
The C code might be hardcoded to call and be called by a class with a hardcoded package name, if you've changed it that might explain the problem.

Martin.

Sent from my GT-P3110 using Tapatalk 2
 

qle

Member
Licensed User
Longtime User
Hi XverhelstX (Tomas),

Did you resolve this issue?
I m trying to get my Serial Library to work using the Simple Library Complier, and i get the same error
B4X:
main_openserialport (B4A line: 43)
sp.SerialportInit("/dev/eser0",9600)
java.lang.UnsatisfiedLinkError: open
   at com.android.barcode.SerialPort.open(Native Method)
   at com.android.barcode.SerialPort.SerialPortInit(SerialPort.java:47)

I can see the 'libserial_port.so' in the File.DirAssets, and also in my File.DirInternal

I did not get the error when i used Eclipse (2 years ago, the library works well).
However, after i updated the ADT eclipse broke and complains about the versions
it seems they want me to go Android Studio

So im trying to get SLC to work with my 3rd party SDK's, with very little luck so far.

Best Regards
Lee.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
xverhelst is no more an active user here. Probably you dont get support anymore for his libs.
 

qle

Member
Licensed User
Longtime User
Hi,
I wrote the wrapper in 2012 to support the android serial library.. using ecplise. (which works)
(ie: not using his library)

But now i seem to be forced to use SLC or Android Studio for library creation of 3rd party SDK's to use in B4a , and seeing as i can find no useful information regarding Android Studio and creating libraries for B4A im trying to use the SLC to ReCreate my Libraries so i can compare.

Im using my serial port library project so i can compare the eclipse generated one and the SLC one.

And the result is that the SLC does not work (yet) and the SLC (Simple Library Compiler) version has very different jar and xml to the one eclipse made.

The SLC one when used reports the error..
B4X:
main_openserialport (B4A line: 43)
sp.SerialportInit("/dev/eser0",9600)
java.lang.UnsatisfiedLinkError: open
   at com.android.barcode.SerialPort.open(Native Method)
   at com.android.barcode.SerialPort.SerialPortInit(SerialPort.java:47)

i was hoping he had resolved 'his' error, as it seems to be the same as im getting...




Lee.
 
Last edited:

qle

Member
Licensed User
Longtime User
Thanks, Ive restored from a backup to get it working again.
Be nice to work out how to get the SLC to create the same results.. but im not that smart..

Edit:
after the restore eclipse now comes up with
"Could not create the view: org.eclipse.jdt.ui.PackageExplorer"

so i guess i will be downloading and installing the ATD bundle again... :)
 
Last edited:
Top