二、NDK实例的实现 对于Windows环境下NDK的开发,如果使用的NDK是r7之前的版本,必须要安装Cygwin才能使用NDK; 从r7开始,Google的提供了一个ndk-build.cmd的脚本,可以直接用它编译,而不需要使用Cygwin了。 只需要为Eclipse Android工程添加一个Builder,就能让Eclipse自动编译NDK。
本文是讲述NDK-r8e下的实现实例。 下面是在windows下为NDK配置自动编译的builder的过程(对于Linux,只需将ndk-build.cmd修改为ndk-build就可以了)。 (1)先下载安装NDK 下载地址:http://developer.android.com/sdk/ndk/index.html 下载后解压缩就可以用了。 (2)新建工程和jni文件夹 打开Eclipse,新建或导入一个Android工程(我的取名为NdkTest),在工程目录下新建jni文件夹; 该jni文件夹就用来保存NDK需要编译的文件代码等。 (3)新建并配置一个Builder: (a)Project->Properties->Builders->New,新建一个Builder。 (b)在弹出的【Choose configuration type】对话框,选择【Program】,点击【OK】: (c)在弹出的【Edit Configuration】对话框中,配置选项卡【Main】。 在“Name“中输入新builders的名称(我取名为Ndk_Builder)。 在“Location”中输入nkd-build.cmd的路径。 (我的是E:\adt-bundle-windows-x86-20130522\ndk-r8e\ndk-build.cmd, 根据各自的ndk路径设置,也可以点击“Browser File System…”来选取这个路径)。 在“Working Diretcoty”中输入项目工程的路径${workspace_loc:/project} (我的是${workspace_loc:/NdkTest},也可以点击“Browse Workspace”来选取本工程目录)。 (d)【Edit Configuration】对话框中,配置选项卡【Refresh】。 勾选“Refresh resources upon completion”, 勾选“The entire workspace”, 勾选“Recuresively include sub-folders”。 (e)【Edit Configuration】对话框中,配置选项卡【Build options】。 勾选“After a “Clean””, 勾选“During manual builds”, 勾选“During auto builds”, 勾选“Specify working set of relevant resources”。 点击“Specify Resources…” 勾选本工程的“jni“目录,点击”finish“。 点击“OK“,完成配置。到这里Eclipse就能自动调用NDK编译jni目录下的C/C++代码了。
(4)新建JniClient.java 在工程中新建一个JniClient.java(为了调用C/C++代码),保存在MainActivity.java目录下;其内容如下:
[java] view plain copy package com.example.ndktest; public class JniClient { static public native String HelloWorld(); }
(5)生成JniClinet.class文件 用cmd命令定位到JniClient.java所在目录,输入“javac JniClient.java“后回车,生成JniClinet.class文件。 (如果是用的Eclipse建的工程,在bin\classes\com\example\ndktest目录下就已经有JniClinet.class文件了)。 (6)生成C++头文件 将JniClinet.class拷贝到bin\classes\com\example\ndktest目录,将cmd命令定位到bin\classes目录, 输入”javah com.example.ndktest.JniClient“后回车,在bin\classes目录下就生成了C++头文件了。 com_example_ndktest_JniClient.h的文件内容如下:
[cpp] view plain copy /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_example_ndktest_JniClient */ #ifndef _Included_com_example_ndktest_JniClient #define _Included_com_example_ndktest_JniClient #ifdef __cplusplus extern "C" { #endif /* * Class: com_example_ndktest_JniClient * Method: HelloWorld * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_example_ndktest_JniClient_HelloWorld (JNIEnv *, jclass); #ifdef __cplusplus } #endif #endif
(7)新建一个Android.mk文件 在jni目录下新建一个Android.mk文件,其内容如下(详细的语法以后再另外解释):
[plain] view plain copy LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := NdkTest LOCAL_SRC_FILES := com_example_ndktest_JniClient.c include $(BUILD_SHARED_LIBRARY)
(8)创建C++源文件 将com_example_ndktest_JniClient.h拷贝到本工程的jni目录下; 然后新建一个com_example_ndktest_JniClient.c文件,用来实现头文件中函数,其内容如下:
[cpp] view plain copy #include "com_example_ndktest_JniClient.h" #include <stdlib.h> #include <stdio.h> #ifdef __cplusplus extern "C" { #endif JNIEXPORT jstring JNICALL Java_com_example_ndktest_JniClient_HelloWorld (JNIEnv *env, jclass arg) { jstring str = (*env)->NewStringUTF(env, "HelloWorld from JNI !"); return str; } #ifdef __cplusplus } #endif 编辑并保存后,可以看到obj/local/armeabi目录下将自动生成libNdkTest.so库。 (9)用java调用c函数 修改MainActivity.java,完成对JniClient.java中函数的调用: [java] view plain copy package com.example.ndktest; import android.os.Bundle; import android.app.Activity; //remove for NdkTest //import android.view.Menu; //add for NdkTest import android.widget.TextView; public class MainActivity extends Activity { //add for NdkTest static { System.loadLibrary("NdkTest"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //remove for NdkTest //setContentView(R.layout.activity_main); //add for NdkTest String str = JniClient.HelloWorld(); TextView tv = new TextView(this); tv.setText(str); setContentView(tv); } //remove for NdkTest /*@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }*/ } (10)运行测试 运行,在模拟器中可以看到界面输出来自com_example_ndktest_JniClient.c 文件中的“HelloWorld from JNI ! “。 参考: http://www.cnblogs.com/chenjiajin/archive/2012/04/12/2444188.html