NDK中包含以下這組穩定的本地API的頭文件:
·libc(C庫)頭文件
·libm(math庫)頭文件
·JNI接口頭文件
·libz(Zlib壓縮)頭文件
·liblog(Android日誌)頭文件
·OpenGL ES 1.1和OpenGL ES 2.0(3D圖形庫)頭文件
·Libjnigraphics(像素緩衝區訪問)頭文件(可用於Android 2.2和更新版本)
·最小的C++支持頭文件集合
·OpenSL ES本地音頻庫
·Android本地應用API
警告:除了上面列出的庫,Android平台中的本地系統庫也是不穩定的,在以後的平台版本中可能會發生變化。應用只應該使用NDK提供的穩定的本地系統庫。
有些庫文件,如libc和libm,在編譯時是自動引用的,因此只需要在源文件的include中引用。但是,有些庫不會自動引用,因此需要在編譯文件Android.mk中具體聲明。
下面是一個Android.mk示例文件,它導入了cpufeatures模塊,會給我們提供之前whatAmI示例中所缺乏的信息:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := sample LOCAL_SRC_FILES := sample.c LOCAL_LDLIBS := -llog # Here we reference the cpufeatures module LOCAL_STATIC_LIBRARIES := cpufeatures include $(BUILD_SHARED_LIBRARY) # Here we import the cpufeatures modules $(call import-module,cpufeatures)
下面這段源代碼(擴展之前章節所示的whatAmI功能)使用了新包含進來的cpufeatures模塊:
// Include the cpu-features module #include <cpu-features.h> #include <jni.h> #include <android/log.h> #define LOGINFO(x...) __android_log_print(ANDROID_LOG_INFO,"SampleJNI",x) jstring Java_com_oreilly_demo_android_pa_ndkdemo_SampleActivityWithNativeMethods_whatAmI( JNIEnv* env, jobject thisobject) { LOGINFO("SampleJNI","Sample Info Log Output"); // -- Here we use the cpufeatures -- // uint64_t cpu_features; if (android_getCpuFamily != ANDROID_CPU_FAMILY_ARM) { return (*env)->NewStringUTF(env, "Not ARM"); } cpu_features = android_getCpuFeatures; if ((cpu_features & ANDROID_CPU_ARM_FEATURE_ARMv7) != 0) { return (*env)->NewStringUTF(env, "ARMv7"); } else if ((cpu_features & ANDROID_CPU_ARM_FEATURE_VFPv3) != 0) { return (*env)->NewStringUTF(env, "ARM w VFPv3 support"); } else if ((cpu_features & ANDROID_CPU_ARM_FEATURE_NEON) != 0) { return (*env)->NewStringUTF(env, "ARM w NEON support"); } // -- End cpufeatures usage -- // return (*env)->NewStringUTF(env, "Unknown"); }