Eclipse下NDK開發(java調用so文件)

eclipse下兩種配置方式:

一種是右鍵工程選擇properties->builders->new->program,然後再做相應的選擇,因爲這種方式我沒有嘗試過,網上也有例子,所以我就不做詳細的說明。

另一種方式:先加載ndk路徑:


選擇標題欄的Eclipse->Preferences->



然後選擇NDK的路徑即可。

接下來新建一個android工程:TestJNI

然後右鍵工程

然後左鍵點一下你的工程就發現


這個小錘子就可以使用了,這個工具就是用來生成so文件的

接下來就是java調用c/c++的過程了(睜大眼睛,不要走開)


在這裏我們新建一個JNIClient.java類用來調用本地相關文件

  1. package com.example.wade;  
  2.   
  3.   
  4.   
  5. publicclass JNIClient {  
  6.   
  7.   
  8. static publicnative String AddStr(String strA,String strB);  
  9.   
  10.   
  11. static publicnative int AddInt(int a,int b);  
  12.   
  13. }  



然後我們使用mac的控制檯terminal,cd到JNIClient.java的目錄,然後javac JNIClient.java生成JNIClient.class文件

然後我們再cd到src目錄javah com.example.wade.JNIClient,會在src目錄下生成com.example.wade.JNIClient.h文件



把這個h文件放在jni目錄下,同時新建一個c文件,mk文件裏

  1. LOCAL_SRC_FILES := com_example_wade_JNIClient.c  

最後重寫C文件

  1. #include"com_example_wade_JNIClient.h"  
  2.   
  3. #include<stdlib.h>  
  4.   
  5. #include<stdio.h>  
  6.   
  7.   
  8.   
  9. #ifdef __cplusplus  
  10.   
  11. extern"C" {  
  12.   
  13. #endif  
  14.   
  15. /* 
  16.  
  17.  * Class:     com_example_wade_JNIClient 
  18.  
  19.  * Method:    AddStr 
  20.  
  21.  * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; 
  22.  
  23.  */  
  24.   
  25. JNIEXPORT jstring JNICALL Java_com_example_wade_JNIClient_AddStr(JNIEnv *env,  
  26.   
  27. jclass arg, jstring a, jstring b) {  
  28.   
  29. jstring str = (*env)->NewStringUTF(env,"HelloWorld from JNI !");  
  30.   
  31. return str;  
  32.   
  33. }  
  34.   
  35.   
  36.   
  37. /* 
  38.  
  39.  * Class:     com_example_wade_JNIClient 
  40.  
  41.  * Method:    AddInt 
  42.  
  43.  * Signature: (II)I 
  44.  
  45.  */  
  46.   
  47. JNIEXPORT jint JNICALL Java_com_example_wade_JNIClient_AddInt(JNIEnv *env, jclass arg,  
  48.   
  49. jint a, jint b) {  
  50.   
  51. return a + b;  
  52.   
  53. }  
  54.   
  55.   
  56.   
  57. #ifdef __cplusplus  
  58.   
  59. }  
  60.   
  61. #endif  


終於到了激動人心的一步了(最後最後一步了)

  1. public class MainActivityextends Activity {  
  2.   
  3.   
  4. static{  
  5.   
  6. System.loadLibrary("wade");  
  7.   
  8. }  
  9.   
  10.   
  11.   
  12. @Override  
  13.   
  14. protected void onCreate(Bundle savedInstanceState) {  
  15.   
  16. super.onCreate(savedInstanceState);  
  17.   
  18. setContentView(R.layout.activity_main);  
  19.   
  20.   
  21. String str = JNIClient.AddStr("","");  
  22.   
  23. System.out.println(""+str);  
  24.   
  25. int sum = JNIClient.AddInt(34);  
  26.   
  27. System.out.println("sum:"+sum);  
  28.   
  29. }  
  30.   
  31.   
  32.   
  33. @Override  
  34.   
  35. public boolean onCreateOptionsMenu(Menu menu) {  
  36.   
  37. // Inflate the menu; this adds items to the action bar if it is present.  
  38.   
  39. getMenuInflater().inflate(R.menu.main, menu);  
  40.   
  41. returntrue;  
  42.   
  43. }  
  44.   
  45.   
  46.   
  47. }  


在我們的主類裏調用庫文件,然後進行相應方法的調用即可。

這時候運行程序,會發現已經成功!!!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章