NDK r8 in windows

来源:互联网 发布:安卓人脸解锁软件 编辑:程序博客网 时间:2024/04/27 23:09
android 最新 NDK r8 在window下开发环境搭建 安装配置与使用 详细图文讲解,完整实际配置过程记录(原创)
一直想搞NDK开发却一直给其他事情耽搁了,参考了些网上的资料今天终于把环境搭建起来了,把过程记录下来分享给大家。
内容目录:
1.默认基础环境
2.NDK下载与配置
3.安装Cygwin
4.用NDK编译
5.安装CDT插件
6.安装Sequoyah插件
7.JNI编译环境配置

画了一个思维导图让大家一目了然配置过程

 

-----------------------------------------------
一、默认基础环境为已经完成Android开发环境的搭建
本人的环境是 jdk1.6.0_07 、Eclipse3.6(Helios)、Android SDK 、 ADT 可以正常开发android应用。
要先创建android虚拟机,能够跑一个android应用起来再跑ndk的
 
二、Android NDK 下载与配置
下载Android NDK:http://developer.android.com/tools/sdk/ndk/index.html
最近google的网站打开都很慢 有时候还可能打不开,需要有点耐心。
选择Windowsandroid-ndk-r8c-windows.zip233787657 bytes (104MB)
下载后解压缩到你的工作目录,例如:E:\android\android-ndk-r8,
samples文件夹下面包含12个实例开发演示项目,是学习的好资源。
 
三、安装Cygwin
 
C/C++的编译环境需要Linux的gcc,所以在Windows环境下用Cygwin来模拟Linux
下载地址:http://cygwin.com/install.html
随便点击一个 setup.exe 的连接即可下载,这个只是个下载的引导程序,实际内容到下一步才下载,有0.97GB需要的时间比较长的。
1.运行setup.exe程序,直接点击Next进入下一步。
 
2.选择安装方式。第一次可以采用Direct Connection在线下载安装,如有现成的离线包,可以选择离线安装(Install from Local Directory)。
 
3.选择安装目录。比如D:\Cygwin,注意此目录是指Cygwin这个虚拟机的安装目录,不是下载文件暂存目录,也不是NDK的目录。
 
4.设置本地包暂存路径。暂存目录默认是放到setup.exe的同级目录下 我的是http%3a%2f%2fmirrors.163.com%2fcygwin%2f,可以备份下来。

 

 

注:第一次安装会弹出个警告框,不要管他,点击确认就可以了。 

 
5.设置网络连接方式。选第一个即可。
 
6.选择下载站点地址,直接选第一个163的站点就可以了,这个我下载的速度最高可以达到185k。
 
7.出现加载安装项后,点击Devel-Default,变成Devel-Install,其他设置都不用动,下一步下载。

 

 
8.等待下载完成,大约需要一个小时左右。下载完成后再点击下一步安装,安装也需要很长时间。

 

 
9.运行安装目录(D:\Cygwin)下的“Cygwin.bat”,输入:“cygcheck -c cygwin”命令,会打印出当前Cygwin的版本和运行状态,如果status是ok的话,则cygwin运行正常。
 
10.输入:“make –v”和,“gcc –v”命令如果检测成功,会有make和gcc相关版本信息打印出来。
11.设置NDK路径,在windows的系统环境变量中添加NDK的路径。
使用“/cygdrive/e/android/android-ndk-r8”这种Linux风格路径,如果使用Windows下的“E:\android\android-ndk-r8”,Cygwin在编译时会发出警告。
 
12.运行Cygwin命令行,可以直接使用$ndk环境变量,不用每次都手动输入目录
 
 
四、使用NDK编译程序:
 
1)编译NDK提供的sample程序hello-jni(E:\android\android-ndk-r8\samples\hello-jni)。
1.运行Cygwin,输入“cd $ndk/samples/hello-jni/”,进入到“hello-jni”工程目录。
2.输入“$ndk/ndk-build”编译。ndk-build是调用ndk的编译程序。

  

3.用ls命令查看libs/armeabi/下是否生成了so文件,有libhello-jni.so说明ndk运行正常。
$ ls libs/armeabi/
gdb.setup  gdbserver  libhello-jni.so
也可以到E:\android\android-ndk-r8\samples\hello-jni\libs\armeabi目录下看有没有生成的.so文件。

 

 

