Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use C library in Android over NDK

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!

like image 814
caw Avatar asked Dec 05 '11 19:12

caw


People also ask

What C library does Android use?

libc++ LLVM's libc++ is the C++ standard library that has been used by the Android OS since Lollipop, and as of NDK r18 is the only STL available in the NDK. Note: For full details of the expected level of C++ library support for any given version, see the C++14 Status, C++17 Status, and C++20 Status pages.

Can I use C in Android Studio?

You can add C and C++ code to your Android project by placing the code into a cpp directory in your project module. When you build your project, this code is compiled into a native library that Gradle can package with your app.

What compiler does NDK use?

The Clang compiler in the NDK is useable with only minimal configuration required to define your target environment. Note: Prior to NDK r19, the toolchains installed by default with the NDK could not be used in-place, and make_standalone_toolchain.py needed to be used instead. This is no longer the case.

Is NDK necessary for Android Studio?

Prerequisite. To use Android NDK support in your application, you need to download the following three tools: LLDB: It is used by Android Studio to debug the native code present in your project. NDK: Native Development Kit(NDK) is used to code in C and C++ i.e. native languages for Android.


2 Answers

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

like image 148
aardvarkk Avatar answered Sep 25 '22 00:09

aardvarkk


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.

like image 32
mchang Avatar answered Sep 22 '22 00:09

mchang