Click here to Skip to main content
15,882,903 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Updated 12-Apr-17 11:40am
Richard MacCutchan 13-Apr-17 4:41am    
Are you sure that the C++ library will run in an Android environment?
vishalgpt 16-Apr-17 10:11am    
Sorry Its Plain "C" library

1 solution

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!
Share this answer
vishalgpt 16-Apr-17 10:11am    
Agree with you. Sorry for bit losely. Its Typical "C" library not "C++" library. Have done some search and found that **JNI** is the main hero here. There are certain requirement/situations where one have to use the Native Library in android. Like it or not it is a big fact. It brings advantage with disadvantage also.

Is there any good JNI Books out there which is suitable for android development. With good examples in it.
Nick_3141592654 16-Apr-17 12:37pm    
I haven't found a great book specifically covering JNI, having several books but it's a topic that seems to be glossed over. I find that most android books trawl over the same topics that are covered very well already by the Google tutorials. There are however some quite good online resources, for example this one:

I think the easiest way "in" now would be to take one of the Google sample projects for JNI and learn by example.

Once you can build a JNI library and do some kind of "Hello world!" in JNI you'll soon be flying.

Regarding pros and cons, of course the big pro is performance. I wrote real-time video processing app in pure Java, not really expecting it to run fast enough and indeed it was horribly slow. JNI gave me at least a x10 speed improvement. If, however, you don't really need such speed, then as the standard Google tutorials say don't bother with JNI. You really need to determine that there's a need for it, rather than doing this just because it seems "professional" or otherwise a generally good thing to do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900