3

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. But I don't know how to load this library in my android studio app and call native function from java code. 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/

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. 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)?
  4. How to call function available in that static library with the help of header file from java code?
Janaki
  • 145
  • 2
  • 16

1 Answers1

2

You need to do several things:

  1. Compile a dynamic library. This is a .so file in Android. You can do this with the 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.
  3. You should wrap your library in JNI. Place them as the advice in this other question: JNI folder in Android Studio
  4. If you have something like this in android:

    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:

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. Note also that you do not need any header file from C++, you just load the library from Java and declare a native function. The library should be already compiled and in the right directory before the Java project uses it.

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.

Community
  • 1
  • 1
Germán Diago
  • 7,473
  • 1
  • 36
  • 59
  • German Diago, I think your answer is for shared library (.so). I need to use static library(.a). Can you help me, to load this static library in android studio and access the functions inside that prebuild static library. – Janaki Jan 23 '17 at 09:46
  • The way to load a static library is that you create a shared library and link your static library to it with the android ndk, later open it. You do not load static libraries. You can only load at runtime .so libraries as far as I know. Noone loads static libraries in android projects, never saw it before. – Germán Diago Jan 23 '17 at 09:49
  • I have used `g++ -shared -o libztx.so ZTX.o` and `ld -r ZTX.o -o libztx.so` command to build so library. But it throws `ZTX.o: Relocations in generic ELF (EM: 40) ZTX.o: Relocations in generic ELF (EM: 40) ZTX.o: Relocations in generic ELF (EM: 40) ZTX.o: Relocations in generic ELF (EM: 40) ZTX.o: Relocations in generic ELF (EM: 40) ZTX.o: error adding symbols: File in wrong format collect2: error: ld returned 1 exit status` error in terminal. Can you help to build .so library from .a library. – Janaki Jan 23 '17 at 10:03
  • Try to use an ndk project that can be built with Android.mk and co. You list your library as an object file: https://developer.android.com/ndk/guides/android_mk.html I am not sure of the problem buy you might be missing some flags or steps. Just use ndk-build to make sure you are doing the right thing. A command line might not be enough. – Germán Diago Jan 23 '17 at 10:14
  • This should also be relevant: http://stackoverflow.com/questions/5463518/android-ndk-link-using-a-pre-compiled-static-library – Germán Diago Jan 23 '17 at 10:15
  • I have tried by exploring above mentioned link. But I got Unsatisfied linker error while calling native function. – Janaki Jan 23 '17 at 10:26
  • Can you load the library? That should be in the right direction already. If you cannot call it, it is because you may be misnaming your function. Try a very basic example first, later, when you feel confident, port the idea somewhere else: http://stackoverflow.com/questions/1358541/jni-hello-world-unsatisfied-link-error – Germán Diago Jan 23 '17 at 10:29
  • Is there any way to verify whether the library is loaded properly or not? – Janaki Jan 23 '17 at 10:36
  • You can inspect first that the library is indeed an arm one or whatever with some commands. For example the command file mylib tells you the kind of binary you have. Later you can inspect with nm the symbols to see if the symbol you are looking for is indeed there: http://stackoverflow.com/questions/34732/how-do-i-list-the-symbols-in-a-so-file. If you can load the library almost for sure it is the symbol what is wrong. – Germán Diago Jan 23 '17 at 12:51