android - 在 Android 应用程序中使用现有的共享库 (.so)

标签 android java-native-interface android-ndk

我有以下场景需要处理。我获得了一个共享库 (libeffect.so),可以在我为客户工作的 Android 项目中使用。我没有共享库源代码,我只有 .so 文件。该库已预编译以在 Android 设备上运行。除了共享库,我还有方法签名

public static native void doEffect(int param1, IntBuffer intBuffer);

所以现在我有一些关于如何调用这个本地方法的问题,当然,如果这可能只有 .so 文件,那么它们是:

  1. 我是否需要将 native 方法签名放在与 .so 时定义的包/类相同的包/类中,或者我可以在我的项目中的任何包/类中使用此签名,在运行时 jvm 将是能够在共享库中找到方法?例如,如果这个共享库首先在类 mypackage.MyClass 中使用,是否需要创建相同的包、类,然后将方法签名放在那里?

  2. 我需要将这个 .so 文件放在我的 eclipse android 项目中的什么地方才能将这个文件部署到我的 apk 文件中?

这些问题可能听起来很菜鸟,但我以前从未使用过 jndi,所以我有点担心是否可以实现无错误地调用方法 doEffect。非常欢迎任何可以指导我的答案。

非常感谢 蒂亚戈

最佳答案

  1. Do I need to place the native method signature in the same package/class as those defined when the .so was or I can use this signature in any package/class in my project that during runtime the jvm will be able to find the method in the shared library? For example, if this shared library was firstly used in a class mypackage.MyClass, do I need to create the same package, class and then put the method signature there?

无需创建相同的包/类。您可以将方法签名放在任何包中。

public class NativeLib {

  static {
    System.loadLibrary("so_file");
  }

  public static native void doEffect(int param1, IntBuffer intBuffer);

}

2.Where do I need to place this .so file inside my eclipse android project to get this file deployed inside my apk file?

您已将此 .so 文件放入应用程序的 lib 文件夹中。如果 lib 文件夹不存在,那么您可以创建一个 lib 文件夹并放置 .so 文件。您可以使用 System.loadLibrary("so_ file");

调用它

关于android - 在 Android 应用程序中使用现有的共享库 (.so),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6449225/

相关文章:

android - 如何在android中实现启动画面的执行程序

android - 从 fragment 的 onCreateView 调用 AsyncTask 方法失败

java - 为什么我的 native C++ 代码在 Android 上的运行速度比 Java 慢得多?

android - 在android中通过JNI在C代码中打开文件时获取NULL

android - IntetentService 中的 NetworkOnMainThreadException

android - Android 生命周期方法中通常应实现哪些应用程序操作

java-native-interface - JNI_CreateJavaVM 使我的程序崩溃,没有任何消息或异常

opengl-es - Android,使用 NDK 优雅地关闭 OpenGL?

android - 在APK中包括刚刚选择的.so库

c++ - 未定义对 'av_rdft_init(int, RDFTransformType)' 的引用