Overview
Android exposes a native API to applications through the Native Development Kit (NDK). It is described in the $NDK/docs/STABLE-APIS.html document and summarized below:
| Name | Include | Linking (LOCAL_LDLIBS :=...) | Android version |
| C runtime library (libc) | #include ... | Automatic | 1.5 |
| Java Native Interface | #include <jni.h> | Automatic | 1.5 |
| System properties | #include <sys/system_properties.h> | Automatic | 1.5 |
| Math | #include <math.h> | Automatic (-lm not needed) | 1.5 |
| POSIX Threads (pthreads) | #include <pthread.h> | Automatic (-lpthread not needed) | 1.5 |
| C++ subset | #include <cstddef> #include <new> #include <stl_pair> #include <utility> |
Automatic (-lstdc++ not needed) | 1.5 |
| Android logging API | #include <android/log.h> | -llog | 1.5 |
| Zlib | #include <zconf.h> #include <zlib.h> |
-lz | 1.5 |
| Dynamic linker | #include <dlfcn.h> | -ldl | 1.5 |
| OpenGL ES 1.x | #include <GLES/gl.h> #include <GLES/glext.h> |
-lGLESv1_CM | 1.6 |
| OpenGL ES 2.0 | #include <GLES2/gl2.h> #include <GLES2/gl2ext.h> |
-lGLESv2 | 2.0 |
| Android bitmap API | #include <android/bitmap.h> | -ljnigraphics | 2.2 |
| EGL | #include <EGL/egl.h> #include <EGL/eglext.h> #include <EGL/eglplatform.h> |
-lEGL | 2.3 |
| OpenSL ES | #include <SLES/OpenSLES.h> #include <SLES/OpenSLES_Platform.h> #include <SLES/OpenSLES_Android.h> #include <SLES/OpenSLES_AndroidConfiguration.h> |
-lOpenSLES | 2.3 |
| Android native application API | #include <android/native_activity.h> #include <android/looper.h> #include <android/input.h> #include <android/keycodes.h> #include <android/sensor.h> Window/Surface management: #include <android/rect.h> #include <android/window.h> #include <android/native_window.h> #include <android/native_window_jni.h> #include <android/configuration.h> #include <android/asset_manager.h> #include <android/storage_manager.h> #include <android/obb.h> |
-landroid | 2.3 |
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). Basically, if one header is not there at build time, it's because its implementation is not available. An overview is provided in the libc/docs/OVERVIEW.TXT file.
Java Native Interface (JNI)
Android uses the same JNI as standard java. Two great resources are the official Programmer's Guide and Specification as well as Android JNI tips.
System properties
Including the header file <sys/system_properties.h> gives access to native functions for accessing system properties like System#getProperty(String) does in java:
POSIX threads (pthreads)
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 pthread_cancel() and read/write locks. Read the Bionic OVERVIEW.txt for more information.
C++ subset
Android officially only supports a small C++ subset limited to the <cstddef>, <new>, <stl_pair> and <utility> headers. Exceptions and RTTI however works from Android version 1.6 onwards. For the standard C++ library a customized version of the NDK is available at crystax.net.
Android logging API
The API for logging from native code is defined in <android/log.h> and matches that of the java android.util.Log class:
Using variadic macros one may write portable logging such as:
Zlib
The standard zlib compression library may be used by linking with libz.
Dynamic linker
The dynamic linking functions dlopen(), dlsym(), dlclose() and dlerror() may be used by linking with libdl.
EGL
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).
OpenGL ES
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"/>
Note that the Android emulator does not support OpenGL ES version 2.x - a physical device is required for testing applications using that version.
Android bitmap API
The API for handling android.util.Bitmap instances from native code is defined in <android/bitmap.h>:
OpenSL ES
The OpenSL ES 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/opensles/OpenSL_ES_Specification_1.0.1.pdf and Android-specific information is available under $NDK/opensles/index.html.
Android native application API
The Android native application API is an umbrella for accessing Android functionality otherwise exposed in java.
Looper
Window management
The API for handling android.view.Surface instances from native code, or "window management functions", are summarized by the below interfaces from rect.h, native_window.h and native_window_jni.h:
Native activity
Using android.app.NativeActivity one may avoid having to write java glue code. See $NDK/docs/NATIVE-ACTIVITY.HTML. Note the bundled static library defined by $NDK/sources/android/native_app_glue/android_native_app_glue.h and implemented in $NDK/sources/android/native_app_glue/android_native_app_glue.c which implements an event loop in a separate thread.