Android Learning Essay 27 (Jni:java Native Interface,java Native Interface)

Source: Internet
Author: User

JNI(java Native Interface,Java Native Interface )

Use JNI to enable Java code to interact with code written in other languages , such as C + + code.

Q: Why do I have to interact ?

    1. First of all, the Java language provides a class library can not meet the requirements , and in mathematics , Real-time rendering of the game , audio and video processing, and other aspects of the efficiency is slightly lower than in C + +.
    2. Then, the Java language cannot manipulate the hardware directly, and C + + code not only can operate the hardware but also perform the best hardware performance.
    3. Next, using Java to invoke the library written by the local C/D + + code eliminates the hassle of repetitive development and can make use of many open source libraries to improve program efficiency.

C Common Language terminology:

Library functions:

    • For code reuse, there are some common functions in C that are used to perform some standard tasks , such as input / out, which are compiled beforehand and generate the target code. The generated target code is then packaged into a library file for reuse. Functions in library files are called library functions, and library files are called function libraries.
    • The intermediate code in the C language library functions in Windows is suffixed with . obj , which is suffixed with. o in Linux.

Tip: A single target code cannot be executed directly, and the target code needs to use the connector to concatenate the target code and other library functions together to generate the executable file before it runs. Files for the. dll under Windows, Linux. So. a file.

Header file: xxx.h

    • The header file is a declaration of functions, macros, types, global variables, etc. defined in a library, similar to a warehouse manifest. If you need to use a function from a library in a user program, you only need to include the header file for that library in your program.
    1. A function prototype for all functions in the library is defined in the header file. The specific implementation of the function is in the library file.
    2. Simply put: The header file is for the compiler, and the library file is for the connector.
    • When the linker connects the program, the corresponding library function is imported into the program, based on the header file that is imported in the user program. The header file is named suffix . h .

function 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:

    1. Static library naming is generally:lib+ library name +. A .
    2. 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.
    3. Dynamic library naming is generally: lib+ library name +. So. . The so description is dynamic.

Cross-compiling:

    • When you connect the intermediate code to a binary program that is executable by the current computer, the connector is converted based on the current computer's CPU and operating system type.

Depending on the device you are running, you can divide the CPU 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.

Cross-compiling: Compiles binary code that can be executed in another platform under one platform. The NDK provided by Google can do cross-compilation work.

NDK Full Name: Native development Kit.

    • The NDK is a collection of tools that have many effects.
    1. First, the NDK can help developers quickly develop C (or C + +) dynamic libraries.
    2. 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.

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.

    1. 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.
    2. The C programmer then uses the C language to implement the functions defined in the header file provided by the Java programmer.
    3. The C programmer then packages the function into a library file and gives the library file to the Java programmer.
    4. 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.

--------------------------------------------------------------------------------------------------------------- -----------------------------

    • Java Platform and System environment (HOST environment)

The system environment refers to the local operating system environment, which has its own local library and CPU instruction set. Native programs (Native applications) are written in a native language such as C + + and are compiled into binary code that runs only in the context of the local system and is linked to the local library. Local programs and local libraries generally depend on a particular local system environment. For example, a C program compiled under a system cannot run on another system.

    • The role that JNI plays

The powerful nature of JNI allows us to reuse the original native code while using the Java platform. As part of the virtual machine implementation, JNI allows bidirectional interaction between Java and native code.

JNI can interact with the local program in this way:

1. You can use JNI to implement a "local method" (native methods) and invoke them in a Java program.

2. JNI supports a "calling interface" (invocation interface), which allows you to embed a JVM in a local program. Local programs can link a local library that implements the JVM, and then use the "Invoke interface" to execute a software module written in the Java language. For example, a browser written in C can perform a applets downloaded from the web on an embedded JVM.

    • Side effects of JNI

Keep in mind that once you use the Jni,java program, you lose the two benefits of the Java platform:

1, the program is no longer cross-platform. To cross-platform, you must recompile the local language section in a different system environment.

2, the program is no longer absolutely safe, the improper use of local code may cause the entire program to crash.

A common rule is that you should let local methods focus on a few classes. This reduces the coupling between Java and C.

    • What scenarios should use JNI

When you start to prepare a project that uses JNI, make sure there are alternatives. As mentioned in the previous section, applications using JNI can have some side effects. Here are a few scenarios where you can avoid using JNI to interact with your local code:

1. Java programs and local programs use TCP/IP or IPC to interact.

2. Use the API provided by JDBC when connecting to a local database with a Java program.

3. Java programs can use distributed object technology, such as the Java IDL API.

What these scenarios have in common is that Java and C are on different threads, or on different machines. This way, when the local program crashes, the Java program is not affected.

In the following scenarios, the use of JNI within the same process cannot be avoided:

1, the program uses the Java API does not provide a special system environment will have the characteristics. And the process of operation is not realistic.

2. You may want to access some of your own local libraries, but don't want to pay the cost of cross-process calls, such as efficiency, memory, and data transfer.

3, Java program part of the code on the efficiency requirements are very high, such as algorithm calculation, graphics rendering and so on.

In summary, use JNI only when you must invoke local code in the same process.

    • The evolution of JNI

JDK1.0 contains a local method interface that allows a Java program to invoke a program written by C + +. Many third-party programs and Java class libraries, such as java.lang,java.io,java.net, rely on local methods to access the characteristics of the underlying system environment.

Unfortunately, the local approach in JDK1.0 has two major problems:

1. The local method accesses the fields in the object like the structure in C (structures). However, the JVM specification does not define how objects are implemented in memory. If a given JVM is implemented in a layout object, and the local method assumes something different, then you have to rewrite the local method library.

2. Because local methods can maintain a direct pointer to objects in the JVM, local methods in JDK1.0 adopt a conservative GC policy.

The birth of JNI is to solve these two problems, it can be supported by the JVM under all platforms:

1. Each VM implementation scenario can support a large number of native code.

2. Developer tool authors do not have to deal with different local method interfaces.

3, most importantly, the local code can run on different JVMs.

For the first time in JDK1.1, JNI is supported, but JDK1.1 still uses the old-style native code to implement the Java API. This situation has been completely changed under JDK1.2 to conform to the standard wording.

--------------------------------------------------------------------------------------------------------------- -------------------------------

Android Learning Essay 27 (Jni:java Native Interface,java Native Interface)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.