Discuz! Board

标题: android NDK的内容分析 [打印本页]

作者: zangcf    时间: 2016-8-14 21:18
标题: android NDK的内容分析
本帖最后由 zangcf 于 2016-8-14 23:13 编辑

一个老版本的内容介绍【NDK】NDK本地API介绍:http://blog.sina.com.cn/s/blog_4119bd830100t0gh.html

原文:android-ndk-r5\docs\STABLE-APIS.html

一些“API的级别”定义。每个级别的AP对应一个给定的Android版本,目前支持:

    android-3 -> 官方Android 1.5  系统映像     (C、C++、Math、Log、Zlib)
    android-4 -> 官方Android 1.6  系统映像     OpenGL ES 1.x)
    android-5 -> 官方Android 2.0  系统映像     OpenGL ES 2.0)
    android-6 -> 官方Android 2.0.1 系统映像
    android-7 -> 官方Android 2.1  系统映像
    android-8 -> 官方Android 2.2  系统映像     (jnigraphics)
    android-9 -> 官方Android 2.3  系统映像     OpenSL ES、android)

请注意,android-6和android-7和android-5相同即他们的本地API是完全一样的
什么级别有什么头文件,在下面路径下可以找到:
   $NDK/platforms/android-<level>/arch-arm/usr/include

二、Android-3 Stable Native APIs:

下面列出的所有API是用于开发NDK代码,可用于运行在Android 1.5系统的及以后版本.

(1)C库:

C库的头文件,在Android 1.5中定义的可用通过他们标准名称找到(/<stdlib.h>,<stdio.h>中,等..).
如果一个头在构建时不存在,可能是在1.5班中中没有实现

编译系统自动链接C库到您的本机模块,您不必将它添加到LOCAL_LDLIBS.

请注意,机器人C库包含的pthread(<pthread.h>)的支持,
所以“LOCAL_LIBS:= - lpthread”是没有必要的。

(2)数学库:

<math.h>是可用的,在构建时.数学库会自动链接到您的本地代码中,
所以不需要指定LOCAL_LDLIBS := -lm

(3)C++库:

C++非常小的一部分API是支持的.对于Android 1.5,仅允许下列头文件:

   <cstddef>
   <new>
   <utility>
   <stl_pair.h>

他们不可能包含所有标准的定义.
值得注意的是,在Android 1.5中,C++异常和RTTI支持是不可用。

C++支持库(-lstdc++)是自动链接到你的本地代码中
所以不需要指定LOCAL_LDLIBS

(4)Android Log:

<android/log.h>包含可用于从您的本机代码中发送各种定义日志的信息。
请您看其内容(build/platforms/android-3/common/include/android/log.h),其中包含如何使用它.

您应该写一个宏来包装他们,以方便使用.

如果你使用它,你的本机模块应该链接/system/lib/liblog.so:

  LOCAL_LDLIBS:= -llog

(5)Zlib压缩库:

<zlib.h><zconf.h>可以使用,可用于使用了zlib压缩库。

如果你使用它,你的本机模块应该链接/system/lib/libz.so:

  LOCAL_LDLIBS := -lz

(6)动态链接库:

<dlfcn.h>可以使用,dlopen()/dlsym()/dlclose()
提供的Android动态链接器功能。您将需要链接/system/lib/libdl.so:

  LOCAL_LDLIBS := -ldl


三. Android-4 Stable Native APIs:

下面列出的所有API是用于开发本机代码,可用于运行在Android 1.6系统的及以后版本.

(1)OpenGL ES 1.x的库:

标准的OpenGL ES的头文件<GLES/gl.h><GLES/glext.h>

如果您使用它们,你的本机模块应该链接到/system/lib/libGLESv1_CM.so,如:

  LOCAL_LDLIBS := - lGLESv1_CM


'1.X'号是指OpenGL ES API的两个版本1.0和1.1。
请注意:

   - OpenGL ES 1.0是支持所有的Android设备
   - OpenGL ES 1.1完全支持相应的图形处理器特定的设备

