The JNI (Java Native Interface,java Local interface) allows the Java code to interact with code written in other languages, such as C + + code. Why do you want to interact? First of all, the Java language provides a class library can not meet the requirements, and in mathematical operations, real-time rendering of the game, audio and video processing and other aspects of the efficiency is slightly lower than that of C + +, as shown in. Second, the Java language does not directly manipulate the hardware, C + + code can not only operate the hardware and can also play the best hardware performance, and the use of Java to call the local code written by C + +, save the trouble of duplication of development, and can use a lot of open source Library to improve program efficiency.
The use of JNI technology, in fact, is in the Java program, called the C language function library provides functions to complete some of the Java language can not complete the task. Because the Java language and the C language structure are completely different, if you want to interact with them, you need to develop a series of specifications. JNI is this set of specifications, when Java interacts only with JNI, and the C language interacts with JNI.
JNI technology is divided into two parts: Java side and C language side. And is dominated by the Java side. First, the Java programmer defines some native methods on the Java side and provides these methods to C programmers in the form of a C-language header file. The C programmer then uses the C language to implement the functions defined in the header file provided by the Java programmer. The C programmer then packages the function into a library file and gives the library file to the Java programmer. Finally, the Java Programmer imports the library file into the Java program and then calls the native method.
When the Java program executes, if the native method is called in a class, the virtual opportunity passes through JNI to invoke the C language code in the library file. Tip: The C code is ultimately executed in the Linux process, not in the virtual machine.
Cross-compiling:
Compiles binary code that can be executed in another platform under one platform. The NDK proposed by Google can do cross-compilation work. The principle is that when the intermediate code is connected to a binary program that is currently executable by the computer, the connector is converted based on the current computer's CPU and operating system type. Depending on the device you are running, the CPU can be divided into: ARM structure: Mainly in mobile handheld, embedded devices. x86 structure: Mainly used on desktops and notebooks. such as Intel and AMD's CPUs.
Cross-compilation is required if you want to compile code that can be run on an operating system that is based on an ARM structure CPU using an operating system based on the x86 structure CPU.
Android NDK
The NDK (Native Development Kit) is a collection of tools that have many effects. First, the NDK can help developers quickly develop C (or C + +) dynamic libraries. Second, the NDK integrates a cross compiler. Using the NDK, we can improve application execution efficiency by using C development for application logic that requires high performance.
The NDK tool must run under Linux, and it can compile binary library files that can be run under the ARM platform in a Linux environment.
C Dynamic Library
Dynamic Library: The library functions used in the user program are not connected to the target code of the user program when compiling the user program, and the corresponding function in the library is called only at run time and the user program executes to the related function, so the dynamic function library produces less executable files. Static libraries: When you compile a user program, the library functions that are used in it are connected to the target code, and the static library is no longer required when the program runs. Using a static library to build the executable file is relatively large.
In Linux, static library naming is generally: lib+ library name +.A. such as: Libcxy.a where Lib indicates that this file is a library file, Cxy is the name of the library, and the. A description is static. Dynamic library naming is generally: lib+ library name +.so. The. So description is dynamic.
Writing an Android project that can interact with C code requires the following steps:
The Java code writes the declaration native method public native String Hellofromjni ();
Create a JNI directory, write C code, the method name corresponds to
Writing a android.mk file (cross-compiled specification)
NDK compilation generates a dynamic library
Java code load Dynamic Library. Calling native code
Android JNI-Basic article