'Is it enough to wrap exported functions of a C library using JNI for it to work using JNI?
I am trying to wrap a C library using JNI.
I am interested about the prospects of using any C library using JNI (particularly with Android, but I assumed this compatability should be the same as being compatible with JNI).
Without being an original developer of a C library, I've found it confusing to know, what methods I need to register in JNI_OnLoad (https://developer.android.com/training/articles/perf-jni#native-libraries).
To progress, I figured out with the help of https://stackoverflow.com/a/4514781/4959635 to call
nm myclib.a | grep " T "
to find a list of (supposed) "exported functions". However, the nm
output may well tell other things as well.
Now my question is,
will the library work, if I wrap only the exported functions via JNI_OnLoad?
Or do I need to ensure other qualities in the library in order to guarantee that it will work with JNI?
On the other hand, I read some other discussions such as:
https://www.thecodingforums.com/threads/unsatisfiedlinkerror-when-loading-shared-lib.360242/
which suggested that having undefined symbols might mean that the C code is not structured properly for JNI.
Whereas this resource:
seems to suggest that wrapping exported functions is in fact what's expected.
Solution 1:[1]
No. Functions you call from JNI must always follow a specific signature: (ignoring the return type for now)
fun(JNIEnv *env, jobject obj, ...)
or
fun(JNIEnv *env, jclass cls, ...)
where the remaining arguments are types that the JVM can process.
You will need to create wrapper functions that go between the JVM types and whatever types your functions expect. See the SWIG library for a potential means of auto-generating these wrappers, but know you will still need to write quite a bit of custom code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Botje |