发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
下载ADT (Android Developer Tools) Bundle:http://developer.android.com/sdk/index.html
创建普通的Android Application工程(注意最小支持的API版本要不小于14)
加入本地代码支持
package com.lc.testndk2;
import android.util.Log;
public class NativeClass {
//数组a中的每个元素都加上b,返回值为在C++中数据是否为a中数据拷贝得到的(按值拷贝还是传递指针)
public static native boolean jniArrayAdd(int[] a, int b);
// 在C++中创建Java中的int数组,其中元素为 数组a中的对应元素乘以b
public static native int[] jnitArrayMul(int[] a,int b);
static {
Log.i("NativeClass","before load library");
System.loadLibrary("TestNDK2");//注意这里为自己指定的.so文件,无lib前缀,亦无后缀
Log.i("NativeClass","after load library");
}
}
使用我写的工具,含本地方法的类多时,一次编译完成:http://cherishlc.iteye.com/blog/1326893
在Eclipse中配置外部工具,配置方法参考自(同时也是讲Android中JNI配置方法的博客):http://www.cnblogs.com/yemeishu/archive/2012/12/24/NDK%E5%BC%80%E5%8F%91.html
-v -classpath "${project_loc}/bin/classes" -d "${project_loc}/jni" ${java_type_name}
package com.lc.testndk2;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
int[] array = new int[] { 1, 2, 3};
String str = "数组,调用C++前" + Arrays.toString(array);
boolean isCopyOfArrayInCpp = NativeClass.jniArrayAdd(array, 1);
str += "\n在C++中为副本? " + isCopyOfArrayInCpp;
str += "\n数组,调用C++后:" + Arrays.toString(array);
tv.setText(str);
setContentView(tv);
}
}
#include <jni.h>
#include "com_lc_testndk2_NativeClass.h"
#ifdef __cplusplus //最好有这个,否则被编译器改了函数名字找不到不要怪我
extern "C" {
#endif
/*
* Class: com_lc_testndk2_NativeClass
* Method: jinArrayAdd
* Signature: ([II)[I
*/JNIEXPORT jboolean JNICALL Java_com_lc_testndk2_NativeClass_jniArrayAdd(
JNIEnv * env, jclass, jintArray array, jint b) {
jsize size = env->GetArrayLength(array);
// jintArray sum=env->NewIntArray(2);
jboolean isCopy;
jint* pArray = (jint*) env->GetPrimitiveArrayCritical(array, &isCopy);
for (int i = 0; i < size; i++)
pArray[i] += b;
env->ReleasePrimitiveArrayCritical(array, pArray, JNI_COMMIT);
//env->ReleasePrimitiveArrayCritical(sum,pSum,JNI_COMMIT);
return isCopy;
}
/*
* Class: com_lc_testndk2_NativeClass
* Method: jnitArrayMul
* Signature: ([II)[I
*/JNIEXPORT jintArray JNICALL Java_com_lc_testndk2_NativeClass_jnitArrayMul(
JNIEnv * env, jclass, jintArray array, jint b) {
jsize size = env->GetArrayLength(array);
jintArray product = env->NewIntArray(size);
jint* pArray = (jint*) env->GetPrimitiveArrayCritical(array, 0);
jint* pProduct=(jint*)env->GetPrimitiveArrayCritical(product,0);
// jintArray product = env->NewIntArray(size); //不能在这里创建!!因为上面的方法会使java进入critical region, 在这里创建的话虚拟机直接崩溃
for (int i = 0; i < size; i++)
pProduct[i] =pArray[i]* b;
env->ReleasePrimitiveArrayCritical(array, pArray, JNI_COMMIT);
env->ReleasePrimitiveArrayCritical(product,pProduct,JNI_COMMIT);
return product;
}
#ifdef __cplusplus
}
#endif
C++的调试器有几秒的延迟才能启动好,也就是程序运行了一会儿才可以开始调试,所以要调试的代码一定要是几秒钟后才能调试!!!
断点设置在C++中才有效。。。
package com.lc.testndk2;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
/**
* @author LC
*
*完整的演示Android通过JNI调用C++代码的工程
*/
public class MainActivity extends Activity {
TextView tv = null;
int count = 0;
Timer timer;
@SuppressLint("HandlerLeak")
class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
if (tv != null) {
tv.setText(msg.getData().getString("text"));
}
super.handleMessage(msg);
}
};
Handler handle= new MyHandler();
class refreshTask extends TimerTask {
@Override
public void run() {
try {
count++;
Log.i("MainActivity", "before call native code,count="
+ count);
int[] array = new int[] { count, -count, 2*count };
String str = "第" + count + "次了\n";
str += "数组,调用C++前" + Arrays.toString(array);
boolean isCopyOfArrayInCpp = NativeClass.jniArrayAdd(array,1);
str += "\n在C++中为副本? " + isCopyOfArrayInCpp;
str += "\n数组,调用C++后:" + Arrays.toString(array)+"\n\n";
str+="测试在C++中创建数组:\n";
str += Arrays.toString(array)+"* 2 =";
str+=Arrays.toString(NativeClass.jnitArrayMul(array, 2))+"\n\n";
Message msg=new Message();
Bundle b=new Bundle();
b.putString("text", str);
msg.setData(b);
handle.sendMessage(msg);
Log.i("MainActivity", "after call native code");
} catch (Exception e) {
Log.i(MainActivity.class.getSimpleName(), e.toString());
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
tv.setText("我是初始值");
setContentView(tv);
}
@Override
protected void onPause() {
Log.i(MainActivity.class.getSimpleName(),"onPuase()");
timer.cancel();
timer=null;
super.onPause();
}
@Override
protected void onResume() {
Log.i(MainActivity.class.getSimpleName(),"onResume()");
timer=new Timer();
timer.scheduleAtFixedRate(new refreshTask(), 0, 1000);
super.onResume();
}
}
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。