Android NDK开发轻松入门

fmms 12年前
     <h2><strong>简介</strong><span style="font-family:Times New Roman,serif;"><strong>:</strong></span></h2>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">AndroidNDK</span>是能使<span style="font-family:Times New Roman,serif;">Android</span>应用开发者把从<span style="font-family:Times New Roman,serif;">c/c++</span>编译而来的本地代码嵌入到应用包中的一系列工具的组合。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="color:#800000;"><strong><em>注意</em><span style="font-family:Times New Roman,serif;"><em>:</em></span></strong></span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="color:#800000;"><span style="font-family:Times New Roman,serif;"><em>AndroidNDK</em></span><em>只能用于</em><span style="font-family:Times New Roman,serif;"><em>Android1.5</em></span><em>及以上版本中。</em></span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <h3><span style="font-family:Times New Roman,serif;">I. Android NDK </span>目标<span style="font-family:Times New Roman,serif;">:</span></h3>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">Android</span>虚拟机允许你的应用在源码中通过<span style="font-family:Times New Roman,serif;">JNI</span>调用本地代码中实现的方法们。概括起来,这表示:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">-</span>你的应用的源中要声明一个或多个方法,这些方法前面需有<span style="font-family:'Times New Roman',serif;">'native'</span>关键字,这表明它们被本地代码实现。如:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">nativebyte[] loadFile(String filePath);</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">-</span>你必须提供本地的共享库,库中包含这些方法的实现。这个库将会打包你的应用<span style="font-family:'Times New Roman',serif;">.apk</span>中。这个库的命名必须符合标准的<span style="font-family:'Times New Roman',serif;">Unix</span>命名规则,也就是:<span style="font-family:'Times New Roman',serif;">lib<something>.so</span>这种形式。并且还要包含一个标准的<span style="font-family:'Times New Roman',serif;">JNI</span>入口。例如:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">libFileLoader.so</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">-</span>你的应用必须显式的加载本地库。例如,要在应用启动时加载,只需简单地在代码中增加如下语句:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">static{</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">System.loadLibrary("FileLoader");</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">}</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="color:#660000;" lang="zh-CN"><em>注意写库的名字时你不需要使用</em></span><span style="font-family:'Times New Roman',serif;color:#660000;"><em>'lib'</em></span><span style="color:#660000;" lang="zh-CN"><em>前缀和</em></span><span style="font-family:'Times New Roman',serif;color:#660000;"><em>'.so'</em></span><span style="color:#660000;" lang="zh-CN"><em>后缀。</em></span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">AndroidNDK</span>只是<span style="font-family:'Times New Roman',serif;">AndroidSDK</span>的一个组件,它帮助你:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">-</span>产生<span style="font-family:'Times New Roman',serif;">JNI</span>兼容的共享库,此库能运行于跑在<span style="font-family:'Times New Roman',serif;">ARMCPU</span>上的<span style="font-family:'Times New Roman',serif;">Android</span><span style="font-family:'Times New Roman',serif;">1.5</span>及以上系统。</p>    <p style="margin-bottom:0cm;"><span style="font-family:'Times New Roman',serif;">-</span>把共享库考贝到你的应用的项目中的合适的位置,并最终把它们添加到你的<span style="font-family:'Times New Roman',serif;">.apks</span>中。</p>    <p style="margin-bottom:0cm;"><span style="font-family:'Times New Roman',serif;">-</span>在后续的<span style="font-family:'Times New Roman',serif;">NDK</span>版本中,我们打算提供通过远程<span style="font-family:'Times New Roman',serif;">gdb</span>调试本地代码的工具并尽量多地提供源码和符号信息。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">AndroidNDK</span>还提供了:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">-</span>一系列跨平台的编译工具<span style="font-family:'Times New Roman',serif;">(</span>编译器,链接器,等等<span style="font-family:'Times New Roman',serif;">)</span>,它们可以在<span style="font-family:'Times New Roman',serif;">Linux,OS X </span>和 <span style="font-family:'Times New Roman',serif;">Windows (</span>使用<span style="font-family:'Times New Roman',serif;">Cygwin)</span>上产生<span style="font-family:'Times New Roman',serif;">ARM</span>上的二进制程序码。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">-</span>一系列的头文件,对应于<span style="font-family:'Times New Roman',serif;">Android</span>系统所支持的稳定的本地<span style="font-family:'Times New Roman',serif;">API</span>们,这保证了你所用的接口在后续所有版本中依旧被支持。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="background-color:#ffffff;"><span style="color:#660000;"><strong>重点注意<span style="font-family:'Times New Roman',serif;">:</span></strong></span></span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="background-color:#ffffff;"><span style="color:#660000;"><strong>记住大多数系统库并没有固定死并且可能在未来的版本中会发生重大变化,甚至被删除,但是”稳定的<span style="font-family:'Times New Roman',serif;">API</span>们”是不变的。</strong></span></span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">-</span>一个构建系统,使得开发者只需写少量编译文件描述哪些源文件需被构建即可。构建系统可处理所有编译工具链<span style="font-family:Times New Roman,serif;">/</span>平台<span style="font-family:Times New Roman,serif;">/CPU/ABI</span>细节。还有,后续的<span style="font-family:Times New Roman,serif;">NDK</span>更新中可以在添加更多的编译工具链,平台,系统接口的同时不需开发者的项目构建文件发生变化。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <h3><span style="font-family:Times New Roman,serif;">II.Android NDK </span>不想做的<span style="font-family:Times New Roman,serif;">:</span></h3>    <p style="margin-bottom:0cm;" lang="zh-CN">使用<span style="font-family:'Times New Roman',serif;">NDK</span>编写运行于<span style="font-family:'Times New Roman',serif;">android</span>设备上的一般本地代码这种方式并不好。你的应用依然应该使用<span style="font-family:'Times New Roman',serif;">Java</span>编写,正确处理<span style="font-family:'Times New Roman',serif;">Android</span>系统的事件来避免<span style="font-family:'Times New Roman',serif;">"</span>应用无反应<span style="font-family:'Times New Roman',serif;">"</span>对话框的出现或处理应用的生命周期。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="color:#660000;"><strong><em>注意,无论如何还是可以通过本地代码编写精巧的应用的,这个应用上只是带有一个很小的用于启动</em><span style="font-family:Times New Roman,serif;"><em>/</em></span><em>关闭应用的包装</em>。</strong></span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN">对<span style="font-family:Times New Roman,serif;">JNI</span>的深刻理解是非常必要的。因为在这个环境中的很多操作需要开发者做出一些特殊的处理,而它们在典型的一般代码(<span style="font-family:Times New Roman,serif;">java</span>)中是不需要的。这些包括:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">-</span>不能通过指针直接使用<span style="font-family:'Times New Roman',serif;">VM</span>对象的内容。例如,你无法安全地获取一个指向<span style="font-family:'Times New Roman',serif;">java</span>字符串对象的<span style="font-family:'Times New Roman',serif;">16</span>位字符数组的指针然后在一个循环中枚举它的每一项。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">-</span>当本地代码想要在不同的<span style="font-family:'Times New Roman',serif;">JNI</span>调用之间保存<span style="font-family:'Times New Roman',serif;">VM</span>对象的句柄时,需要对句柄进行明确的引用管理。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">NDK</span>仅仅为被<span style="font-family:Times New Roman,serif;">android</span>系统支持的本地<span style="font-family:Times New Roman,serif;">API</span>和库中很少的一些提供了头文件。然而一个典型的<span style="font-family:Times New Roman,serif;">Android</span>系统映像包含了很多本地共享库,但这些应被看做是实现的细节,这些实现可能在平台更新或发布时产生彻底的变化。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">如果一个<span style="font-family:'Times New Roman',serif;">Android</span>系统的库没有被<span style="font-family:'Times New Roman',serif;">NDK</span>的头文件明确支持,那么应用不应依赖于它。否则可能在下一次系统升级后出现杯具。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">选中的系统库将逐渐地被添加到稳定版的<span style="font-family:'Times New Roman',serif;">NDKAPI</span>中。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <h3><span style="font-family:Times New Roman,serif;">III.NDK </span>开发实践<span style="font-family:Times New Roman,serif;">:</span></h3>    <p style="margin-bottom:0cm;" lang="zh-CN">下面是一个对使用<span style="font-family:'Times New Roman',serif;">NDK</span>开发本地代码过程的粗略的概览<span style="font-family:'Times New Roman',serif;">:</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">1/</span>将你的本地代码源码放在路径<span style="font-family:'Times New Roman',serif;">$PROJECT/jni/</span>下。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">2/</span>写一个文件:<span style="font-family:'Times New Roman',serif;">$PROJECT/jni/Android.mk</span>,来描述你的源文件们。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">3/</span>可选的:在文件<span style="font-family:'Times New Roman',serif;">$PROJECT/jni/Application.mk</span>中描述你的项目的更多细节。尽管你不需要从头写,但你可以处理多<span style="font-family:'Times New Roman',serif;">CPU</span>问题以及改写编译<span style="font-family:'Times New Roman',serif;">/</span>链接选项。<span style="font-family:'Times New Roman',serif;">(</span>更多细节请观<span style="font-family:'Times New Roman',serif;">docs/APPLICATION-MK.html )</span>。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">4/</span>在你的项目路径下或其任何子路径下运行<span style="font-family:'Times New Roman',serif;">"$NDK/ndk-build"</span>来编译你的本地代码。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN">最后一步将在编译成功时复制你的应用所需共享库到你项目的跟路径下。然后你可以用跟以前一样的方式产生最终的<span style="font-family:Times New Roman,serif;">.apk</span>文件。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN">下面,是一些更多的细节:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;"><br /> </span></p>    <h4><span style="font-family:'Times New Roman',serif;">III.1/</span>配置<span style="font-family:Times New Roman,serif;">NDK:</span></h4>    <p style="margin-bottom:0cm;" lang="zh-CN">以前的发行版需要你运行<span style="font-family:Times New Roman,serif;">'build/host-setup.sh'</span>脚本来配置你的<span style="font-family:Times New Roman,serif;">NDK</span>。但是这一步从第<span style="font-family:Times New Roman,serif;">4</span>版<span style="font-family:Times New Roman,serif;">(NDK r4)</span>开始被移除了。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <h4><span style="font-family:Times New Roman,serif;">III.2/</span>放置 <span style="font-family:Times New Roman,serif;">C </span>和 <span style="font-family:Times New Roman,serif;">C++</span>源码:</h4>    <p style="margin-bottom:0cm;" lang="zh-CN">将你的本地源码放在以下路径下:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">$PROJECT/jni/</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">$PROJECT</span>对应你的<span style="font-family:'Times New Roman',serif;">android</span>应用项目的路径。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">你可以随意组织<span style="font-family:'Times New Roman',serif;">jni</span>下的内容,路径名和路径结构不会影响到最终产生的应用包。所以你无需使用类似于<span style="font-family:'Times New Roman',serif;">com.<mycompany>.<myproject></span>这样的名字。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">注意 <span style="font-family:'Times New Roman',serif;">C</span>和<span style="font-family:'Times New Roman',serif;">C++</span>源码都是被支持的。默认<span style="font-family:'Times New Roman',serif;">C++</span>文件扩展名是<span style="font-family:'Times New Roman',serif;">'.cpp'</span>,但是其它的扩展名也可以被处理。<span style="font-family:'Times New Roman',serif;">(</span>见文档<span style="font-family:'Times New Roman',serif;">docs/ANDROID-MK.html).</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN">也可以通过调整文件<span style="font-family:'Times New Roman',serif;">Android.mk</span>的内容,把你的源码存放在其它路径下。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <h4><span style="font-family:Times New Roman,serif;">III.3/</span>写一个<span style="font-family:Times New Roman,serif;">Android.mk</span>构建脚本:</h4>    <p style="margin-bottom:0cm;" lang="zh-CN">一个 <span style="font-family:Times New Roman,serif;">Android.mk</span>文件是一个很小的构建脚本。你编写它以描述你给<span style="font-family:Times New Roman,serif;">NDK</span>构建器的源码文件们。它的语法在<span style="font-family:Times New Roman,serif;">docs/ANDROID-MK.html</span>中有详细描述。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">NDK</span>简单的将你的原文件组织到多个<span style="font-family:Times New Roman,serif;">"</span>模块<span style="font-family:Times New Roman,serif;">"</span>中,每个模块可以是以下的任意一种:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">-</span>一个静态库</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">-</span>一个共享库</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN">你可以在一个<span style="font-family:Times New Roman,serif;">Android.mk</span>中定义多个模块或写多个<span style="font-family:Times New Roman,serif;">Android.mk</span>文件,每个文件只对应一个模块。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">注意,一个<span style="font-family:'Times New Roman',serif;">Android.mk</span>文件可能被构建系统分析多遍,所以不要假设某个变量没有被定义。默认下,<span style="font-family:'Times New Roman',serif;">NDK</span>将寻找下面的构建脚本:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">$PROJECT/jni/Android.mk</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN">如果你想在子路径下定义<span style="font-family:'Times New Roman',serif;">Android.mk</span>文件,你应该在顶层的<span style="font-family:'Times New Roman',serif;">Android.mk</span>中包含它们。有个函数可以做到这个功能:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">include$(call all-subdir-makefiles)</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN">这将会包含当前构建路径的所有子路径下的<span style="font-family:'Times New Roman',serif;">Android.mk</span>文件们。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <h4><span style="font-family:Times New Roman,serif;">III.4/</span>写一个<span style="font-family:Times New Roman,serif;">Application.mk</span>构建文件<span style="font-family:Times New Roman,serif;">(</span>可选<span style="font-family:Times New Roman,serif;">):</span></h4>    <div>     <span style="font-family:Times New Roman,serif;"><br /> </span>    </div>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">Android.mk</span>描述你要构建的模块们,而<span style="font-family:Times New Roman,serif;">Application.mk</span>文件描述你的应用自身。看文档<span style="font-family:Times New Roman,serif;">docs/APPLICATION-MK.html</span>来了解这个文件允许你做什么。这个文件主要包含:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">-</span>你的应用所需要模块的准确列表。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">-</span>产生的机器码所对应的<span style="font-family:'Times New Roman',serif;">CPU</span>架构。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">-</span>可选的信息,像你要构建<span style="font-family:'Times New Roman',serif;">release</span>还是<span style="font-family:'Times New Roman',serif;">debug</span>,特殊的<span style="font-family:'Times New Roman',serif;">C</span>或 <span style="font-family:'Times New Roman',serif;">C++</span>编译参数以及其它需要应用到所有模块的构建选项。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN">这个文件是可选的:默认情况下,<span style="font-family:'Times New Roman',serif;">NDK</span>将构建在<span style="font-family:'Times New Roman',serif;">Android.mk</span>中列出的所有模块的并且默认面向<span style="font-family:'Times New Roman',serif;">CPUABI (armeabi).</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN">有两种方法使用一个<span style="font-family:'Times New Roman',serif;">Application.mk:</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">-</span>将它放在<span style="font-family:'Times New Roman',serif;">$PROJECT/jni/Application.mk</span>位置,那么它会被<span style="font-family:'Times New Roman',serif;">'ndk-build'</span>脚本自动使用。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">-</span>将它放在<span style="font-family:'Times New Roman',serif;">$NDK/apps/<name>/Application.mk</span>,<span style="font-family:'Times New Roman',serif;">$NDK</span>代表你的<span style="font-family:'Times New Roman',serif;">NDK</span>安装路径。之后,在<span style="font-family:'Times New Roman',serif;">NDK</span>路径下运行<span style="font-family:'Times New Roman',serif;">"make APP=<name>"</span>。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">这是在<span style="font-family:'Times New Roman',serif;">NDKr4</span>之前的办法。出于兼容的原因,当前还是被支持的,但是我们强烈鼓励你使用第一种方法。因为它简单并且不用改动<span style="font-family:'Times New Roman',serif;">NDK</span>安装路径下的路径树结构。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <h4><span style="font-family:'Times New Roman',serif;">III.5/</span>调用<span style="font-family:'Times New Roman',serif;">NDK</span>构建系统:</h4>    <div>     <br />    </div>    <p style="margin-bottom:0cm;" lang="zh-CN">使用<span style="font-family:Times New Roman,serif;">NDK</span>构建机器代码的最佳方式是使用<span style="font-family:Times New Roman,serif;">'ndk-build'</span>脚本。你也可以使用另一个老的方式--依赖于创建<span style="font-family:Times New Roman,serif;">'$NDK/apps'</span>子目录的方式。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">不论哪种方式,编译成功后,那些编译出的“裸体”(不带有调试信息的)二进制模块都将被复制到你应用项目所在的路径下<span style="font-family:'Times New Roman',serif;">(</span>注意非“裸体”的二进制模块会保留以提供调试能力。没有必要把非“裸体”模块复制到设备上去<span style="font-family:'Times New Roman',serif;">)</span>。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">1:</span>使用<span style="font-family:Times New Roman,serif;">'ndk-build'</span>命令:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">'ndk-build'</span>脚本可以在<span style="font-family:Times New Roman,serif;">NDK</span>安装所在的顶级目录下找到<span style="font-family:Times New Roman,serif;">,</span>可以直接你的应用项目的目录<span style="font-family:Times New Roman,serif;">(</span>也就是你的<span style="font-family:Times New Roman,serif;">AndroidManifest.xml</span>所在的目录<span style="font-family:Times New Roman,serif;">)</span>或任何子目录下调用。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">例如<span style="font-family:Times New Roman,serif;">:</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">cd$PROJECT</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">$NDK/ndk-build</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN">这将启动<span style="font-family:Times New Roman,serif;">NDK</span>构建脚本,脚本将自动探测你的开发系统和应用项目文件来决定构建什么东西。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">例如:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">ndk-build</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">ndk-build clean --></span>清空所编译出的二进制文件们。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">ndk-build -B V=1 --></span>强制完全重新编译,并显示命令</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN">默认下,脚本希望看到一个可选的<span style="font-family:Times New Roman,serif;">$PROJECT/jni/Application.mk</span>和一个必须的<span style="font-family:Times New Roman,serif;">$PROJECT/jni/Android.mk</span>。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">成功后,会把产生的二进制模块<span style="font-family:'Times New Roman',serif;">(</span>即共享库<span style="font-family:'Times New Roman',serif;">)</span>复制到你的项目树中的合适位置。你可以在之后使用’<span style="font-family:'Times New Roman',serif;">ant’</span>命令或<span style="font-family:'Times New Roman',serif;">ADP</span>插件来重建完整的应用包。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">关于这个脚本的更完整的说明和可用的选项,见<span style="font-family:'Times New Roman',serif;">docs/NDK-BUILD.html</span>。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">2:</span>使用<span style="font-family:'Times New Roman',serif;">$NDK/apps/<name>/Application.mk:</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN">这种构建方式是<span style="font-family:'Times New Roman',serif;">NDKr4</span>以及正前版本的唯一选择,当前还支持,仅仅是为了兼容的原因。我们强烈建议你麻溜地移植为使用<span style="font-family:'Times New Roman',serif;">'ndk-build'</span>命令的方式,因为我们可能很快就把这种方式抛弃掉。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">用它需要这样做:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">1.</span>在你的<span style="font-family:'Times New Roman',serif;">NDK</span>安装目录下<span style="font-family:'Times New Roman',serif;">(</span>不是你的应用路径<span style="font-family:'Times New Roman',serif;">)</span>创建一个子目录,叫:<span style="font-family:'Times New Roman',serif;">$NDK/apps/<name>/</span>。<span style="font-family:'Times New Roman',serif;"><name></span>是一个任意的名字用来向<span style="font-family:'Times New Roman',serif;">NDK</span>构建系统描述你的应用<span style="font-family:'Times New Roman',serif;">(</span>不能有空格<span style="font-family:'Times New Roman',serif;">)</span>。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">2.</span>写一个<span style="font-family:'Times New Roman',serif;">$4NDK/apps/<name>/Application.mk</span>,需在里面定义一个<span style="font-family:'Times New Roman',serif;">APP_PROJECT_PATH</span>指向你的应用项目目录。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">3.</span>在命令行中,进入到<span style="font-family:'Times New Roman',serif;">NDK</span>安装路径,然后调用顶层的<span style="font-family:'Times New Roman',serif;">GNUMakefile,</span>如下<span style="font-family:'Times New Roman',serif;">:</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">cd$NDK</span></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">makeAPP=<name></span></p>    <p style="margin-bottom:0cm;" lang="zh-CN">结果同第一种方法一样,除了一些中间产物放在<span style="font-family:'Times New Roman',serif;">$NDK/out/apps/<name>/</span>下之外。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">IV.</span>重建你的应用包:</p>    <p style="margin-bottom:0cm;" lang="zh-CN">使用<span style="font-family:Times New Roman,serif;">NDK</span>产生二进制文件后,你需是用通常的方式重建的你应用包文件<span style="font-family:Times New Roman,serif;">(.apk)</span>,即使用<span style="font-family:Times New Roman,serif;">'ant'</span>命令或<span style="font-family:Times New Roman,serif;">ADTEclipse</span>插件。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">你的新<span style="font-family:'Times New Roman',serif;">.apk</span>中将被嵌入共享库文件,然后在安装到设备时被系统自动分离出来。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:Times New Roman,serif;">V.</span>对调试的支持:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">NDK</span>提供了一个辅助脚本,叫做<span style="font-family:'Times New Roman',serif;">'ndk-gdb'</span>,来十分轻松地为你的应用启动一个调试会话。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN">本地调试只能在运行<span style="font-family:Times New Roman,serif;">Android2.2</span>或更高系统的设备上执行。并且不需要特殊用户权限。</p>    <p style="margin-bottom:0cm;" lang="zh-CN">更多信息,请对<span style="font-family:'Times New Roman',serif;">docs/NDK-GDB.html</span>。简要来说,本地调试分以下几步:</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">1.</span>确定你的应用是可调试的<span style="font-family:'Times New Roman',serif;">(</span>在<span style="font-family:'Times New Roman',serif;">AndroidManifest.xml</span>中设置<span style="font-family:'Times New Roman',serif;">android:debuggable</span>为<span style="font-family:'Times New Roman',serif;">"true")</span>。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">2.</span>使用<span style="font-family:'Times New Roman',serif;">'ndk-build'</span>构建你的共享库,然后构建你的应用然后安装到设备或模拟器上。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">3.</span>运行你的应用。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"><span style="font-family:'Times New Roman',serif;">4.</span>在你的应用项目目录下运行<span style="font-family:'Times New Roman',serif;">'ndk-gdb'</span>。</p>    <p style="margin-bottom:0cm;" lang="zh-CN"></p>    <p style="margin-bottom:0cm;" lang="zh-CN">你将看到<span style="font-family:Times New Roman,serif;">gdb</span>提示符出现。然后扒着<span style="font-family:Times New Roman,serif;">GDB</span>手册笨笨的调吧。</p>