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

非 Y 不嫁゛ 提交于 2019-11-30 09:02:20

问题


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.


回答1:


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.




回答2:


Do go to project properties and build paths and remove JNA 4.0 and related classes. This will work!



来源:https://stackoverflow.com/questions/30179833/how-do-i-call-c-c-code-from-android-using-jna

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!