Android studio JNI technology links with the native C ++

Also his earlier writing a recognition Android the APP, wherein in order to improve the speed, the local side used C ++ code realization face detection recognition +

We all know that Android supports Java language interaction, but does not support C and C ++, but sometimes for the convenience and needs, the need to use the Java application layer calls the underlying C and C ++ code, this time you need to use the Java JNI technology called Java native Interface technology, Java and C or C ++ call

Now I would like to talk a whole steps and principles of JNI technology:

1. Create a new Android studio project (note on this check, otherwise the back also need to configure, no need to check on their own configuration Cmake, Gradle friends)

2, Android specified position to create a new class, as JavaJNI.java class (general location, src / main / java / "your package name" /), a declaration inside the class the method has achieved the local side, i.e., The public native void open ();

3, after written after local native method configured Javah, steps: File-Tools-External Tools- "click the plus sign" name casually from a convenient unified call javah, the following Description and above can be consistent;

Program: $JDKPath$\bin\javah.exe

Auguments: -classpath . -jni -o $ModuleFileDir$/src/main/jni/$Prompt$ $FileClass$

Working directory: $ModuleFileDir$\src\main\java

The above is the configuration Javah process, to which configured, you note that the above configuration can be understood as a few fixed configuration, in fact, it is some of the path definition, can not control, and

4, the mouse to select just the new class contains native implementation methods, right click and select External Tools of Javah, randomly pop-up window to enter the name (the name is immediately generated names in C or C ++ header files, the file will be saved in the / src / main / jni / below), so we have a C or C ++ header files are generated good at JNI file,

5, the generated .h files into the header, src / main / cpp files

6, in the cpp file .cpp file corresponding to a new start writing method needs to call a local function (the specific approach just .h file name generation method is copied, ";" Method to form "{}" then the method body using C ++ implementation of the features you need)

7, local calls JavaJNI.java class declared in class MainActivity.java test methods

8, the compiler can achieve a successful call C ++ methods you write

Guess you like

Origin www.cnblogs.com/rhp2lmy1314/p/11028224.html