使用JNI从Java类调用.SO函数 [英] Calling .SO functions from a java Class using JNI

查看:971
本文介绍了使用JNI从Java类调用.SO函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这似乎很奇怪,但是,现在我有一个.SO(C中的共享对象),必须在我的Android项目中使用。

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.

现在,我的要求是直接在Java中调用.SO中的函数,为.SO中的标头创建JNI包装器。

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。创建JNI本机方法->然后创建头文件,然后->从头文件创建这些方法的实现,包括.h文件。 SO在本地实现中,并调用.SO

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


  1. 直接为.SO中存在的头文件创建JNI本机方法,该头文件将直接调用C方法。


  1. 还有其他方法吗?

如果我缺少某些东西,请纠正我。

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

<一个href = https://dzone.com/articles/generating-executable-jar-file rel = nofollow noreferrer> https://dzone.com/articles/generating-executable-jar-file

https: //www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html

如何从android使用c lib(.so)?

推荐答案

通常,您有:


  • lib.so -黑盒子

  • lib.h -库头文件暴露一些lib.so方法。

  • jni.cpp jni.h 文件-此代码是Java和lib端之间的桥梁。在这里,导入 lib.h 头文件。这样,您会收到来自Java端的调用,与 lib.so 方法进行通信并将结果发送回Java端。

  • 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.

最后,您必须具有一个JNI代码,该代码将负责与Java和lib端进行通信。该实现可以是非常基本的,例如调用lib方法并返回结果,也可以更复杂,例如启动线程,等待结果,然后报告完成后的报告等,其中包含更复杂的类等。

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.

在任何情况下,您的JNI代码(是单个方法还是更复杂的类)必须依次包含 lib.h 头文件就能调用 lib.so 方法。

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.

这篇关于使用JNI从Java类调用.SO函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