Trossachs
Trossachs

Reputation: 21

Will C/C++ libraries work on Android Phone

Does anyone have any experience using JNI to call native C/C++ libraries in Android? Is the environment suitable for running C/C++ libraries and if so is there anything specific about the environment which you need to accommodate? Thanks

Upvotes: 2

Views: 841

Answers (3)

Peter Teoh
Peter Teoh

Reputation: 6713

For a good example of C and Java integration, check this out:

https://github.com/jackpal/Android-Terminal-Emulator

Untar the files and you can find a jni/termExec.cpp - which uses normal C API like "exec()", "fork()", and "open(/dev/ptmx)" to implement terminal emulation (http://linux.die.net/man/4/ptmx).

Looking up the jni/Android.mk file, and u can see that the cpp is compiled as a library - libandroidterm.

And then the java application (src/jackpal/androidterm/Exec.java) will load the library via System.loadLibrary("androidterm").

I think this application provide a small enough example for u to extend whichever way u like - either the cpp or the java file. (The cpp file is basically C-based, not C plus plus).

And remember the mapping between them, for example here it is:

static JNINativeMethod method_table[]
= {
    { "createSubprocess", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[I)Ljava/io/FileDescriptor;",
        (void*) android_os_Exec_createSubProcess },
    { "setPtyWindowSize", "(Ljava/io/FileDescriptor;IIII)V",
        (void*) android_os_Exec_setPtyWindowSize},
    { "waitFor", "(I)I",
        (void*) android_os_Exec_waitFor},
    { "close", "(Ljava/io/FileDescriptor;)V",
        (void*) android_os_Exec_close} };

Upvotes: 1

Elliott Hughes
Elliott Hughes

Reputation: 4665

for a C library, you shouldn't have any trouble. a C++ library might be more fun if it uses much of the standard library, because most of the C++ standard library is missing, but you can always supply your own "mini-STL". that's basically how external/webkit works.

much of Android's java.util.regex, java.nio.charset, java.util, and java.text are implemented by calling ICU4C, for example. (the library's in external/icu4c and the JNI is in dalvik/libcore/icu/src/main/native.) a mix of ICU's C and C++ interfaces are used, so you can rest assured this stuff gets quite a good workout on a daily basis ;-)

Upvotes: 0

Daniel Yankowsky
Daniel Yankowsky

Reputation: 7006

My understanding is that Android provides only a subset of the standard C++ runtime library. For example, Android does not support exceptions in native code. I think there are other restrictions as well.


One complication is that, while Android itself might include many native libraries, only some of them are considered stable enough to link against. The Android NDK page lists the libraries which are safe.

  • libc (C library) headers
  • libm (math library) headers
  • JNI interface headers
  • libz (Zlib compression) headers
  • liblog (Android logging) header
  • A Minimal set of headers for C++ support

If your C library only uses those, you should be fine. C++ support sounds a little spottier.

Upvotes: 2

Related Questions