How can I call C / C ++ code from Android using JNA?

I am trying to integrate this specific library into my Android project, and the library is written in C / C ++. I miraculously got ndk-build to give me the necessary .so file.

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

How can I

  • create this special .jar file with .so based API?

OR

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

I tried to override things using JNI, but it doesn't seem to work. I keep getting UnsatisfiedLinkError .

Throughout the documentation on the Internet, jni is used as a textbook. I am pleased with just a few links to JNA tutorials.

+3
c ++ android android-ndk jni jna
May 12 '15 at
source share
2 answers

JNA provides a source stub library, libjnidispatch.so for various platforms. You can create this library yourself or extract one of the previously created binaries from the lib/native/<platform>.jar project packages.

You include libjnidispatch.so in your Android project, like in any other JNI library. It's necessary; you cannot rely on JNA to dynamically unpack and use your own library automatically, as on other platforms. The JNA project contains data for this (as well as instructions for creating libjnidispatch.so yourself).

Then you use jna.jar , like any other Java jar file, and create your own (Java) mappings to match the source library you are trying to access. There is also jna-min.jar that skips all the native platform libraries that are usually contained in jna.jar .

+6
May 13 '15 at 1:13
source share

Go to project properties and build paths and remove JNA 4.0 and related classes. It will work!

0
Aug 25 '16 at 9:15
source share



All Articles