您的位置:首页 > 移动开发 > Android开发

Android NDK 之 NDK 预生库支持

2014-04-07 17:17 603 查看
NDK Prebuilt library support:NDK 预生库支持:-----------------------------Android NDK r5 introduced support for prebuilt libraries (shared and static), i.e. the ability to include and use, in your applications, prebuilt version of libraries.Android NDK r5 已引入对预生成库的支持(共享库和静态库),也就是,在你的应用程序中包含和使用库的预生成版本。This feature can be useful for two things:该功能对两种情况是有用的:1/ You want to distribute your own libraries to third-party NDK developerswithout distributing your sources.你想要对第三方 NDK 开发者发布你自己的库而不发行你的源文件。2/ You want to use a prebuilt version of your own libraries to speed upyour build.你想要使用一个你自己的库的预生版本使你的生成加快速度。This document explains how this support works.本文档说明如何这个支持如何工作。I. Declaring a prebuilt library module:I. 声明一个预生成库模块:---------------------------------------Each prebuilt library must be declared as a *single* independent module tothe build system. 对每个预生成库必须作为单个的独立的模块向生成系统声明。Here is a trivial example where we assume that the file"libfoo.so" is located in the same directory than the Android.mk below:这里是一个小小的例子我们假设 libfoo.so 文件是和下面的 Android.mk 文件位于相同目录:LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := foo-prebuiltLOCAL_SRC_FILES := libfoo.soinclude $(PREBUILT_SHARED_LIBRARY)Notice that, to declare such a module, you really only need the following:注意,对于声明这样的模块,你实际仅需要如下步骤:1. Give the module a name (here 'foo-prebuilt'). 给出一个模块名(这是 foo-prebuilt)。This does not need to correspond to the name of the prebuilt library itself.  这不需要对应预生成库它自己的名称。2. Assign to LOCAL_SRC_FILES the path to the prebuilt library you are providing. 指定 LOCAL_SRC_FILES 变量值为你提供的预生成库路径。注:一个静态或共享库只能做为一个预生库来提供!即 LOCAL_SRC_FILES 变量值只能为一个!As usual, the path is relative to your LOCAL_PATH.像平常一样,该路径是相对于你的 LOCAL_PATH 变量值的路径。IMPORTANT: 重点:You *must* ensure that the prebuilt library corresponds to the target ABI you are using. 你必须确保该预生成库对应你正在使用的目标 ABI 。More on this later.更多信息在后面。3. Include PREBUILT_SHARED_LIBRARY, instead of BUILD_SHARED_LIBRARY, if you are providing a shared library. 如果你是提供一个共享库的话,包含 PREBUILT_SHARED_LIBRARY 替代 BUILD_SHARED_LIBRARY 。For static ones, use PREBUILT_STATIC_LIBRARY.对于静态库,包含 PREBUILT_STATIC_LIBRARY 。 A prebuilt module does not build anything. 一个预生模块不生成任何东西。However, a copy of your prebuilt shared library will be copied into $PROJECT/obj/local, and another will be copied and stripped into $PROJECT/libs/<abi>.然而,一个你的预生成共享库的拷贝将复制进 $PROJECT/obj/local 目录下,并且另一个将复制并裁剪进 $PROJECT/libs/<abi> 目录下。II. Referencing the prebuilt library in other modules:II. 在其它模块中引用预生成库:------------------------------------------------------Simply list your prebuilt module's name in the LOCAL_STATIC_LIBRARIES orLOCAL_SHARED_LIBRARIES declaration in the Android.mk of any module thatdepends on them.在任何依赖预生成库的模块的 Android.mk 文件中,简单地列出你的预生成模块名作为 LOCAL_STATIC_LIBRARIES 或 LOCAL_SHARED_LIBRARIES 的变量值。For example, a naive example of a module using libfoo.so would be:例如,一个模块使用 libfoo.so 的单纯例子将是: include $(CLEAR_VARS) LOCAL_MODULE := foo-user LOCAL_SRC_FILES := foo-user.c LOCAL_SHARED_LIBRARY := foo-prebuilt include $(BUILD_SHARED_LIBRARY)III. Exporting headers for prebuilt libraries:III. 为预生成库导出头文件:----------------------------------------------The example above was called 'naive' because, in practice, the code in foo-user.c is going to depend on specific declarations that are normally found in a header file distributed with the prebuilt library (e.g. "foo.h").上面的例子被称之单纯是因为在实际中用 foo-user.c 代码将要依赖具体的声明,通常找到在随预生成库一起发布的头文件中。(例如:foo.h)In other words, foo-user.c is going to have a line like:换句话说,foo-user.c 将要有像如下一行: #include <foo.h>And you need to provide the header and its include path to the compilerwhen building the foo-user module.并且你需要在生成 foo-user 模块时提供头文件和它所在的路径给编译器。A simple way to deal with that is to use exports in the prebuilt module definition. 一个简单处理方法是在预生成模块定义中使用导出。For example, assuming that a file "foo.h" is located underthe 'include' directory relative to the prebuilt module, we can write:例如,假设一个 foo.h 文件是位于相对于预生成模块的 include 目录下,我们可以写成:include $(CLEAR_VARS)LOCAL_MODULE := foo-prebuiltLOCAL_SRC_FILES := libfoo.soLOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/includeinclude $(PREBUILT_SHARED_LIBRARY)The LOCAL_EXPORT_C_INCLUDES definition here ensures that any module thatdepends on the prebuilt one will have its LOCAL_C_INCLUDES automaticallyprepended with the path to the prebuilt's include directory, and will thusbe able to find headers inside that.LOCAL_EXPORT_C_INCLUDES 定义在这里,确保了任何模块依赖预生成模块将自动进行它的 LOCAL_C_INCLUDES 作为预生包含目录的前置,且将因此有能力在这里面找到头文件。IV. Debugging prebuilt binaries:IV. 调试预生成二进制文件:--------------------------------We recommend you to provide prebuilt shared libraries that contain debug symbols. 我们推荐你提供包含调试符号的预生成共享库。The version that is installed into $PROJECT/libs/<abi>/ is alwaysstripped by the NDK build system, but the debug version will be used fordebugging purposes with ndk-gdb.安装进 $PROJECT/libs/<abi>/ 目录中的版本总是由 NDK 生成系统裁剪过的,但是调试版本将被 ndk-gdb 用于调试目的。V. ABI Selection of prebuilt binaries:V. 预生成二进制文件的 ABI 选择:--------------------------------------As said previously, it is crucial to provide a prebuilt shared librarythat is compatible with the targetted ABI during the build. 如前面所述,在生成期间提供一个兼容目标 ABI 的预生成共享库是至关重要的。To do that,check for the value of TARGET_ARCH_ABI, its value will be:要做到这一点,检查 TARGET_ARCH_ABI 的值,它的值将是:armeabi => when targetting ARMv5TE or higher CPUs 当把 ARMv5TE 或更高版本 CPU 作为目标时armeabi-v7a => when targetting ARMv7 or higher CPUs 当把 ARMv7 或更高版本 CPU 作为目标时x86 => when targetting x86 CPUs 当把 x86 CPU 作为目标时Note that armeabi-v7a systems can run armeabi binaries just fine.注意 armeabi-v7a 系统可以很好的运行 armeabi 二进制文件。Here's an example where we provide two versions of a prebuilt libraryand select which one to copy based on the target ABI:这里是一个例子,我们提供一个预生成库的两个版本并基于目标 ABI 来选择复制哪一个: include $(CLEAR_VARS) LOCAL_MODULE := foo-prebuilt LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libfoo.so LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include include $(PREBUILT_SHARED_LIBRARY)Here, we assume that the prebuilt libraries to copy are under the following directory hierarchy:这里,我们假设预生成库复制是在如下目录层次: Android.mk --> the file above 上述文件 armeabi/libfoo.so --> the armeabi prebuilt shared library armeabi 预生成共享库 armeabi-v7a/libfoo.so --> the armeabi-v7a prebuilt shared library armeabi-v7a 预生成库 include/foo.h --> the exported header file 导出的头文件NOTE: 注意:Remember that you don't need to provide an armeabi-v7a prebuilt library, since an armeabi one can easily run on the corresponding devices.记住你不需要提供一个 armeabi-v7a 预生成库,因为一个 armeabi 可以顺利地运行在相应的设备上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ndk 预编译