In this chapter we will cover the following recipes:
Thanks to the Android native application APIs, it is possible to write an Android application with pure native code since Android API level 9 (Android 2.3, Gingerbread). That is, not a single line of Java code is needed. The Android native APIs are defined in several header files under the <NDK root>/platforms/android-<API level>/arch-arm/usr/include/android/
folder. Based on the features provided by the functions defined in these header files, they can be grouped as follows:
native_activity.h
looper.h
rect.h
window.h
native_window.h
native_window_jni.h
input.h
keycodes.h
sensor.h
configuration.h
asset_manager.h
asset_manager_jni.h
storage_manager.h
obb.h
In addition, Android NDK also provides a static library named native app glue to help create and manage native activities. The source code of this library can be found under the sources/android/native_app_glue/
directory.
In this chapter, we will first introduce the creation of a native activity with the simple callback model provided by native_acitivity.h
, and the more complicated but flexible two-threaded model enabled by the native app glue library. We will then discuss window management at Android NDK, where we will draw something on the screen from the native code. Input events handling and sensor accessing are introduced next. Lastly, we will introduce asset management, which manages the files under the assets
folder of our project. Note that the APIs covered in this chapter can be used to get rid of the Java code completely, but we don't have to do so. The Managing assets at Android NDK recipe provides an example of using the asset management API in a mixed-code Android project.
Before we start, it is important to keep in mind that although no Java code is needed in a native activity, the Android application still runs on Dalvik VM, and a lot of Android platform features are accessed through JNI. The Android native application API just hides the Java world for us.