Android-JNI development-things you don't know

Source: Internet
Author: User

I. JNI(Java Native Interface,Java Native Interface)

Using JNI can make JavaCodeCode written in other languages(Such as C/C ++ code).

Q: Why is interaction required??

|-First, the class libraries provided by Java cannot meet the requirements.,And in mathematical operations,Real-time rendering on the game,Audio and video processing is less efficient than C/C ++.

|-Then, the Java language cannot directly operate the hardware, and the C/C ++ code can operate not only the hardware but also the best performance of the hardware.

|-Next, we use Java to call the libraries written by local C/C ++ code, saving the trouble of repeated development, and can use a lot of open-source libraries to improveProgramEfficiency.

II. CCommon Language terms:

Library functions:

|-For code reuse, the C language provides some common standard tasks for execution.(As input/Output)These functions are compiled in advance, and the target code is generated. Then, the generated target code is packaged into a library file for reuse. Functions in the library file are called library functions, and library files are called function libraries.

|-In Windows, the intermediate code in C language library functions is. OBJIn Linux. OSuffix.

Tip: a single target Code cannot be directly executed. Before running the target code, you need to use a Connection Program to connect the target code with other library functions to generate executable files. Windows. dll file, Linux. So. A file.

Header file:

|-The header file declares the functions, macros, types, and global variables defined in a database. It is similar to a repository list. If the user program needs to use a function in a library, you only needIncludeTo the program.

|-The header file defines the function prototype of all functions in the library. The specific implementation of the function is in the library file.

|-Simply put, the header file is used by the compiler, and the library file is used by the connector.

|-When connecting to a program through a connector, the corresponding library functions are imported to the program based on the header files imported in the user program. The header file uses. HSuffix.

Function library:

|-Dynamic library: when compiling a user program, the library functions used in the user program are not connected to the target code of the user program, the corresponding functions in the function library are called only when the user program executes the relevant functions. Therefore, the executable files generated by the dynamic function library are relatively small.

|-Static Library: when compiling a user program, the library functions used in the program are connected to the target code, and the static library is no longer required when the program is running. Generating executable files using static libraries is relatively large.

In Linux:

|-The static library name is generally:Lib+ Database name +..

|-For example:Libcxy.Lib indicates that this file is a library file, and cxy indicates the library name,.The description is static.

|-The dynamic library name is generally: Lib + Library name +.So..So is dynamic.

Cross-Compilation:

|-When the intermediate code is connected to a binary program that can be executed by the current computer, the Connection Program is converted according to the CPU and operating system types of the current computer.

The CPU can be divided:

|-Arm Structure: mainly on mobile handheld devices and embedded devices.

|-X86 structure: Mainly used on desktops and laptops. Such as Intel and AMD CPUs.

If you want to compile the code that can be run on an arm-structured CPU operating system in an operating system that uses an x86 CPU, you must use cross-compilation.

Cross-Compilation: Compile binary code that can be executed on another platform. The ndk proposed by Google can complete cross-compilation.

Ndk stands for Native Development Kit.

|-Ndk is a collection of tools and has many functions.

|-First, ndk can help developers quickly develop C (or C ++) dynamic libraries.

|-Secondly, ndk integrates the cross compiler. With ndk, we can use C to develop high-performance application logic, thus improving the application execution efficiency.

The ndk tool must run in Linux. It can compile binary library files that can run on the ARM platform in Linux.

Using JNI technology is actually a Java program that calls the functions provided by the C function library to complete tasks that cannot be completed by Java. Because the Java and C language structures are completely different, if you want them to interact, you need to develop a series of specifications. JNI is this set of specifications. At this time, Java only interacts with JNI, and JNI interacts with C.

JNI technology is divided into two parts: Java and C. It is dominated by Java.

|-First, Java programmers define some native methods on the Java end and provide these methods to C programmers in the form of C header files.

|-Then, C programmers use the C language to implement the functions defined in the header file provided by Java programmers.

|-Then, C programmers package functions into a library file and hand over the library file to Java programmers.

|-Finally, the Java programmer imports the library file in the Java program and then calls the native method.

During Java program execution, if the native method is called in a class, the C language code in the library file is transferred through JNI. Tip: The C code is finally executed in the Linux Process, rather than in the virtual machine.

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.