The NDK, SDK, and JNI relationships

Source: Internet
Author: User

Recently, due to the needs of the project, the use of the Android NDK technology, the project core algorithm cross-platform porting. In short, the original algorithm was improved using C and integrated into the original app project.

The previous project has never had the opportunity to develop with the NDK, and has been admiring the technical bull's articles on the NDK and C/D + + development, which have been very esoteric and have been stirring.

In fact, the development of the NDK is not complex, in terms of the introduction can even be said to be easy job, it is difficult to find it hard to write and debug C/s code. This is my recent experience in the development of the NDK!

First, we need to understand a few concepts, what is the NDK, and what does it have to do with the SDK and JNI? Look before you:

JNI (Java Native Interface), Java Local interface

JNI is one of the many Java development technologies that are designed to leverage native code to provide more efficient and flexible development of Java programs. The application scenarios include: the implementation of the efficiency-sensitive algorithm, cross-platform application porting, invoking the system's underlying driver, calling the hardware, etc. Although Java has always been known for its good cross-platform, the True cross-platform king should be C + +, as 90% of the world's systems are written in C/S. Java's cross-platform, is to sacrifice efficiency in exchange for a variety of platform compatibility, so JNI can be said to be the Java Short Board supplement! As an example, the current popular mobile operating system Android, has been said that the system operation is not as fluent as iOS, because the Android app is based on Java development, iOS is based on the object-c development, the difference is the same operation, on iOS on the completion of an instruction , on Android you need three instructions to complete (data from the network, not necessarily accurate)! So in the Android Jellybean version, Google introduced the Projectbutter (butter program), used a lot of local libraries in the application layer, and optimized the architecture of the system to improve the overall operation of the Android system response!

Ka Ka, the introduction of JNI first here, in short, JNI is a technology, is the Java code and C + + code link bridge!

The process of JNI development

1, write Java code, such as the following example:

public class Mainactivity extends Activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Toast.maketext (Getapplicationcontext (), Sayhellow (), Toast.length_long). Show ();
}

Public native String Sayhellow (); Calling local methods

static {
System.loadlibrary ("Scgps_client"); Loading a local shared library
}

}

2, write C + + code, such as the following example:

#include
#include

Jniexport jstring jnicall Java_com_scgps_client_mainactivity_sayhellow (jnienv* env, Jobject thiz)
{
const char * ret = "Hellow Form Ndk";
Return (*ENV)->newstringutf (env, ret);
}

3. Compile C + + Code, succeed and get local shared library

The local shared library is the name under Linux and the file extension is. so,windows is called the dynamic link library, and the file name extension is. dll. In front of the C + + is the cross-platform king, which is the truth, facing different platforms, compiling different results. The cross-platform performance of C/s + + is a sacrifice of compile time and compilation difficulty, compared to Java, which runs across platforms at the expense of running efficiently. The difficulty of compiling here refers to the adjustment of the compiling process to adapt to different platforms, the difficulty of this work can be small, not necessarily successful, depending on the compatibility of the platform and Support. When it comes to this, people will inevitably spray: Say what cross-platform, so complex is not stable! It is true that the cross-platform is limited, but in the current platform and system, which one does not support the local development of C/D + +? Just the underlying API and the compiler conditions are different, just need to adjust the C + + compiler code, compiled to run, is not a thing?

4. Compiling and Packaging Java

Place the local shared library into the specified directory of the Java project, usually the Libs file, the Android project is Libs/armeabi (Armeabi is the corresponding platform, will be detailed later), and then compile Java code to run!

NDK, (Native develop kit), Local development Kit

The NDK is a local development tool released by Google for Local development of Android, including Android's native API, public libraries, and compilation tools, noting that the NDK needs support for Android version 1.5 or more.

According to the explanation, the NDK is in the compilation of the development process, and, to put it simply, the NDK is an extension toolkit developed by JNI! For the Android platform, it supports a wide range of devices, the core CPU alone, there are three categories: ARM, x86 and MIPS, and arm is divided into ARMv5 and ARMv7, and so on, why Android can be adapted to such a large number of devices? Then the JNI development process, using the NDK, we can compile a corresponding local shared library for different mobile devices, as to how to use the NDK for compiling and development, we will leave it for the next discussion.

SDK, standard Develop kit, standards Development Kit

The SDK is the Android Standard development Kit provided by Google, which contains complete API documentation, Android version of the development library, Android virtual machines and Android packaging tools. As is known to all, the Android application development language is Java,app runtime, which belongs to the modified version of the JVM, and the official said Delvik VMs are more suitable for mobile devices. In general, since Google's SDK provides a powerful and sophisticated API to develop applications that are generally required, the SDK is sufficient. However, as already mentioned, Java's operational efficiency has caused a lot of problems, so there is the existence of JNI technology, the SDK and the NDK relationship? The idea is that, to be said, the NDK is a complement to the SDK.

The development process of SDK,JNI,NDK

This development process is roughly the same as the JNI development process, and I'll explain each step in detail below:

SDK Development, write Java code, call a variety of Android API implementation functions, write code containing native keyword start jni;

JNI development, according to the JNI coding specification, write native code that interacts with Java, generally is the conversion of data type, the data class of C + + is converted into Java can recognize, or vice versa. Also because of this, I think JNI is actually adapter, as the data transfer layer exists, the specific JNI general operation, I then share;

D/C + + development, coding to implement business logic, or to invoke the NDK to provide local APIs or libraries to complete the development of specific features on the Android platform, Packaging;

The NDK compiles, writes. mk files, compiles and debugs, and finally modifies. mk Files to optimize the compilation results for specific platforms (ARM/X86);

Finally, the SDK compiled, packaged, on the real machine debugging ...

The NDK, SDK, and JNI relationships

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.