2)导入NDK的hello-jni示例到Eclipse中
1.在Eclipse中新建一个Android工程HelloJni。
在Create Android Project时勾选“Create project from existing source”,Location中填E:\android\android-ndk-r8\samples\hello-jni

 

 

2.直接以Android Aplication运行com.example.hellojni.HelloJni项目。要先有.so文件才能运行起来。
 
 
五、在Eclipse中集成C/C++开发环境CDT
CDT插件可以在一个工程中同时开发基于C/C++的Native代码和基于Java的代码,加上Sequoyah插件可以一次编译两部分代码。
下载地址:http://www.eclipse.org/cdt/downloads.php
不同eclipse版本要下载对应的包,否则安装不了。
Help-->About Eclipse 下可以看到自己用的版本,我的是 Version: Helios Service Release 2 即Helios版本
http://www.eclipse.org/downloads/download.php?file=/tools/cdt/releases/helios/dist/cdt-master-7.0.2.zip
cdt-master-7.0.2.zip:这个是CDT的离线安装包。(由于文件比较大推荐使用离线包,7.0.2的54.8M,8.1.1的103MB)
安装:Eclipse -> Help -> Install New Software,点击add。
Name:CDT_版本。

Location:点击Archive,定位到下载的“cdt-master-7.0.2.zip”文件。

如果Location的下面出现“Duplicate location”错误,请到Window -> preferences -> Install/Update -> Avaliable Software Site中找到remove掉。
如果出现缺少eclipse包说明你下载的版本和eclipse版本不匹配,重新下载匹配的重新安装就可以了。

 

另外需要重启eclipse后才生效。
安装完成重启后,在Eclispe中新建一个项目,如果出现了C/C++项目,则表明CDT插件安装成功了。

 

六、安装Sequoyah插件(必须先安装CDT之后才能安装这个,可以直接安装最新版2.0的)
Sequoyah插件用于设置Android工程对Native开发的支持。
官方网址:http://www.eclipse.org/sequoyah/downloads/
打开上面网址就可以看到具体的安装说明了
Location:http://download.eclipse.org/sequoyah/updates/2.0/
 
For Helios SR2 Sequoyah version: http://download.eclipse.org/sequoyah/updates/1.0.2/ 
注:只是安装1.0.2的没有出来“本机开发”的配置所以没法用,本人重新安装了2.0的可以了,这个应该直接安装最新版2.0的就可以了。
在安装界面不要勾选“Group items by category”复选框,选中出现了列表为空(There are no categorized items)的情况。
 
在“window –> preferences ->Android -> 本机开发”中添加NDK的路径。

 

这个插件安装后也需要重启eclipse的
重启后右键之前建立的“HelloJni”项目,在“Android Tools”选项中包含“Add Native Support…”选项即成功。

 

七、JNI编译环境配置
现在修改“/HelloJni/jni/hello-jni.c”文件,动态链接库libhello-jni.so文件不会被重新编译生成。
需要给JNI项目添加编译配置和依赖库后才行。
1.转换工程:新建项目,选择“C/C++”下的“Convert to a C/C++ Project(Adds C/C++ Nature)”。进入“下一步”。
2.选中你刚才建的“HelloJni”工程,下面左边选“Makefile project”右边选“Cygwin GCC”,点击Finish创建项目。
 

 

3.在“HelloJni”工程上右键,选择“属性”。配置“C/C++ Build”:点击“C/C++ Build”,在右边的“Builder Settings”中去掉默认勾选的“Use default build command”复选框。

设置Build command为“bash E:\android\android-ndk-r8\ndk-build”。

 

4.配置依赖库:C/C++ General ->  Paths and Symbols:在Includes下add新的GNU,C依赖路径。
“HelloJni”工程需要“E:\android\android-ndk-r8\platforms\android-8\arch-arm\usr\include”即可,以后根据不同项目选择不同的依赖库。 

 

5.将“/HelloJni/jni/hello-jni.c”中的字符串“Hello from JNI !”,改为“Hello JNI from ZDZ!”

 

6.运行后在模拟器上输出的字符串为“Hello JNI from ZDZ!”即说明配置成功。 

 

