海洋生态文明建设的网站名,廊坊网站建设电话,广东网站建设多少钱,登录腾讯邮箱企业邮箱入口这几天一直在研究JNI的开发过程#xff0c;顺便把NDK环境搭建一起总结下。在windows环境下开发jni需要c/c编译器的支持#xff0c;网络上我看很多人使用cygwin。呵呵我不是很喜欢使用它#xff0c;感觉安装起来挺麻烦的。我使用GNUStep#xff0c;下载地址http://www.gnust…这几天一直在研究JNI的开发过程顺便把NDK环境搭建一起总结下。在windows环境下开发jni需要c/c编译器的支持网络上我看很多人使用cygwin。呵呵我不是很喜欢使用它感觉安装起来挺麻烦的。我使用GNUStep下载地址http://www.gnustep.org/experience/Windows.html。下载安装后验证是否成功。打开GNUstep-Shell输入make -v 和 gcc -v命令如图所示。配置ndk环境变量gnustep是模拟linux的环境的打开gnustep的安装目录下的G:\softinstall\GNUstep\GNUstep\GNUstep.conf文件添加以下内容NDK/g/softinstall/Android/android-ndk-r8bexportNDK说明如果不知道ndk目录在linux下应该是在哪里你可以打开gnustep的命令窗口输入mount就可以找到对应的盘符。验证环境变量如下图。 以上就配置成功了。下载进入正题啦。Jni的开发步骤。打开eclipse新建工程名为testJni。在activity中添加以下代码packagecom.xzw.jni;importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.widget.TextView;importandroid.support.v4.app.NavUtils;/**** author XuZhiwei (xuzhiweigmail.com)* sinahttp://weibo.com/xzw1989** Create at 2012-8-30 上午10:49:45*/publicclassMainActivityextendsActivity {OverridepublicvoidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}publicnativeString hello();static{System.loadLibrary(testJni);}}编译后的文件在bin目录下通过javah命令生成c/c的文件头。如下图会在项目目录下生成jni/com_xzw_jni_TestJni.h。头文件代码如下/* DO NOT EDIT THIS FILE - it is machine generated */#include /* Header for class com_xzw_jni_TestJni */#ifndef _Included_com_xzw_jni_TestJni#define _Included_com_xzw_jni_TestJni#ifdef __cplusplusexternC{#endif/** Class: com_xzw_jni_TestJni* Method: hello* Signature: ()Ljava/lang/String;*/JNIEXPORT jstring JNICALL Java_com_xzw_jni_TestJni_hello(JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif根据头文件编写c代码#include #include jstringJava_com_xzw_jni_TestJni_hello(JNIEnv* env, jobject thiz){return(*env)-NewStringUTF(env,哈哈完成自动化编译 !);}接下来编写 Android.mk,该文件可以直接从NDK的samples下的hello-jni的jni文件下直接靠过来改改就可以了。也贴下代码哈。# Copyright (C) 2009 The AndroidOpenSource Project## Licensed under the Apache License, Version 2.0 (the License);# you may notuse this fileexceptincompliancewiththe License.# You may obtain a copy ofthe Licenseat## http://www.apache.org/licenses/LICENSE-2.0## Unless required byapplicable laworagreedtoinwriting, software# distributed under the License isdistributedonanAS ISBASIS,# WITHOUT WARRANTIES ORCONDITIONSOFANYKIND, either expressorimplied.# See the License forthe specific language governing permissionsand# limitations under the License.#LOCAL_PATH : $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE : testJniLOCAL_SRC_FILES : testJni.cinclude $(BUILD_SHARED_LIBRARY)其中你只需要该LOCAL_MODULE和LOCAL_SRC_FILES就可以了。说明:LOCAL_MODULE是描述模块的用来给java调用的模块名会生成对应的libtestJni.soLOCAL_SRC_FILES就是源文件啦多个文件空格隔开即可。接下来我们要开始编译生成so文件咯。打开gnustep的命令窗口进入到项目底下输入$NDK/ndk-build命令即可自动生成libs/armeabi/libtestJni.so文件。接下来就是java调用来。直接上代码packagecom.xzw.jni;importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.widget.TextView;importandroid.support.v4.app.NavUtils;/**** author XuZhiwei (xuzhiweigmail.com)* sinahttp://weibo.com/xzw1989** Create at 2012-8-30 上午10:49:45*/publicclassTestJniextendsActivity {OverridepublicvoidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);TextView tv newTextView(this);tv.setText(hello());setContentView(tv);}publicnativeString hello();static{System.loadLibrary(testJni);}}运行结果如下