是因为Android1.0使用相对较少的设备.

开发人员应该查询OpenGL ES版本字符串和扩展名字符串,如果知道当前设备支持哪些东西。
参见glGetString()的说明,以了解如何做到这一点:

    http://www.khronos.org/opengles/sdk/1.1/docs/man/glGetString.xml

此外,开发者必须在manifest.xml中指明<uses-feature>标签,说明应用程序使用
哪些OpenGL ES版本。有关详细信息,请查看下面链接的信息:

http://developer.android.com/gui ... eature-element.html

请注意,目前,本地的头文件和库在 EGL APIs是不可用.
EGL是用于执行平面创建和翻转(而不是渲染).
相应的操作必须要在您的应用程序而不是虚拟机。


四、Android-5 Stable Native APIs:

下面列出的所有API是用于开发本机代码,可用于运行在Android 2.0系统的及以后版本.

(1)OpenGL ES 2.0库:

标准的OpenGL ES 2.0的头文件<GLES2/gl2.h><GLES2/gl2ext.h>
在本地调用中,要包含声明需要执行从本地代码调用的OpenGL ES 2.0渲染
这包括定义顶点和片段着色器的使用的GLSL的语言。

如果您使用它们,你的本机模块应该链接到/system/lib/libGLESv2.so如:

  LOCAL_LDLIBS := -lGLESv2

并非所有设备都支持OpenGL ES 2.0,开发人员应该这样查询
实现的版本和扩展字符串,见上文第三节详情。

请注意,目前,本地的头文件和库在 EGL APIs是不可用.
EGL是用于执行平面创建和翻转(而不是渲染).
相应的操作必须要在您的应用程序而不是虚拟机

重要注意事项:
    Android模拟器不支持OpenGL ES 2.0的硬件.
    如果运行和测试代码,使用这个API需要真实的设备
.
    (意思使用这个OPenGL的库,必须真机运行或调试)


五、Android-8 Stable Native APIs:
下面列出的所有API是用于开发本机代码,可用于运行在Android 2.2系统的及以后版本.
(1)'jnigraphics'库:
这是一个很小的库,展示一个稳定的,基于C语言的,接口,使
本机代码安全地访问Java对象的像素缓冲区的位图.
使用它,在你的源代码中包含<android/bitmap.h>,并链接库jnigraphics:
  LOCAL_LDLIBS + = -ljnigraphics
详细信息,请阅读bitmap.h:
    build/platforms/android-8/arch-arm/usr/include/android/bitmap.h
简单地说,典型的使用应该是这样的:
    1  根据JNI位图句柄,然后使用AndroidBitmap_getInfo()来检索有关信息
       (例如它的宽度/高度/像素格式)
    2  使用AndroidBitmap_lockPixels()来锁定像素缓冲区和指针。
       直到AndroidBitmap_unlockPixels()被调用之前可确保像素
       不被移动???
    3  修改像素缓冲区,本地代码中根据其像素格式,宽度,步幅等
    4  调用AndroidBitmap_unlockPixels()来解锁缓冲区。

六、Android-9 Stable Native APIs:
下面列出的所有API是用于开发本机代码,可用于运行在Android 2.3系统的及以后版本.
(1)OpenSL ES的音频库:
使用它,在你的源代码中包含<SLES/OpenSLES.h><SLES/OpenSLES_Platform.h>.
包含需要从本地端的Android执行音频输入和输出的声明.
注意:尽管目前OpenSL ES的规范使用<OpenSLES.h>,
   目前Khronos的修改了,使用该文件建议包含<SLES/OpenSLES.h>,
      以后使用这个库使用Android的方案
这个API版本也提供Android的特定扩展,详情请参看
<SLES/OpenSLES_Android.h>和<SLES/OpenSLES_AndroidConfiguration.h>
该系统库命名为“libOpenSLES.so”,它在本地代码中实现了音频功能。
在你的模块使用以下链接:
    LOCAL_LDLIBS + = - lOpenSLES