到现在你可以在一个工程中同时开发基于C/C++的Native代码和基于Java的代码,开发NDK应用和开发普通android应用感觉不到什么差别了。

 


附上官方文档

Getting Started with the NDK


Once you've installed the NDK successfully, take a few minutes to read the documentation included in the NDK. You can find the documentation in the <ndk>/docs/ directory. In particular, please read the OVERVIEW.HTML document completely, so that you understand the intent of the NDK and how to use it.

If you used a previous version of the NDK, take a moment to review the list of NDK changes in the CHANGES.HTML document.

Here's the general outline of how you work with the NDK tools:

  1. Place your native sources under <project>/jni/...
  2. Create <project>/jni/Android.mk to describe your native sources to the NDK build system
  3. Optional: Create <project>/jni/Application.mk.
  4. Build your native code by running the 'ndk-build' script from your project's directory. It is located in the top-level NDK directory:
    cd <project><ndk>/ndk-build

    The build tools copy the stripped, shared libraries needed by your application to the proper location in the application's project directory.

  5. Finally, compile your application using the SDK tools in the usual way. The SDK build tools will package the shared libraries in the application's deployable .apk file.

For complete information on all of the steps listed above, please see the documentation included with the NDK package.

Using the NDK

The Android framework provides two ways to use native code:

  • Write your application using the Android framework and use JNI to access the APIs provided by the Android NDK. This technique allows you to take advantage of the convenience of the Android framework, but still allows you to write native code when necessary. If you use this approach, your application must target specific, minimum Android platform levels, see Android platform compatibility for more information.
  • Write a native activity, which allows you to implement the lifecycle callbacks in native code. The Android SDK provides the NativeActivity class, which is a convenience class that notifies your native code of any activity lifecycle callbacks (onCreate()onPause()onResume(), etc). You can implement the callbacks in your native code to handle these events when they occur. Applications that use native activities must be run on Android 2.3 (API Level 9) or later.

    You cannot access features such as Services and Content Providers natively, so if you want to use them or any other framework API, you can still write JNI code to do so.

Contents of the NDK


The NDK contains the APIs, documentation, and sample applications that help you write your native code. Specifically:

  • A set of tools and build files used to generate native code libraries from C and C++ sources
  • A way to embed the corresponding native libraries into an application package file (.apk) that can be deployed on Android devices
  • A set of native system headers and libraries that will be supported in all future versions of the Android platform, starting from Android 1.5. Applications that use native activities must be run on Android 2.3 or later.
  • Documentation, samples, and tutorials

The latest release of the NDK supports the following instruction sets:

  • ARMv5TE, including Thumb-1 instructions (see docs/CPU-ARCH-ABIS.html for more information)
  • ARMv7-A, including Thumb-2 and VFPv3-D16 instructions, with optional support for NEON/VFPv3-D32 instructions (see docs/CPU-ARM-NEON.html for more information)
  • x86 instructions (see docs/CPU-X86.html for more information)
  • MIPS instructions (see docs/CPU-MIPS.html for more information)

ARMv5TE machine code will run on all ARM-based Android devices. ARMv7-A will run only on devices such as the Verizon Droid or Google Nexus One that have a compatible CPU. The main difference between the two instruction sets is that ARMv7-A supports hardware FPU, Thumb-2, and NEON instructions. You can target either or both of the instruction sets — ARMv5TE is the default, but switching to ARMv7-A is as easy as adding a single line to the application's Application.mkfile, without needing to change anything else in the file. You can also build for both architectures at the same time and have everything stored in the final .apk. Complete information is provided in the CPU-ARCH-ABIS.HTML in the NDK package.

The NDK provides stable headers for libc (the C library), libm (the Math library), OpenGL ES (3D graphics library), the JNI interface, and other libraries, as listed in the Development tools section.

Development tools

The NDK includes a set of cross-toolchains (compilers, linkers, etc..) that can generate native ARM binaries on Linux, OS X, and Windows (with Cygwin) platforms.

It provides a set of system headers for stable native APIs that are guaranteed to be supported in all later releases of the platform:

  • libc (C library) headers
  • libm (math library) headers
  • JNI interface headers
  • libz (Zlib compression) headers
  • liblog (Android logging) header
  • OpenGL ES 1.1 and OpenGL ES 2.0 (3D graphics libraries) headers
  • libjnigraphics (Pixel buffer access) header (for Android 2.2 and above).
  • A Minimal set of headers for C++ support
  • OpenSL ES native audio libraries
  • Android native application APIS

