Android JNI, NDK, and androidjnindk

Source: Internet
Author: User

Android JNI, NDK, and androidjnindk

 

1. Android NDK

I. background of NDK generation

Since its birth, the Android platform has supported C and C ++ development. As we all know, Android sdks are implemented based on Java, which means that third-party applications developed based on Android sdks must use the Java language. However, this is not equivalent to "third-party applications can only use Java ". At the first release of the Android SDK, Google declared that its virtual machine Dalvik supports the JNI programming method, that is, third-party applications can call their own C dynamic library through JNI, that is, on the Android platform, the "Java + C" programming method can always be implemented.

However, Google also said that native SDK programming is also inferior to the Dalvik virtual machine, and no JNI help can be found in the Android SDK documentation. Even if third-party application developers use JNI to complete their own C dynamic link library (so) development, How can so be packaged into an apk and released together with the application? There are also technical barriers. For example, the program is more complex, the compatibility is difficult to guarantee, the Framework API cannot be accessed, and debugging is more difficult. Developers need to use it at their discretion.

Therefore, NDK came into being. The full name of NDK is Native Development Kit.

With the release of NDK, the "Java + C" development method has finally become officially supported. NDK will be the beginning of C development support for the Android platform.

 

Ii. Why NDK

1. Code Protection. Because the java-Layer Code of apk is easily decompiled, it is difficult to reverse the C/C ++ library.

2. You can easily use existing open source libraries. Most of the existing open-source libraries are written in C/C ++ code.

3. Improve program execution efficiency. The high-performance application logic is developed using C to improve the application execution efficiency.

4. Ease of transplantation. The C/C ++ library can be easily used on other embedded platforms.

 

Iii. About NDK

1. NDK is a collection of tools

NDK provides a series of tools to help developers quickly develop C (or C ++) dynamic libraries, and can automatically package so and java applications into apk together. These tools are of great help to developers.

NDK integrates with the cross compiler and provides mk files to isolate differences such as CPU, platform, and ABI, developers can create so simply by modifying the mk file (indicating "which files need to be compiled" and "Compilation feature requirements.

NDK can automatically package so and Java applications, greatly reducing developers' packaging work.

2. NDK provides a stable and functional API header file statement.

Google explicitly states that this API is stable and supports the currently released API in all subsequent versions. From the NDK version, we can see that these Apis support very limited functions, including: C standard library (libc), standard Math Library (libm), and compression library (libz), Log Library (liblog ).

 

Iv. Establishment of the NDK Development Environment

1. Download and install Android NDK

Address: http://developer.android.com/sdk/ndk/index.html

 

2. Android JNI

Native; indigenous; natural; inherent; gifted

Native App Local application; Native application; client application; Native Application

 

I. Basic Concepts

JNI: the Java Native Interface (JNI) standard is part of the java platform. It allows Java code to interact with code written in other languages.

JNI is a local programming interface that enables the Java code running inside the Java Virtual Machine (VM) to work with other programming languages (such as C, C ++, and assembly languages) write Applications and libraries for interaction.

 

Ii. Android JNI is generally used:


1. Mainly some algorithms. Because c/c ++ is more efficient than Java, applications run faster, especially some algorithms in games.
2. For the sake of confidentiality, we all know that apk can be decompiled. Even if there is code obfuscation, it is only hard to understand. It is not completely incomprehensible, but compiled. so is different, so it can achieve the purpose of not disclosing commercial secrets.
3. When a platform (C ++ Code) is migrated to the Android platform, the underlying logic is the same, so that the underlying C ++ code can be called using JNI through transplantation, avoid repeated write of code with the same logic. However, you must pay attention to the release of underlying objects in this process.

 

3. Relationship between NDK and JNI

 

JNI is a mechanism for communication between java and C/C ++ provided by Java. Java can call local C/C ++ code through JNI, the local C/C ++ code can also call java code.

JNI is a local programming interface that Java and C/C ++ use each other. One of the key reasons why Java uses local code through C/C ++ is the efficiency of C/C ++ code.

 

NDK is a collection of tools. It provides a series of tools to help developers quickly develop C (or C ++) dynamic libraries, and can automatically package so and java applications into apk together. These tools are of great help to developers.

It integrates with the cross compiler and provides mk files to isolate differences such as CPU, platform, and ABI, developers can create so simply by modifying the mk file (indicating "which files need to be compiled" and "Compilation feature requirements.

It can automatically package so and Java applications, greatly reducing developers' packaging work.

 

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.