在没有JNI的情况下访问android中的C ++库 [英] Accessing C++ library in android without JNI

查看:64
本文介绍了在没有JNI的情况下访问android中的C ++库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这可以在没有编写Jni Wrapper的情况下访问android中的c ++库函数。因为我必须使用一个大型库并为所有模块编写jni包装器是一项艰巨的任务。 Becoz c ++模块由众多结构和类组成。



请指导。



什么我试过了:



搜索互联网但没有找到任何令人满意的结果。

Hi to all, Is this possible to access c++ library functions in android without writing Jni Wrapper. Because i have to use a big library and writing jni wrapper for all the modules is a difficult task. Becoz the c++ modules consists of numerous structures and class.

Please guide.

What I have tried:

Search the internet but not found anything satisfactory.

推荐答案

没有JNI就不可能使用C ++ - 重点是JNI * IS *本机代码,即您想要使用C ++(或C)的事实意味着您在Java环境中使用本机库=> JNI。



请记住,您只需要在两个域之间为您的API提供JNI包装器代码:Java和本机(C ++)。因此,尽管您可能有几十(甚至几百)个C ++类/方法,但最不可能所有这些都需要在您的JNI接口上公开。



考虑不知怎的,你将不得不将这些Java方法参数传递到C ++世界,同样将结果从C ++返回给Java。你无法摆脱处理这个问题的需要。



尽管如此,例如你有大量从Java到JNI的必需方法调用有类似的方法签名,那么你可以概括这些。例如,您可以传递一个String,用于标识所需的JNI方法以及可变长度的对象数组(或整数,浮点数,双精度数等等)。单个JNI方法可以解析这样的参数列表并调用对相应C ++类/方法的调用,并类似地提供一组通用的返回结果。这需要更多的前期投资来设置界面,但一旦完成,您可以轻松添加方法调用。只有当你有许多JNI接口能够很好地适应这种概括时,我才会推荐这种方法。如果没有,除了开始输入外别无选择!
It's not possible to use C++ without JNI - the point is that JNI *IS* native code, i.e. the very fact that you want to use C++ (or C) means that you are using a native library, within a Java environment => JNI.

Remember that you only need to provide the JNI wrapper code for your API between the two domains: Java and native (C++). Therefore although you might have several tens (even hundreds) of C++ classes/methods, it's most unlikely that all of these need to be exposed at your JNI interface.

Consider this.. somehow you're going to have to transfer those Java method arguments into the C++ world and similarly return results from C++ to Java. You cannot escape from the need to handle this.

Having said all that, if for example you have a large number of required method calls from Java to JNI that have similar method signatures, then you may be able to generalise these. For example you could pass a String identifying the required JNI method along with a variable-length array of objects (or ints, floats, doubles, whatever..). A single JNI method could parse such an argument list and "farm-out" the call to the appropriate C++ class/method, and similarly provide a generic set of return results. This would require more up-front investment to set-up the interface, but once done you could add method calls easily. I would recommend this approach only if you have many JNI interfaces that lend themselves well to such a generalisation. If not, you have no choice other than to start typing!


这篇关于在没有JNI的情况下访问android中的C ++库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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