简体   繁体   English

如何从android studio链接和调用预构建静态本机库的功能

[英]How to link and call function of prebuild static native library from android studio

I need to change pitch and time stretching of an audio.我需要更改音频的音高和时间拉伸。 For this I am using prebuild static library.为此,我使用预构建静态库。 Currently I am having libZtxAndroid.a static library and corresponding header file which contains function declaration.目前我有 libZtxAndroid.a 静态库和包含函数声明的相应头文件。 But I don't know how to load this library in my android studio app and call native function from java code.但我不知道如何在我的 android studio 应用程序中加载这个库并从 java 代码调用本机函数。 I explored many links and tried to load this library.我浏览了许多链接并尝试加载这个库。 But all attempts are failed.但是所有的尝试都失败了。 This is the one link which I have tried last time https://tariqzubairy.wordpress.com/2012/03/12/use-of-prebuild-static-library-compiled-with-android-toolchain/这是我上次尝试过的一个链接https://tariqzubairy.wordpress.com/2012/03/12/use-of-prebuild-static-library-compiled-with-android-toolchain/

Also I am using FFMPEG shared library and MP4Parser ( https://github.com/sannies/mp4parser ) library in this app for adding water mark to video and merging audio respectively.此外,我在此应用程序中使用 FFMPEG 共享库和 MP4Parser ( https://github.com/sannies/mp4parser ) 库分别为视频添加水印和合并音频。 Can any one help from basics.任何人都可以从基础知识中获得帮助。

  1. How to load static library?如何加载静态库?
  2. Where I need to place that static library?我需要把那个静态库放在哪里?
  3. Where I need to create jni folder (folder structure)?我需要在哪里创建 jni 文件夹(文件夹结构)?
  4. How to call function available in that static library with the help of header file from java code?如何在java代码的头文件的帮助下调用该静态库中可用的函数?

You need to do several things:你需要做几件事:

  1. Compile a dynamic library.编译动态库。 This is a .so file in Android.这是 Android 中的 .so 文件。 You can do this with the android ndk.您可以使用 android ndk 执行此操作。
  2. There is a directory in every android project, I am saying from the top of my head, but I think it is in a jni subdirectory where you must put your library.每个android项目都有一个目录,我是从头顶上说的,但我认为它在一个jni子目录中,你必须把你的库放在那里。
  3. You should wrap your library in JNI.您应该将您的库包装在 JNI 中。 Place them as the advice in this other question: JNI folder in Android Studio将它们作为另一个问题的建议: Android Studio 中的 JNI 文件夹
  4. If you have something like this in android:如果你在android中有这样的东西:

    Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ) Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz )

where Java_comp_example_hellojni_HelloJni is your project name, you have to do from Java, assuming the name of your lib is libmylib.so:其中 Java_comp_example_hellojni_HelloJni 是您的项目名称,您必须从 Java 开始,假设您的 lib 名称是 libmylib.so:

public class HelloJni {
  static {
    System.LoadLibrary('mylib');
  }

  public native stringFromJni();
}

Note that the native library name does not need the lib prefix and the .so suffix.请注意,本机库名称不需要 lib 前缀和 .so 后缀。 Note also that you do not need any header file from C++, you just load the library from Java and declare a native function.另请注意,您不需要来自 C++ 的任何头文件,您只需从 Java 加载库并声明本机函数。 The library should be already compiled and in the right directory before the Java project uses it.在 Java 项目使用它之前,该库应该已经编译并位于正确的目录中。

Be careful at loading: if you use the shared version of the standard library, you will also need to add it to your static { section in Java for loading it, before your library.加载时要小心:如果您使用标准库的共享版本,您还需要将它添加到 Java 中的静态 { 部分以在您的库之前加载它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM