Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i call C/C++ code from Android using JNA?

I'm trying to integrate this specific library to my Android project, and the library is written in C/C++. I've miraculously gotten ndk-build to give me the needed .so file.

However, looking at it, there's a sample in the project, and they use a mysterious .jar with the API bindings of the .c/c++ files.

How do i either

  • create this special .jar file that has the API, based on the .so?

OR

  • directly add a method to the main c++ file and then call it from Java?

I've tried to re-wrap things using JNI, but it definitely doesn't seem to work. i keep getting UnsatisfiedLinkError.

A lot of the documentation online uses jni as the tutorial. i'm happy with just a few links to tutorials on JNA.

like image 397
David T. Avatar asked May 12 '15 00:05

David T.


1 Answers

JNA provides a stub native library, libjnidispatch.so for a variety of platforms. You can build this library yourself, or extract one of the pre-built binaries from the project's lib/native/<platform>.jar packages.

You include libjnidispatch.so in your Android project the way you would any other JNI library. This is required; you cannot rely on JNA to dynamically unpack and use its native library automatically like on other platforms. The JNA project includes details for doing so (as well as instructions for building libjnidispatch.so yourself).

You then use jna.jar as you would any other Java jar file, and write your own (Java) mappings to match the native library you're trying to access. There's also a jna-min.jar which omits all the native platform libraries that are normally bundled in jna.jar.

like image 181
technomage Avatar answered Sep 23 '22 18:09

technomage