本文共 5317 字,大约阅读时间需要 17 分钟。
Android 1.0的时代,没有提供对于C/C++开发本地Android代码的支持,尽管Android系统本身使用了大量的C++做底层开发。
Android第一次支持本地开发是在Android 1.5版本,对应Android API level 3。这一版本,有了正式的Android NDK的支持,可以通过jni写so库的方式,供Android应用来调用。
Android 1.6增加了对于OpenGL ES 1.x的支持
Android 2.0开始支持OpenGL ES 2.0Android 2.3是一个重要的版本,这一版本增加了完全用C++写本地应用的接口。
我们看看这一版提供了什么API头文件:
直至今天,Android 7.0,API level 24的时代,上面这些仍然构成了我们这系列教程的主要内容。
同时,Android 2.3还开始支持EGL接口。
Android 4.0开始支持OpenMAX AL库。
Android 4.3开始支持Open GL ES 3.0版
Android 6.0开始支持trace库
Android 7.0开始支持Vulkan, Camera, Choreographer和Multinework库,同时对Open GL ES 3.2的支持
首先,写native应用需要一个本地应用的框架的支持。
Java应用需要写一个manifest.xml,我们也入乡随俗需要写一个,我们先看一个NDK sample的例子:因为我们没有写Java代码,所以需要将android:hasCode值设为false.
可以使用NDK中的android_native_app_glue定义的类来对本地API进行封装,我们来看一下,先认识一下后面我们会介绍的几个类:
struct android_app { // The application can place a pointer to its own state object // here if it likes. void* userData; // Fill this in with the function to process main app commands (APP_CMD_*) void (*onAppCmd)(struct android_app* app, int32_t cmd); // Fill this in with the function to process input events. At this point // the event has already been pre-dispatched, and it will be finished upon // return. Return 1 if you have handled the event, 0 for any default // dispatching. int32_t (*onInputEvent)(struct android_app* app, AInputEvent* event); // The ANativeActivity object instance that this app is running in. ANativeActivity* activity; // The current configuration the app is running in. AConfiguration* config; // This is the last instance's saved state, as provided at creation time. // It is NULL if there was no state. You can use this as you need; the // memory will remain around until you call android_app_exec_cmd() for // APP_CMD_RESUME, at which point it will be freed and savedState set to NULL. // These variables should only be changed when processing a APP_CMD_SAVE_STATE, // at which point they will be initialized to NULL and you can malloc your // state and place the information here. In that case the memory will be // freed for you later. void* savedState; size_t savedStateSize; // The ALooper associated with the app's thread. ALooper* looper; // When non-NULL, this is the input queue from which the app will // receive user input events. AInputQueue* inputQueue; // When non-NULL, this is the window surface that the app can draw in. ANativeWindow* window; // Current content rectangle of the window; this is the area where the // window's content should be placed to be seen by the user. ARect contentRect; // Current state of the app's activity. May be either APP_CMD_START, // APP_CMD_RESUME, APP_CMD_PAUSE, or APP_CMD_STOP; see below. int activityState; // This is non-zero when the application's NativeActivity is being // destroyed and waiting for the app thread to complete. int destroyRequested; // ------------------------------------------------- // Below are "private" implementation of the glue code. pthread_mutex_t mutex; pthread_cond_t cond; int msgread; int msgwrite; pthread_t thread; struct android_poll_source cmdPollSource; struct android_poll_source inputPollSource; int running; int stateSaved; int destroyed; int redrawNeeded; AInputQueue* pendingInputQueue; ANativeWindow* pendingWindow; ARect pendingContentRect;};
我们看下这个结构中都用到了什么:
native应用比起Java应用来,跟Android版本的相关性更高一些。
所以,这些API都是根据平台版本号分成不同的目录的。下面介绍的android下面的API,对应于Android源码中的frameworks/native/include/中。在NDK中,以r13b为例,Android 7.0,也就是API 24的头文件位于:android-ndk/r13b/platforms/android-24/arch-arm64/usr/include/中。我们首先通过一张图来看看Android为我们提供了哪些API:主要包括下面的头文件:
android/native_activity.h
android/native_window.h
android/rect.h
android/asset_manager.h
android/asset_manager_jni.h
bitmap.h
configuration.h
input.h
android/looper.h
android/storage_manager.h
android/obb.h
android/sensor.h
转载地址:http://khlfo.baihongyu.com/