howtechstuffworks
howtechstuffworks

Reputation: 1926

Need directions to integrate already existing c++ code to Android NDK

I have some C++ code (interacts with micro controllers) written already by someone else. I learnt android & NDK and comfortable writing small sample programs. Now I need to integrate both.

So, How should I start proceeding on the integration part? How does the NDK actually works? Assuming I have 3 parts now A - C++ code, B - NDK native interface code, C - Android Activity/Class .

1) Should I compile A (g++ linaro) and then place the object file in Android project to be called by C through B?

(or)

2) Should I compile the A & B together using g++ (linaro) and then copy the .so file into the Android Eclipse project? (Not sure how complex it will be to mimic NDK-build command in normal eclipse).

(or)

3) Copy A into Android Eclipse project and generate java.h file, then generate .so file using the both A & B. (In this method I need to find the right place to put the whole CPP project files in the Android/NDK eclipse project).

PS: I tried to find examples that does this, but only seem to find the simple basic examples, which I am pretty comfortable creating already. I need help in the integration part, please post me tutorial if you know (Android/NDK/Eclipse/already_existing_C++_code).

Upvotes: 1

Views: 345

Answers (2)

Alex Cohn
Alex Cohn

Reputation: 57163

You should compile A using the Android toolchain. Note that Android supports not only ARM (a.k.a. armeabi) but also armv7a, x86, mips, and recently - armeabi-v7a-hard. Soon, x86-64 will be released.

You can compile A with Android standalone toolchain, no need to adopt the NDK build system.

You can compile B as part of A, or separately. In the latter case, simply load A before B in your Java static constructor:

{
    loadLibrary("A");
    loadLibrary("B");
}

because libB.so will have dependencies on libA.so.

You can pack both libA.so and libB.so in the APK (in folders libs/armeabi, libs/x86, etc.)

Upvotes: 2

Kazuki Sakamoto
Kazuki Sakamoto

Reputation: 13999

First of all, I recommend you to read Android NDK documents. Android.mk is not hard to write in order to compile C++ code into shared library for JNI using NDK toolchain. The most difficult part might be that Android libc (bionic) is not the same as ordinary Linux libc.

So, try to compile A - C++ code using NDK toolchain first. If you failed it, you should port it to Android libc, or you should compile it and link statically it using linaro toolchain. Take a look at the documents to link static elf library using NDK toolchain. But the binary wouldn't work on Linux because Android Linux kernel is not the same as linaro.

Anyway if you got to compile a shared library, easy to integrate it to Android project. just put the shared library to libs/[arch], like libs/armeabi-v7a/libfoo.so.

Upvotes: 0

Related Questions