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

Asked by: Kellan437 | Posted: 25-01-2022

Answer 1

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.

Answered by: Anna542 | Posted: 26-02-2022

Answer 2

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


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} };

Answered by: Victoria132 | Posted: 26-02-2022

Answer 3

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

Answered by: Dexter666 | Posted: 26-02-2022

Similar questions

How to build an android app with external libraries using ant?

I have an existing project that builds fine using my IDE. I'd like to use the "android update" command to generate an ant buildfile for this project. The buildfile is generated fine, but the build fails because it's not building with some jarfiles I have in my libs directory. I'd like to fig...

opencv - Computer Vision and AR libraries availabe for Android?

I am starting out as an Android Developer, and I would like to know if there are any Computer vision libraries or Augmented Reality libraries for the Android SDK, as I am planning to use these libraries for a mobile app. I have read that if I download the NDK, I might be able to "import/use" the C openCV, and ARtoolkit libraries, but I am wondering if this is possible, or if there is a better and easier way of usin...

java - What can I access in Androids Native libraries? And How?

I am completely new to the NDK. I have done a couple of the tutorials including the hello from jni one and another one that calculates the sum of two numbers. They involved using cygwin and the ndk to create the library so file and I have a bit of a grasp on how to insert my own libraries into the libraries layer of Android. I have now been asked to access the native libraries on Android and see wha...

java - Project setup for creating third party libraries for Android

I am creating a library for Android that others can include in their own project. So far I have been working on it as a normal Java project with JDK 1.6 setup as system library. This works just fine in Eclipse when I add the android.jar. The issue comes when I try to my build script. I am running Gradle and doing a normal compile and test build cycle. My thoughts were that it does not matter if I compile it with a ...

ant - How to add external jar libraries to an android project from the command line

I'm trying to build an Android project that has some dependencies. The jar files are in the lib/ directory. I can build the project by adding those jar file to my classpath, but of course it Force Closes in the emulator because those libraries aren't present. I'm doing this from the command line with ant (not in eclipse). How can I make it include those libraries in the apk from the command line...

java - Do external libraries make apps slower?

I am building an app that scrapes information from web pages. To do that I have chosen to use an html scraper called Jsoup because it's so simple to use. Jsoup is also dependent on Apache Commons Lang libray. (Together they make up a total of 385kB ). So Jsoup will be used to Download the page and parse it. My question is if the use of these simplifying libr...

build - Target 'Android 1.5' does not support building project with libraries

I'm trying to build a project in Android 1.5 that references another Android (library) project compiled against Android 1.5 and I keep seeing this error in the Console in Eclipse: Target 'Android 1.5' does not support building project with libraries. I'm running Eclipse 3.5, and version 0.9.7 of the ADT. I followed the instructions here:

android UI external libraries?

I'm starting with android, and the app I'm developing is gonna need custom widgets look (glossy buttons, animated backgrounds etc.), I've googled for any external libraries to achieve this and did not find anything. let me guess, the only way to this is by painly extending base view classes and overriding onDraw etc. ?

How do I add the JJIL libraries to my Android Eclipse project?

I'm trying to use the JJIL libraries with my Android project: http://code.google.com/p/jjil/ I followed all the instructions on importing libraries into eclipse on the android guides, and have had no successes. It just fails to import. Can some one point me in the right direction with this? Cheers, Nick D

How to develop using android libraries in a simple java project (without using dalvik and such)

As I am very pissed off of using the emulator to develop any Java class, I thought of setting up a project in Eclipse and instead of the usual JRE I linked to the Android.jar (version 2.1) that usually the Android projects link to. I don't mean to use this to develop Layouts or other specific platform things, I was just trying to create a class that uses HttpClient. It miserably crashes like this. E...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android