Current location - Quotes Website - Signature design - Introduction to Android NDK Development What is the relationship between NDK, SDK and JNI_What does Android ndk do?
Introduction to Android NDK Development What is the relationship between NDK, SDK and JNI_What does Android ndk do?

NDK: Android NDK adds the word "native" in front of the SDK, that is, Native Development Kit, so it is also called "NDK" by Google.

NDK full name: NativeDevelopmentKit.

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. These tools are of great help to developers.

*NDK integrates a cross-compiler and provides corresponding mk files to isolate differences in CPU, platform, ABI, etc. Developers only need to simply modify the mk file (point out "which files need to be compiled", "compile Feature requirements", etc.), you can create so.

*NDK can automatically package so and Java applications together, greatly reducing the packaging work of developers.

Actually:

NDK is a tool that can easily and quickly develop .so files. The process of JNI is relatively complicated, and generating .so requires a lot of operations, but NDK simplifies this process.

AndroidSDK:

SDK (software development kit) software development kit. A collection of development tools used by software development engineers to build application software for a specific software package, software framework, hardware platform, operating system, etc. therefore! AndroidSDk refers to Android-specific software development kit

JNI:

The JavaNativeInterface (JNI) standard is part of the Java platform, which allows Java code to interact with code written in other languages. . JNI is a native programming interface that enables Java code running inside a Java Virtual Machine (VM) to interoperate with applications and libraries written in other programming languages ??such as C, C, and assembly language

Of course, the following operation process is generally required:

1) Write a java program: Here, take HelloWorld as an example. In order to call the c function printf in java code.

Code 1:

classHelloWorld{

publicnativevoid();

static{

System.loadLibrary( "hello");

}

publicstaticvoidmain(String[]args){

newHelloWorld().();

}

}

Declare a native method: If you want to make a method a native method, then you must declare the method as native, and it cannot be implemented.

Load dynamic library: System.loadLibrary("hello");

This is generally loaded with static blocks. At the same time, it should be noted that the parameter "hello" of System.loadLibrary() is the name of the dynamic library.

2) Compile

javacHelloWorld.java

3) Generate the header file javah with extension h?

JNIEXPORTvoidJNICALLJava_HelloWorld_(JNIEnv* , jobject);

This h file is equivalent to our interface in java. Here we declare a Java_HelloWorld_(JNIEnv*, jobject) method, and then implement this method in our local method, that is to say The method names we use when writing C/C programs must be consistent with the ones here).

4) Write a local method to implement a method with the same name as the method declared in the header file generated by the javah command

Code 2:

#include"jni .h"

#include"HelloWorld.h"

#includeotherheaders

JNIEXPORTvoidJNICALLJava_HelloWorld_(JNIEnv*env,jobjectobj)

{< /p>

printf("Helloworld!/n");

return;

}

Note that line 1 in code 2 requires Import the jni.h (this file can be found under the %JAVA_HOME%/include folder) file, because the JNIEnv, jobject and other types in the program are defined in this header file; in addition, HelloWorld needs to be added in line 2 The .h header file is introduced. Then save it as .c and it will be ok.

5) Generate dynamic library

Here, taking Windows as an example, you need to generate a dll file. Under the save .c folder, use VC's compiler cl. cl-I%java_home%/include-I%java_home%/include/win32-LD.c-Fehello.dll Note: The generated dll file name is configured after the option -Fe, here is hello, because in the HelloWorld.java file The name we use when loadingLibary is hello.

In addition, you need to add the -I%java_home%/include-I%java_home%/include/win32 parameter because the jni.h file was introduced when writing the local method in the fourth step.