Android JNI into the Pit Tour (i): A brief introduction to JNI

Source: Internet
Author: User

1.JNI Introduction

JNI is all called Java Native Interface;

Starting with Java1.1, JNI becomes part of the Java platform, allowing Java code to interact with code written in other languages, such as c&c++;

The concept of JNI was not introduced from Android.

2. What is the NDK?

The NDK is all called the native Development Kit native language (c&c++) development kit;

The NDK provides a number of cross-compiler toolchain and Android-brought libraries;

These Android libraries allow developers to invoke programs in their native languages;

The cross-compilation toolchain provided by the NDK compiles the c&c++ code that has been written, creating a library.

Basic call flow for 3.JNI

1). The Android application layer is based on Java and the. java file must be executed by the Dalvik virtual machine (VM) after it is compiled into a. dex file.

2). If you need to use local methods (that is, C + + function library) during execution, Dalvik will load the C + + local function library (system.loadlibrary ("libname"););

Note: Because the C + + native function library is not running in the Dalvik virtual machine, the efficiency and speed are relatively fast;

3). Dalvik the virtual machine load C + + library will automatically look for the Jni_onload function inside the library (the Jni_onload function will be explained in detail later);

4). The Jni_onunload function corresponding to the Jni_onload function is called when the virtual machine releases the C + + function library to handle the aftermath cleanup.

4. Some notes about the Jni_onload function

1). Tell the Dalvik virtual machine which JNI version is used by the C + + function library;

2). The JNI1.1 version is used by default when the function is not in the library;

3). The new version of JNI has a lot of functionality, and it is necessary to declare the version of JNI in the Jni_onload () function to use these features:

1 result = jni_version_1_4;

4). When there is no jni_onload () function in the library, the Android debugging information will give the "no jni_onload found" hint;

5). When the C + + library is loaded, the Jni_onload () method is called first, and some initialization work can be done in the method, such as registering the JNI function and so on (can speed up the call efficiency and speed);

Android JNI into the Pit Tour (i): A brief introduction to JNI

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.