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