Small white: Why can't I write C or C + + code directly in Java code? And the OC code can be written directly!
Small range: You can write, but only compile.
Xiao Bai: That is not to write it!
Whether it is compiled to the JVM or run on the Dalvik, the Java compiler does not generate the execution file directly, but only the bytecode (represented as a class file, or a converted Dex file), which is quite different from the C + + compiler, and most likely for this reason, So that Java can not be mixed with C + + code, and objective-c could be mixed with C + +, because its compiler directly generated execution files.
Can't the Java language use C + + code? If there is a problem, there is a solution, and the way is to use JNI.
The advent of JNI (Java Native Interface, or Java native Interface) solves the problem of Java interacting with C + + and is not limited to C + +.
Little white: But, my Android program, why should I call C + + code?
Small range: For a reason, for example, has implemented a function of C + + code instead of Java code, such as to do a lot of data operations and C + + execution performance is far above Java, such as to invoke some of the driver class interface, such as ...
This article demonstrates how Java calls C + + code, which is the use of JNI.
JNI layer, can be divided into the upper Jni, with the lower JNI.
The JNI upper layer is implemented in Java, and the JNI lower layer is implemented in C + +.
The approximate invocation relationship is this:
The implementation of the JNI upper and lower layers is described in detail, and how to generate the so library (so library represents the C + + implementation function) and call it.
(1) Implementation of the JNI upper layer
Just look for a Java class (or create a new one) and then declare a native method inside, and implement the JNI upper layer.
For example, create an Android studio project and have such a native method declaration:
At this point, the top of the JNI has been established.
(2) Implementing the JNI lower layer
What functions are called to C + + when Java code calls the native function in the JNI upper layer? A mapping relationship is required here.
There are two ways to build this mapping.
Method One: Use signature
For the native method, using Javah, you can extract the corresponding signature, and then C + + Implement the signature function.
You can generate signatures like this:
The contents of this. h file are as follows:
That long string is the corresponding signature, the C + + code implementation of this function, so that Java in the call to the JNI layer declaration of the native function, it will be called to this side. Obviously, once a mapping is established, the upper and lower JNI can no longer change names (unless re-signed).
Small white: A large string of, not to rename, Big human.
Small range: To change the name of the new interface, or, the use of a second method.
Method Two: Use Registernatives
When the JNI layer is loaded, call registernatives.
This approach, instead of using Javah to generate a fixed signature, uses Registernatives to establish an association between the Navtive method and the specific implementation, which can be arbitrarily named.
Within the Jni_onload function, the invocation of the registernatives is triggered:
After solving the problem of the mapping of the top and bottom of JNI, it is the concrete implementation of the Natvie method. This article describes how to generate a dynamic link so library.
In the root directory of the project, create a JNI directory where you write C + + code and compile the script, which is the directory structure:
Content of IMPL.C:
What the script android.mk:
Execute ndk-build, generate so library:
You can view so libraries with readelf:
This involves compiling scripts (ANDROID.MK) rules, Smalis language features (JNI data types, etc.), as well as the use of ndk-build knowledge, the small range is not expanded, the reader can focus on "Guangzhou Xiao Cheng" public number, and to obtain follow-up updates.
(3) Call the JNI layer
Call the native method at the Java layer, for example:
To execute this program, you can see the output:
In this way, how to invoke the C + + implementation on the Android platform is complete.
To summarize, this article describes how to invoke the C + + implementation on the Android platform, demonstrates the creation and compilation of the JNI layer and the so library, and calls the so library. Readers can apply this knowledge to real Android projects.
Android Platform calls C + +