0

I'm currently trying to write a JNA wrapper for Libftd3xx (a library for FTDI chips). Unfortunately, I have never done this before and need some help from you guys to set things up correctly.

I already managed to call native code from my app following this nicely written tutorial for Android Studio. Furthermore, I managed to successfully compile the FTDI driver on my Windows 10 machine resulting in the file "libftd3xx.so". Now I would like to call into the FTDI driver using JNA.

The JNA website states the following:

Make your target library available to your Java program. There are several ways to do this:

The preferred method is to set the jna.library.path system property to the path to your target library. This property is similar to java.library.path, but only applies to libraries loaded by JNA.

[...]

Now I'm not sure how to proceed from here. How do I set the the jna.library.path using Android Studio? Once I have done that, how would I proceed to call into the precompiled driver?

Thank you very much for your help. It's highly appreciated.

Community
  • 1
  • 1
Hagbard
  • 3,430
  • 5
  • 28
  • 64
  • 1
    It's looking for a system environment variable, similar to this: https://developer.android.com/studio/command-line/variables. As for the second part, you'll write an interface, use Native.loadLibrary() to reference the .so file, and write method signatures to match the library and map Java classes to the native args. – Daniel Widdis Aug 01 '18 at 15:02
  • Thanks a lot for your comment. I read [here](https://stackoverflow.com/questions/2370545/how-do-i-make-a-target-library-available-to-my-java-app?rq=1) that I can set the respective path with System.setProperty("jna.library.path","path/to/libftd3xx.so"). Is that the right way to go? – Hagbard Aug 01 '18 at 15:21
  • That's one way (although just include the path/to/ part and not the .so file). There are many. Setting `PATH` from the OS works in a more "permanent" manner. You can launch java from the commandline (or in an IDE) by using `-Djna.library.path=`. Or you can do it from inside the program with `System.setProperty()`. If you do this you must do it before you use JNA anywhere else in your program, as it's part of the initialization, and won't work if you've already initialized JNA. You can also just put an absolute path in the `Native.loadLibrary()` command. – Daniel Widdis Aug 01 '18 at 19:56
  • Sorry for the late reply. I finally managed to load the library by using Native.loadLibrary(). Took me a while to figure out that I had to put the respective .so files in the "jniLibs" folder as explained [here](https://stackoverflow.com/questions/36305914/setting-up-jna-in-android-studio/36697552#36697552). Unfortunately, I now end up with the error "Native library (com/sun/jna/android-aarch64/libjnidispatch.so) not found in resource path (.)" whenever I try to call any function from my library instance. – Hagbard Aug 07 '18 at 15:31
  • I already tried to follow the steps mentioned [here](https://stackoverflow.com/questions/47800043/android-arm-libjnidispatch-so-not-found-error) but no success so far... Is there maybe another way to simply get a list of callable functions once I managed to create an instance of the library writing "D3XXLibrary Instance = (D3XXLibrary) Native.loadLibrary("libftd3xx", D3XXLibrary.class);" – Hagbard Aug 07 '18 at 15:31
  • I don't think you want it in the jnilibs folder. That's specific for JNI. You want it in a jna library path. As a debugging step put it in your project root directory and see if it works... then we can move it to a better place. – Daniel Widdis Aug 07 '18 at 19:40
  • Thanks a lot for staying with me, Daniel. Unfortunately, I didn't have any luck with that. Copying the whole folder structure including the subfolders "arm64-v8a", "armeabi", "armeabi-v7a", "mips", "mips64", "x86" and "x86_64" including the respective .so files to "app/" still results in " java.lang.UnsatisfiedLinkError: Native library (com/sun/jna/android-aarch64/libjnidispatch.so) not found in resource path (.)". This is actually quite frustrating. Is there really no default way to simply get a set (or a map) of callable functions of a .so file from within JNA? – Hagbard Aug 08 '18 at 08:10
  • 1
    I would suggest at this point you ask your question on the JNA mailing list. You'll get a wider audience. – Daniel Widdis Aug 08 '18 at 18:22

0 Answers0