(2)Android的本地应用程序的APIs:
从API 9起,有可能完全写一个Android本机代码应用程序(即没有任何Java).
这并不意味着您的代码不运行在虚拟机,
虽然,大部分的功能在该平台将仍然需要通过JNI访问.
更多的关于这个主题的信息,
请阅读docs/NATIVE-ACTIVITY.html(TODO:写文档)
下面的头文件是新增的本地API:
  <android/native_activity.h> 活动生命周期管理(和入口点)
  <android/looper.h>
  <android/input.h>
  <android/keycodes.h>
  <android/sensor.h>
直接从本地代码监听和传感器的输入事件
  <android/rect.h>
  <android/window.h>
  <android/native_window.h>
  <android/native_window_jni.h>
  窗口管理,包括能够锁定/解锁像素,可在缓冲区中操作.
  <android/configuration.h>
  <android/asset_manager.h>
  <android/storage_manager.h>
  <android/obb.h>

        直接(只读)访问您的.apk文件嵌入资源或
        在不透明的二进制(OBB)文件,允许分配应用程序
        大量的数据在.apk外.
        (对于开发游戏是非常有用的,例如)
               
相应的功能函数在"libandroid.so"库中,
API 9才能使用.要使用它,使用以下配置:
    LOCAL_LDLIBS + = - landroid


作者: zangcf    时间: 2016-8-14 23:18
标题: 一篇英文对于NDK的介绍
http://mobilepearls.com/labs/native-android-api/
::
Mobile PearlsLabsThe Native Android API
The Native Android API

Tip: Do you want to experiment with the Android native API directly on a device using a terminal emulator? InstallTermux, a terminal emulator and Linux enviromnent for Android!
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:
NameIncludeLinking (LOCAL_LDLIBS :=...)Android version
C runtime library (libc)#include ...Automatic1.5
Java Native Interface#include <jni.h>Automatic1.5
Math#include <math.h>Automatic (-lm not needed)1.5
POSIX Threads (pthreads)#include <pthread.h>Automatic (-lpthread not needed)1.5
C++#include ..Automatic (-lstdc++ not needed)1.5
Android logging API#include <android/log.h>-llog1.5
Zlib#include <zconf.h>
#include <zlib.h>-lz1.5
Dynamic linker#include <dlfcn.h>-ldl1.5
OpenGL ES 1.x#include <GLES/gl.h>
#include <GLES/glext.h>-lGLESv1_CM1.6
OpenGL ES 2.0#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>-lGLESv22.0
Android bitmap API#include <android/bitmap.h>-ljnigraphics2.2
EGL#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <EGL/eglplatform.h>-lEGL2.3
OpenSL ES#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Platform.h>
#include <SLES/OpenSLES_Android.h>
#include <SLES/OpenSLES_AndroidConfiguration.h>-lOpenSLES2.3
Android native application APIEvent loop:
#include <android/looper.h>

Input events:
#include <android/input.h>
#include <android/keycodes.h>

Sensor events:
#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>

Assets:
#include <android/asset_manager.h>

Configuration:
#include <android/configuration.h>

OBB files:
#include <android/storage_manager.h>
#include <android/obb.h>

