Tip: Do you want to experiment with the Android native API directly on a device using a terminal emulator? Install Termux, a terminal emulator and Linux enviromnent for Android!
C runtime library (libc)
Android uses a custom C library named Bionic, which has a smaller API than a traditional Unix-like C library (it does not claim POSIX compliance). Generally, if one header is not there at build time, it's because its implementation is not available.
Java Native Interface (JNI)
POSIX threads (pthreads)
The android libc, bionic, provides built-in support for pthreads, so no additional linking (-lpthreads) is necessary. It does not implement full POSIX threads functionality and leaves out support for read/write locks, pthread_cancel(), process-shared mutexes and condition variables as well as other more advanced features. Read the bionic OVERVIEW.txt for more information.
TLS, thread-local storage, is limited to 59 pthread_key_t slots available to applications, lower than the posix minimum of 128.
The Android default system C++ runtime is a small C++ subset (e.g. no
std::vector) limited to the following headers:
cassert cctype cerrno cfloat climits cmath csetjmp csignal cstddef cstdint cstdio cstdlib cstring ctime cwchar new stl_pair.h typeinfo utility.
More complete C++ runtimes can be bundled with the application using either shared or static versions of STLport or the GNU Standard C++ Library provided with the NDK - see $NDK/docs/CPLUSPLUS-SUPPORT.html for more information.
By default both RTTI (Run-Time Type Information) and exceptions are disabled, but can be enabled with flags in either Application.mk or Android.mk:
APP_CPPFLAGS += -frtti # Enable RTTI in Application.mk LOCAL_CPP_FEATURES += rtti # Enable exceptions in Android.mk APP_CPPFLAGS += -fexceptions # Enable exceptions in Application.mk LOCAL_CPP_FEATURES += exceptions # Enable exceptions in Android.mk
Note that the default system C++ runtime does not have exception or RTTI support - use another of the above mentioned C++ runtimes for that.
Android logging API
Using variadic macros one may write portable logging such as:
The standard zlib compression library may be used by linking with libz.
The dynamic linking functions dlopen(), dlsym(), dlclose() and dlerror() may be used by linking with libdl.
Android support for OpenGL ES, a subset of OpenGL, is exposed both in Java and native API.
All android devices supports version OpenGL ES version 1.0, though some may use a software renderer. Applications which requires (as opposed to may optionally use features from through runtime checks) a higher version must specify so in their AndroidManifest.xml file with the android:glEsVersion attribute on the <uses-feature/> tag, as a 32-bit number with the higher and lower 16 bits representing the major and minor OpenGL version number:
<-- require OpenGL ES version 1.0 (default) --> <uses-feature android:glEsVersion="0x00010000"/> <-- require OpenGL ES version 1.1 --> <uses-feature android:glEsVersion="0x00010001"/> <-- require OpenGL ES version 2.0 --> <uses-feature android:glEsVersion="0x00020000"/> <-- require OpenGL ES version 3.0 --> <uses-feature android:glEsVersion="0x00030000"/>
Note that the Android emulator does not support OpenGL ES version 3.x - a physical device is required for testing applications using that version.
Android bitmap API
EGL (http://www.khronos.org/egl) exposes API to list supported EGL configurations, allocate and release OpenGLES surfaces and swap/flip surfaces for display (eglSwapBuffers).
The OpenSL ES audio API, as well as some Android-specific extensions, is supported since Android version 2.3. The OpenSL ES 1.0.1 specification is bundled with the NDK under $NDK/docs/opensles/OpenSL_ES_Specification_1.0.1.pdf and Android-specific information is available under $NDK/docs/opensles/index.html.
Android native application API
The Android native application API is an umbrella for accessing Android functionality otherwise exposed in java, all contained in a shared library named libandroid.so.
ALooper struct is a native equivalent of the android.os.Looper java class and provides a basic event loop.
Support for reading touch and key input from native code is defined in <android/input.h>, with keycodes available in <android/keycodes.h>. Note that this is only available when using NativeActivity (otherwise the java input API must be used).
The API defined in <android/sensor.h> gives access to accelerometers, magnetic field, gyroscope, light and proximity sensors. See also the Sensors Overview for further information and best practices regarding sensors on Android.
The API for handling android.view.Surface instances from native code, or "window management functions", is described by the below interfaces from <android/rect.h>, <android/native_window.h> and <android/native_window_jni.h>:
The android.app.NativeActivity java class and the corresponding native
struct ANativeActivity defined in <android/native_activity.h> frees applications from having to write java glue code, as well as providing native input event support not available otherwise. See $NDK/docs/NATIVE-ACTIVITY.HTML. Note the bundled utility android_native_app_glue.h and android_native_app_glue.c which implements an event loop in a separate thread receiving events from the main thread using a pipe.