Sunshine Xiao Qiang took part in the CSDN blog star, if you think Sun Qiang's blog to help you, for Xiao Qiang vote: http://vote.blog.csdn.net/blogstar2014/details?username=lxq_xsyu#content
In the previous article we learned about the use of JNI in Android, in fact Jni is very early, not the new technology created by Android, is a method that Sun provides us with a way to call each other between Java and native code. In this article, we'll build a common Java project to see how C + + code is called in Java.
First, create a common Java project
Package com.csdn.test;/** * Sunshine Cockroach Http://blog.csdn.net/dawanganban * @author Lixiaoqiang * */public class Testjni {public n ative void Savehello ();p ublic static void Main (string[] args) {Testjni jni = new Testjni (); Jni.savehello ();}}
In Testjni we have declared a native method.
Second, generate A/C + + header file
Sun's JDK has provided us with a tool to generate a corresponding C + + header file (Javah)
Description: I use the Max OS system here, if you are a Windows system, look in the JDK directory.
Locate the directory where our project files are located, execute the javah command as follows:
You can see that a com_csdn_test_testjni.h header file has been generated for us
/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Com_csdn_test_testjni */#if Ndef _included_com_csdn_test_testjni#define _included_com_csdn_test_testjni#ifdef __cplusplusextern "C" {#endif/* * Class: com_csdn_test_testjni * Method: Savehello * Signature: () V */jniexport void Jnicall java_com_csdn_test_ Testjni_savehello (jnienv *, jobject); #ifdef __cplusplus} #endif #endif
Third, generate the dynamic library file below we open the C + + editor (which I use when Xcode, a friend on the Windows platform can use VisualStudio)
In the main.cpp we output a line of text, as follows:
#include "com_csdn_test_testjni.h" #include <iostream>using namespace std; Jniexport void Jnicall Java_com_csdn_test_testjni_savehello (jnienv * evn, Jobject obj) { cout << "Hello World" & lt;< Endl;}
In the process of compiling will find that jni.h and jni_md.h files cannot be found, these two files can be found in the JDK's include directory, copied to the project, the above project to export the dynamic link library.
When exporting a link library (compile) in MacOS and Linux, Windows is different, not compiled into. So or DLLs, but MacOS's own jnilib. And jni.h directory is also more special, is /system/library/frameworks/javavm.framework/headers/, this needs a little attention, the specific command is as follows:
You can see that a libhelloworld.jnilib file is generated in the directory, and we copy the file to our Java project's Bin directory (under Windows is the classes directory)
Add code to the Java project that adds a dynamic library (the System.loadlibrary method in the following static statement block)
Package com.csdn.test;/** * Sunshine Cockroach Http://blog.csdn.net/dawanganban * @author Lixiaoqiang * */public class Testjni {public n ative void Savehello ();p ublic static void Main (string[] args) {Testjni jni = new Testjni (); Jni.savehello ();} Static{system.loadlibrary ("HelloWorld");}}
Finally, you can run the output "HelloWorld" by adding the library file to an environment variable that the Java program can access.
Next article we will look at how to invoke the methods in Java in C + +
In-depth understanding of Android (2)-Understanding JNI in Android (medium)