如何使用 JNA 从 Android 调用 C/C++ 代码? [英] How do i call C/C++ code from Android using JNA?

查看:37
本文介绍了如何使用 JNA 从 Android 调用 C/C++ 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此特定库集成到我的 Android 项目中,并且该库是用 C/C++ 编写的.我奇迹般地得到了 ndk-build 给我所需的 .so 文件.

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.

但是,看了一下,项目中有一个示例,他们使用了一个神秘的 .jar.c/c++ 的 API 绑定 文件.

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.

我该怎么做

  • 根据 .so 创建这个具有 API 的特殊 .jar 文件?
  • create this special .jar file that has the API, based on the .so?

  • 直接在c++主文件中添加方法,然后从Java中调用?
  • directly add a method to the main c++ file and then call it from Java?

我尝试使用 JNI 重新包装东西,但它似乎肯定不起作用.我不断收到 UnsatisfiedLinkError.

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

很多在线文档都使用 jni 作为教程.我对 JNA 教程的几个链接感到满意.

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

推荐答案

JNA 为各种平台提供了一个存根原生库 libjnidispatch.so.您可以自己构建这个库,或者从项目的 lib/native/<platform>.jar 包中提取其中一个预构建的二进制文件.

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.

您在 Android 项目中包含 libjnidispatch.so,就像在任何其他 JNI 库中一样.这是必需的;您不能像在其他平台上那样依赖 JNA 来动态解包并自动使用其原生库.JNA 项目包括这样做的详细信息(以及说明用于自己构建 libjnidispatch.so).

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).

然后,您可以像使用任何其他 Java jar 文件一样使用 jna.jar,并编写自己的 (Java) 映射以匹配您尝试访问的本机库.还有一个 jna-min.jar,它省略了通常捆绑在 jna.jar 中的所有本机平台库.

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.

这篇关于如何使用 JNA 从 Android 调用 C/C++ 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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