Calling .SO functions from a java Class using JNI

蓝咒 提交于 2019-12-08 15:18:44

问题


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

  1. Directly Create a JNI native methods for the header files present in .SO which will call the C methods directly.

or

  1. 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 box
  • lib.h - Library Header files exposing some lib.so methods. Those are the methods accessible to you.
  • jni.cpp and jni.h files - This code is the bridge between the java and lib side. Here, you import the lib.h header file. This way, you receive a call from java side, communicate with lib.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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!