Use C library in Android over NDK

10,993

Solution 1

A good tutorial for how to start working with the NDK can be found here. And yes, you should be able to get it to compile and call it from the NDK without many changes (assuming the code doesn't reference other libraries).

Solution 2

Look at the NDK getting started samples here: http://developer.android.com/sdk/ndk/overview.html#samples

Then in your NDK, look at the two-libs example. You will probably want to just statically link your third-party audio pitch detection library to your own C code.

You'll need to look at the Android.mk and modify it to build your third-party library statically and then indicate that your main project uses that library.

It should be pretty straightforward. The NDK (haven't used it in a while) is a bit of a bear. So make sure your build environment (especially if you are using Windows + Cygwin) works. Make sure the hello-jni builds, and the default two-libs builds. Modify the second one and you should be there.

Share:
10,993
caw
Author by

caw

Updated on June 09, 2022

Comments

  • caw
    caw almost 2 years

    What I want to do:

    I've found a C library which computes an audio stream's pitch and want to use it in Android.

    I thought instead of porting it I could also use it with the help of the NDK, right?

    How does this work? I have to install the NDK, of course, and then? Can I call functions of this C library just as normal in Android?

    The library in C that I want to "import":

    #include "second_c_file.h"
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    
    #ifndef max
    #define max(x, y) ((x) > (y)) ? (x) : (y)
    #endif
    #ifndef min
    #define min(x, y) ((x) < (y)) ? (x) : (y)
    #endif
    
    int _power2p(int value) {
        ...
    }
    
    typedef struct _minmax {
        int index;
        struct _minmax *next;
    } minmax;
    
    double _test_calculate(double * var1, int var2, int var3) {
        ...
    }
    

    The file "second_c_file.h" is another file that I need to import, obviously.

    Thanks for your help!

  • caw
    caw over 12 years
    Thanks for the link to this tutorial! The installation works fine. But when it comes to the integration of the C library into the Android project (the actual use), it doesn't work. My Android application just shows an error message that it had been stopped unexpectedly. I don't know what I did wrong. I did what the tutorial said. And maybe you could have a look at my C library in the question: What changes do I have to do? (... as the C class names must be something like "Java_com_your_package_MainActivity_invokeNativeFunction" and in my C library there aren't any classes yet ...).
  • caw
    caw over 12 years
    Thank you very much! You helped me a lot by referencing the NDK sample "two-libs". But unfortunately, I can only choose one answer.
  • caw
    caw over 12 years
    Thank you, the tutorial you linked to helped me to install and use the NDK.