The NDK also provides a build system that lets you work efficiently with your sources, without having to handle the toolchain/platform/CPU/ABI details. You create very short build files to describe which sources to compile and which Android application will use them — the build system compiles the sources and places the shared libraries directly in your application project.

Important: With the exception of the libraries listed above, native system libraries in the Android platform are not stable and may change in future platform versions. Your applications should only make use of the stable native system libraries provided in this NDK.

Documentation

The NDK package includes a set of documentation that describes the capabilities of the NDK and how to use it to create shared libraries for your Android applications. In this release, the documentation is provided only in the downloadable NDK package. You can find the documentation in the <ndk>/docs/ directory. Included are these files (partial listing):

  • INSTALL.HTML — describes how to install the NDK and configure it for your host system
  • OVERVIEW.HTML — provides an overview of the NDK capabilities and usage
  • ANDROID-MK.HTML — describes the use of the Android.mk file, which defines the native sources you want to compile
  • APPLICATION-MK.HTML — describes the use of the Application.mk file, which describes the native sources required by your Android application
  • CPLUSPLUS-SUPPORT.HTML — describes the C++ support provided in the Android NDK
  • CPU-ARCH-ABIS.HTML — a description of supported CPU architectures and how to target them.
  • CPU-FEATURES.HTML — a description of the cpufeatures static library that lets your application code detect the target device's CPU family and the optional features at runtime.
  • CHANGES.HTML — a complete list of changes to the NDK across all releases.
  • DEVELOPMENT.HTML — describes how to modify the NDK and generate release packages for it
  • HOWTO.HTML — information about common tasks associated with NDK development
  • IMPORT-MODULE.HTML — describes how to share and reuse modules
  • LICENSES.HTML — information about the various open source licenses that govern the Android NDK
  • NATIVE-ACTIVITY.HTML — describes how to implement native activities
  • NDK-BUILD.HTML — describes the usage of the ndk-build script
  • NDK-GDB.HTML — describes how to use the native code debugger
  • PREBUILTS.HTML — information about how shared and static prebuilt libraries work
  • STANDALONE-TOOLCHAIN.HTML — describes how to use Android NDK toolchain as a standalone compiler (still in beta).
  • SYSTEM-ISSUES.HTML — known issues in the Android system images that you should be aware of, if you are developing using the NDK.
  • STABLE-APIS.HTML — a complete list of the stable APIs exposed by headers in the NDK.

Additionally, the package includes detailed information about the "bionic" C library provided with the Android platform that you should be aware of, if you are developing using the NDK. You can find the documentation in the<ndk>/docs/system/libc/ directory:

  • OVERVIEW.HTML — provides an overview of the "bionic" C library and the features it offers.

Sample apps

The NDK includes sample applications that illustrate how to use native code in your Android applications:

  • hello-jni — a simple application that loads a string from a native method implemented in a shared library and then displays it in the application UI.
  • two-libs — a simple application that loads a shared library dynamically and calls a native method provided by the library. In this case, the method is implemented in a static library imported by the shared library.
  • san-angeles — a simple application that renders 3D graphics through the native OpenGL ES APIs, while managing activity lifecycle with a GLSurfaceView object.
  • hello-gl2 — a simple application that renders a triangle using OpenGL ES 2.0 vertex and fragment shaders.
  • hello-neon — a simple application that shows how to use the cpufeatures library to check CPU capabilities at runtime, then use NEON intrinsics if supported by the CPU. Specifically, the application implements two versions of a tiny benchmark for a FIR filter loop, a C version and a NEON-optimized version for devices that support it.
  • bitmap-plasma — a simple application that demonstrates how to access the pixel buffers of Android Bitmap objects from native code, and uses this to generate an old-school "plasma" effect.
  • native-activity — a simple application that demonstrates how to use the native-app-glue static library to create a native activity
  • native-plasma — a version of bitmap-plasma implemented with a native activity.

For each sample, the NDK includes the corresponding C source code and the necessary Android.mk and Application.mk files. There are located under <ndk>/samples/<name>/ and their source code can be found under<ndk>/samples/<name>/jni/.

