eclipse下两种配置方式:
一种是右键工程选择properties->builders->new->program,然后再做相应的选择,因为这种方式我没有尝试过,网上也有例子,所以我就不做详细的说明。
另一种方式:先加载ndk路径:

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


然后选择NDK的路径即可。
接下来新建一个android工程:TestJNI
然后右键工程

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

这个小锤子就可以使用了,这个工具就是用来生成so文件的
接下来就是java调用c/c++的过程了(睁大眼睛,不要走开)

在这里我们新建一个JNIClient.java类用来调用本地相关文件
- package com.example.wade;
-
-
-
- publicclass JNIClient {
-
-
- static publicnative String AddStr(String strA,String strB);
-
-
- static publicnative int AddInt(int a,int b);
-
- }
然后我们使用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文件里
- LOCAL_SRC_FILES := com_example_wade_JNIClient.c
最后重写C文件
- #include"com_example_wade_JNIClient.h"
-
- #include<stdlib.h>
-
- #include<stdio.h>
-
-
-
- #ifdef __cplusplus
-
- extern"C" {
-
- #endif
-
-
-
-
-
-
-
-
-
-
-
- JNIEXPORT jstring JNICALL Java_com_example_wade_JNIClient_AddStr(JNIEnv *env,
-
- jclass arg, jstring a, jstring b) {
-
- jstring str = (*env)->NewStringUTF(env,"HelloWorld from JNI !");
-
- return str;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- JNIEXPORT jint JNICALL Java_com_example_wade_JNIClient_AddInt(JNIEnv *env, jclass arg,
-
- jint a, jint b) {
-
- return a + b;
-
- }
-
-
-
- #ifdef __cplusplus
-
- }
-
- #endif
终于到了激动人心的一步了(最后最后一步了)
- public class MainActivityextends Activity {
-
-
- static{
-
- System.loadLibrary("wade");
-
- }
-
-
-
- @Override
-
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.activity_main);
-
-
- String str = JNIClient.AddStr("","");
-
- System.out.println(""+str);
-
- int sum = JNIClient.AddInt(3, 4);
-
- System.out.println("sum:"+sum);
-
- }
-
-
-
- @Override
-
- public boolean onCreateOptionsMenu(Menu menu) {
-
-
-
- getMenuInflater().inflate(R.menu.main, menu);
-
- returntrue;
-
- }
-
-
-
- }
在我们的主类里调用库文件,然后进行相应方法的调用即可。
这时候运行程序,会发现已经成功!!!