Eclipse下NDK开发(java调用so文件)

eclipse下两种配置方式:

一种是右键工程选择properties->builders->new->program,然后再做相应的选择,因为这种方式我没有尝试过,网上也有例子,所以我就不做详细的说明。

另一种方式:先加载ndk路径:


选择标题栏的Eclipse->Preferences->



然后选择NDK的路径即可。

接下来新建一个android工程:TestJNI

然后右键工程

然后左键点一下你的工程就发现


这个小锤子就可以使用了,这个工具就是用来生成so文件的

接下来就是java调用c/c++的过程了(睁大眼睛,不要走开)


在这里我们新建一个JNIClient.java类用来调用本地相关文件

[java]  view plain copy print ?
  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文件里

[plain]  view plain copy print ?
  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  


终于到了激动人心的一步了(最后最后一步了)

[java]  view plain copy print ?
  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. }  


在我们的主类里调用库文件,然后进行相应方法的调用即可。

这时候运行程序,会发现已经成功!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值