You can build the shared libraries for the sample apps by going into <ndk>/samples/<name>/ then calling the ndk-buildcommand. The generated shared libraries will be located under <ndk>/samples/<name>/libs/armeabi/ for (ARMv5TE machine code) and/or <ndk>/samples/<name>/libs/armeabi-v7a/ for (ARMv7 machine code).

Next, build the sample Android applications that use the shared libraries:

  • If you are developing in Eclipse with ADT, use the New Project Wizard to create a new Android project for each sample, using the "Import from Existing Source" option and importing the source from <ndk>/samples/<name>/. Then, set up an AVD, if necessary, and build/run the application in the emulator.
  • If you are developing with Ant, use the android tool to create the build file for each of the sample projects at<ndk>/samples/<name>/. Then set up an AVD, if necessary, build your project in the usual way, and run it in the emulator.

For more information about developing with the Android SDK tools and what you need to do to create, build, and run your applications, see the Overview section for developing on Android.

Exploring the hello-jni Sample

The hello-jni sample is a simple demonstration on how to use JNI from an Android application. The HelloJni activity receives a string from a simple C function and displays it in a TextView.

The main components of the sample include:

  • The familiar basic structure of an Android application (an AndroidManifest.xml file, a src/ and res directories, and a main activity)
  • jni/ directory that includes the implemented source file for the native code as well as the Android.mk file
  • tests/ directory that contains unit test code.
  1. Create a new project in Eclipse from the existing sample source or use the android tool to update the project so it generates a build.xml file that you can use to build the sample.
    • In Eclipse:
      1. Click File > New Android Project...
      2. Select the Create project from existing source radio button.
      3. Select any API level above Android 1.5.
      4. In the Location field, click Browse... and select the <ndk-root>/samples/hello-jni directory.
      5. Click Finish.
    • On the command line:
      1. Change to the <ndk-root>/samples/hello-jni directory.
      2. Run the following command to generate a build.xml file:
        android update project -p . -s
  2. Compile the native code using the ndk-build command.
    cd <ndk-root>/samples/hello-jni<ndk_root>/ndk-build
  3. Build and install the application as you would a normal Android application. If you are using Eclipse, run the application to build and install it on a device. If you are using Ant, run the following commands from the project directory:
    ant debugadb install bin/HelloJni-debug.apk

When you run the application on the device, the string Hello JNI should appear on your device. You can explore the rest of the samples that are located in the <ndk-root>/samples directory for more examples on how to use the JNI.

Exploring the native-activity Sample Application

The native-activity sample provided with the Android NDK demonstrates how to use the android_native_app_glue static library. This static library makes creating a native activity easier by providing you with an implementation that handles your callbacks in another thread, so you do not have to worry about them blocking your main UI thread. The main parts of the sample are described below:

  • The familiar basic structure of an Android application (an AndroidManifest.xml file, a src/ and res directories). The AndroidManifest.xml declares that the application is native and specifies the .so file of the native activity. SeeNativeActivity for the source or see the <ndk_root>/platforms/samples/native-activity/AndroidManifest.xml file.
  • jni/ directory contains the native activity, main.c, which uses the android_native_app_glue.h interface to implement the activity. The Android.mk that describes the native module to the build system also exists here.

To build this sample application:

  1. Create a new project in Eclipse from the existing sample source or use the android tool to update the project so it generates a build.xml file that you can use to build the sample.
    • In Eclipse:
      1. Click File > New Android Project...
      2. Select the Create project from existing source radio button.
      3. Select any API level above Android 2.3.
      4. In the Location field, click Browse... and select the <ndk-root>/samples/native-activity directory.
      5. Click Finish.
    • On the command line:
      1. Change to the <ndk-root>/samples/native-activity directory.
      2. Run the following command to generate a build.xml file:
        android update project -p . -s
  2. Compile the native code using the ndk-build command.
    cd <ndk-root>/platforms/samples/android-9/samples/native-activity<ndk_root>/ndk-build
  3. Build and install the application as you would a normal Android application. If you are using Eclipse, run the application to build and install it on a device. If you are using Ant, run the following commands in the project directory, then run the application on the device:
    ant debugadb install bin/NativeActivity-debug.apk


0 0
原创粉丝点击