0

I am new in JNI and trying to use a [library][1] in android project using ndk. It seems the

It seems the library is loaded in the android project properly using the following code :

static{
        System.loadLibrary("usb1.0");
    }

but when I try to call a native method following the libusb API like libusb_init() I get following error :

 No implementation found for void com.example.hellojni.MainActivity.libusb_init() (tried Java_com_example_hellojni_MainActivity_libusb_1init and Java_com_example_hellojni_MainActivity_libusb_1init__)

what am I missing ? Do you have any better beginner examples ? [1]: https://libusb.sourceforge.io/api-1.0/index.html

mzc
  • 31
  • 4
  • do you have the code for the JNI layer of that library? the method names don't seem match with your java project's packagename – Shark Jul 08 '21 at 15:45
  • i think you're right. no I don't have the code for that layer, I have built using ndk-build as described [here](https://github.com/libusb/libusb) and added the android.mk in android studio project could you please tell how to generate the JNI layer code ? – mzc Jul 09 '21 at 06:25
  • hmm, some of my (much) older questions were related to JNI (or JNA), take a look at some of them. [link1](https://stackoverflow.com/questions/12214707/properly-returning-a-hardcoded-byte-from-jni-to-java), [link2](https://stackoverflow.com/questions/8532642/c-to-java-calls-pass-but-jvm-crashes-in-a-weird-unexplained-way) you'll find the useful links for JNI there and get some ideas. but i'm personally not a huge fan of generating the JNI layer, although if you have tons of methods - you can use [SWIG](http://www.swig.org/) for that – Shark Jul 09 '21 at 10:27
  • but basically, the JNI layer is just C code that's boilerplate for java - it defines what parts (methods) of the native library get exposed to Java and does the necessary type conversions (e.g. `int` to `jint` and such). Some reading and digging for material is required though until you get the concept. Some of that material can be found in the links i posted. – Shark Jul 09 '21 at 10:30

0 Answers0