JNI开发示例
安装:eclipse(http://www.eclipse.org/)、CDT(C/C++ Development Tooling)、ADT(Android Development Tools)
https://dl-ssl.google.com/android/eclipse/
可以先从官方下载jar,然后本地安装:http://www.eclipse.org/cdt/downloads.php
下载Android NDK (Native Development Kit)http://developer.android.com/tools/sdk/ndk/index.html
JNI的使用方法
使用javah建立C/C++的头文件(*.h)
切换到src目录,然后输入命令(注意编码):
将.h复制到jni目录下
如果运行出错,参考这里下载资源:http://blog.csdn.net/wjr2012/article/details/16359113
在C++中调用Java方法设置文本的内容
最后我们通过Android的点击事件来调用C++,该C++方法调用Java方法来弹出一个对话框
效果如下图
源代码已经上传至github https://github.com/zhangyi/AndroidJNIProject
最后附上一些JNI的参考链接:
https://en.wikipedia.org/wiki/Java_Native_Interface
https://newcircle.com/s/post/1292/jni_reference_example
http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/index.html
返回值为jintArray、jbyteArray、jlong、jobjectArray、jint、jboolean、jobjectArray、void的示例代码
https://joinup.ec.europa.eu/svn/mocca/osx-pcsc-jni/trunk/openjdk7/pcsc.c
Java Programming Tutorial Java Native Interface(JNI)
https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html
Call Java functions from C++ in cocos2d-x
http://stnguyen.com/cocos2d-x/call-java-functions-from-cpp.html
Call C++ functions from Java in cocos2d-x
http://stnguyen.com/cocos2d-x/call-cpp-functions-from-java.html
Java programming with JNI
http://www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html