问题
I know this might seems weird, but, right now I have a .SO (Shared Object in C) and have to make use in my Android Project.
Now My requirement is to call the functions that are in .SO directly in Java creating JNI wrapper for the headers in .SO .
My question is what I am assuming the flow as,
1.Create JNI native methods -> then Create header file and then -> create a implementation of those methods from header file, include the .h files from .SO in local implementation and call the methods present in .SO
or
- Directly Create a JNI native methods for the header files present in .SO which will call the C methods directly.
or
- Do we have any other approach?
Please correct me If I am missing something.
I have referred some online content on the same, and I could say that all these below examples are completely different than what I need.
https://www.codepool.biz/package-jni-shared-library-jar-file.html
https://www.codepool.biz/build-so-aar-android-studio.html
https://dzone.com/articles/generating-executable-jar-file
https://www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html
How to use c lib(.so) from android?
回答1:
Usually, you have:
lib.so
- The black boxlib.h
- Library Header files exposing some lib.so methods. Those are the methods accessible to you.jni.cpp
andjni.h
files - This code is the bridge between the java and lib side. Here, you import thelib.h
header file. This way, you receive a call from java side, communicate withlib.so
method and send the result back to java side.
In the end, you must have a JNI code which will responsible to talk with both: java and lib side. That implementation can be very basic like invoke a lib method and return the result or it can be more complex like starting a thread, wait for the result and then, report back when done etc with more complex classes etc.
For any case, your JNI code (be a single method or a more complex class) must include the lib.h
header files in order to be able to invoke lib.so
methods.
来源:https://stackoverflow.com/questions/55263368/calling-so-functions-from-a-java-class-using-jni