Native activity:
#include <android/native_activity.h>
-landroid2.3
OpenMAX AL#include <OMXAL/OpenMAXAL.h>
#include <OMXAL/OpenMAXAL_Platform.h>
#include <OMXAL/OpenMAXAL_Android.h>-lOpenMAXAL4.0
OpenGL ES 3.0#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>-lGLESv34.3
Link Top
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). Generally, if one header is not there at build time, it's because its implementation is not available.
Link Top
Java Native Interface (JNI)
Android uses the same JNI as standard java. Three resources available online are the Java Native Interface Specification, the Programmer's Guide and Specification as well as Android JNI tips.
Link Top
POSIX threads (pthreads)
The android libc, 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 read/write locks, pthread_cancel(), process-shared mutexes and condition variables as well as other more advanced features. Read the bionicOVERVIEW.txt for more information.
TLS, thread-local storage, is limited to 59 pthread_key_t slots available to applications, lower than the posix minimum of 128.
Link Top
C++
The Android default system C++ runtime is a small C++ subset (e.g. no std::string or std::vector) limited to the following headers: cassert cctype cerrno cfloat climits cmath csetjmp csignal cstddef cstdint cstdio cstdlib cstring ctime cwchar new stl_pair.h typeinfo utility.
More complete C++ runtimes can be bundled with the application using either shared or static versions of STLport or the GNU Standard C++ Library provided with the NDK - see $NDK/docs/CPLUSPLUS-SUPPORT.html for more information.
By default both RTTI (Run-Time Type Information) and exceptions are disabled, but can be enabled with flags in either Application.mk or Android.mk:
APP_CPPFLAGS += -frtti     # Enable RTTI in Application.mkLOCAL_CPP_FEATURES += rtti # Enable exceptions in Android.mkAPP_CPPFLAGS += -fexceptions     # Enable exceptions in Application.mkLOCAL_CPP_FEATURES += exceptions # Enable exceptions in Android.mk
Note that the default system C++ runtime does not have exception or RTTI support - use another of the above mentioned C++ runtimes for that.
Link Top
Android logging API
The native logging API 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:
Link Top
Zlib
The standard zlib compression library may be used by linking with libz.
Link Top
Dynamic linker
The dynamic linking functions dlopen(), dlsym(), dlclose() and dlerror() may be used by linking with libdl.
Link Top
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"/><-- require OpenGL ES version 3.0 --><uses-feature android:glEsVersion="0x00030000"/>
Note that the Android emulator does not support OpenGL ES version 3.x - a physical device is required for testing applications using that version.
Link Top
Android bitmap API
The API for handling android.util.Bitmap instances from native code is defined in <android/bitmap.h>:
Link Top
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).
Link Top
OpenSL ES
The OpenSL ES audio 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/docs/opensles/OpenSL_ES_Specification_1.0.1.pdf and Android-specific information is available under$NDK/docs/opensles/index.html.
Link Top
Android native application API
The Android native application API is an umbrella for accessing Android functionality otherwise exposed in java, all contained in a shared library named libandroid.so.
Link Top
Event loop
The ALooper struct is a native equivalent of the android.os.Looper java class and provides a basic event loop.
Link Top
Input events
Support for reading touch and key input from native code is defined in <android/input.h>, with keycodes available in<android/keycodes.h>. Note that this is only available when using NativeActivity (otherwise the java input API must be used).
Link Top
Sensor events
The API defined in <android/sensor.h> gives access to accelerometers, magnetic field, gyroscope, light and proximity sensors. See also the Sensors Overview for further information and best practices regarding sensors on Android.
Link Top
Window management
The API for handling android.view.Surface instances from native code, or "window management functions", is described by the below interfaces from <android/rect.h>, <android/native_window.h> and<android/native_window_jni.h>:
Link Top
Assets
Files in the assets/ folder can be accessed through AAssetManager, a native counterpart to theandroid.content.res.AssetManager java class described by the <android/asset_manager.h>:
Link Top
Configuration
Native API corresponding to the android.content.res.Configuration java class is available in<android/configuration.h>.
Link Top
OBB files
Native code can access OBB, Opaque Binary Blob files using API in <android/storage_manager.h> and<android/obb.h>.
Link Top
Native activity
The android.app.NativeActivity java class and the corresponding native struct ANativeActivity defined in<android/native_activity.h> frees applications from having to write java glue code, as well as providing native input event support not available otherwise. See $NDK/docs/NATIVE-ACTIVITY.HTML. Note the bundled utilityandroid_native_app_glue.h and android_native_app_glue.c which implements an event loop in a separate thread receiving events from the main thread using a pipe.
TopOpenMAX AL
See $NDK/docs/openmaxal/index.html and the bundled OpenMAX AL 1.0.1 specification for information about using this multimedia API.







欢迎光临 Discuz! Board (http://47.89.242.157:9000/bbs/discuz/) Powered by Discuz! X3.2