java 用jni调用so全过程

http://xuzhiwei.blog.51cto.com/978424/976839

 这几天一直在研究JNI的开发过程,顺便把NDK环境搭建一起总结下。在windows环境下开发jni需要c/c++编译器的支持,网络上我看很多人使用cygwin。呵呵我不是很喜欢使用它,感觉安装起来挺麻烦的。我使用GNUStep,下载地址http://www.gnustep.org/experience/Windows.html

下载安装后,验证是否成功。打开GNUstep->Shell,输入make -v 和 gcc -v命令,如图所示。

  

下载NDK,地址http://developer.android.com/tools/sdk/ndk/index.html。下载完后解压即可。

 配置ndk环境变量,gnustep是模拟linux的环境的,打开gnustep的安装目录下的G:\softinstall\GNUstep\GNUstep\GNUstep.conf文件,添加以下内容:

 

NDK=/g/softinstall/Android/android-ndk-r8b

export=NDK

说明如果不知道ndk目录在linux下应该是在哪里,你可以打开gnustep的命令窗口,输入mount,就可以找到对应的盘符。

验证环境变量,如下图。 

以上就配置成功了。

 

下载进入正题啦。Jni的开发步骤。

打开eclipse,新建工程名为testJni。在activity中添加以下代码

  

  1. package com.xzw.jni; 
  2.  
  3. import android.os.Bundle; 
  4. import android.app.Activity; 
  5. import android.view.Menu; 
  6. import android.view.MenuItem; 
  7. import android.widget.TextView; 
  8. import android.support.v4.app.NavUtils; 
  9. /** 
  10.  *  
  11.  * @author XuZhiwei (xuzhiwei@gmail.com) 
  12.  * sina:http://weibo.com/xzw1989 
  13.  *  
  14.  * Create at 2012-8-30 上午10:49:45 
  15.  */ 
  16. public class MainActivity extends Activity {  
  17.     @Override 
  18.     public void onCreate(Bundle savedInstanceState) { 
  19.         super.onCreate(savedInstanceState);  
  20.     } 
  21.  
  22.     public native String hello(); 
  23.     
  24.     static
  25.         System.loadLibrary("testJni"); 
  26.     } 
  27.      
编译后的文件在bin目录下,通过javah命令生成c/c++的文件头。如下图  
 

会在项目目录下生成jni/com_xzw_jni_TestJni.h。

头文件代码如下:

  1. /* DO NOT EDIT THIS FILE - it is machine generated */ 
  2. #include <jni.h> 
  3. /* Header for class com_xzw_jni_TestJni */ 
  4.  
  5. #ifndef _Included_com_xzw_jni_TestJni 
  6. #define _Included_com_xzw_jni_TestJni 
  7. #ifdef __cplusplus 
  8. extern "C" { 
  9. #endif 
  10. /* 
  11.  * Class:     com_xzw_jni_TestJni 
  12.  * Method:    hello 
  13.  * Signature: ()Ljava/lang/String; 
  14.  */ 
  15. JNIEXPORT jstring JNICALL Java_com_xzw_jni_TestJni_hello 
  16.   (JNIEnv *, jobject); 
  17.  
  18. #ifdef __cplusplus 
  19. #endif 
  20. #endif 

 

根据头文件编写c代码

  

  1. #include <string.h> 
  2. #include <jni.h> 
  3.   
  4. jstring 
  5. Java_com_xzw_jni_TestJni_hello 
  6.   (JNIEnv* env, jobject thiz){ 
  7.       return (*env)->NewStringUTF(env, "哈哈完成自动化编译 !"); 

接下来编写 Android.mk,该文件可以直接从NDK的samples下的hello-jni的jni文件下直接靠过来改改就可以了。也贴下代码哈。

 

  1. # Copyright (C) 2009 The Android Open Source Project 
  2. # Licensed under the Apache License, Version 2.0 (the "License"); 
  3. # you may not use this file except in compliance with the License. 
  4. # You may obtain a copy of the License at 
  5. #      http://www.apache.org/licenses/LICENSE-2.0 
  6. # Unless required by applicable law or agreed to in writing, software 
  7. # distributed under the License is distributed on an "AS IS" BASIS, 
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  9. # See the License for the specific language governing permissions and 
  10. # limitations under the License. 
  11. LOCAL_PATH := $(call my-dir) 
  12.  
  13. include $(CLEAR_VARS) 
  14.  
  15. LOCAL_MODULE    := testJni 
  16. LOCAL_SRC_FILES := testJni.c 
  17.  
  18. include $(BUILD_SHARED_LIBRARY) 

其中你只需要该LOCAL_MODULE和LOCAL_SRC_FILES就可以了。

说明:LOCAL_MODULE是描述模块的,用来给java调用的模块名,会生成对应的libtestJni.so

LOCAL_SRC_FILES就是源文件啦,多个文件空格隔开即可。

 

接下来,我们要开始编译生成so文件咯。

打开gnustep的命令窗口,进入到项目底下,输入$NDK/ndk-build命令,即可自动生成libs/armeabi/libtestJni.so文件。

接下来就是java调用来。直接上代码 

  1. package com.xzw.jni; 
  2.  
  3. import android.os.Bundle; 
  4. import android.app.Activity; 
  5. import android.view.Menu; 
  6. import android.view.MenuItem; 
  7. import android.widget.TextView; 
  8. import android.support.v4.app.NavUtils; 
  9. /** 
  10.  *  
  11.  * @author XuZhiwei (xuzhiwei@gmail.com) 
  12.  * sina:http://weibo.com/xzw1989 
  13.  *  
  14.  * Create at 2012-8-30 上午10:49:45 
  15.  */ 
  16. public class TestJni extends Activity {  
  17.     @Override 
  18.     public void onCreate(Bundle savedInstanceState) { 
  19.         super.onCreate(savedInstanceState); 
  20.         TextView tv = new TextView(this); 
  21.         tv.setText(hello()); 
  22.         setContentView(tv); 
  23.     } 
  24.  
  25.     public native String hello(); 
  26.     
  27.     static
  28.         System.loadLibrary("testJni"); 
  29.     } 
  30.      

运行结果如下

 

posted @ 2014-01-04 11:19  爱编程hao123  阅读(930)  评论(0编辑  收藏  举报