Android development NDK programming

Android NDK is a set of development kits that allow developers to embed native code into Android applications. As we all know, Android applications run on the Dalvik virtual machine, and NDK allows developers to use C/C++ language for some functions in Android applications. To achieve this, and compile this part of C/C++ code into native code that can be run directly on the Android platform. These native codes exist in the form of a dynamic link library (.so file). This feature of NDK is not only conducive to code reuse, but also can improve the efficiency of the program to some extent.

NDK provides the following content:

1. A set of tools is provided, which can generate native code from C/C++ source code;

2. C header files (*.h) used to define NDK interfaces and library files that implement these interfaces;

3. A set of compilation system that can generate object files with very few configurations;

4. Starting from Android 2.3, native Activity (Native Activity) is supported.

The latest version of Android NDK supports ARMv5TE machine instructions, and provides a large number of C language libraries, including libm (Math library), OpenGL ES, JNI interface and other libraries.

Although the use of NDK in the program can improve the running speed, the use of NDK will also bring many side effects. For example, the use of NDK does not always improve the performance of the application, but it increases the complexity of the program, and the use of NDK must be controlled by yourself The allocation and release of memory will not be able to use the Dalvik virtual machine to manage memory, and it will also bring great risks to the application. Therefore, it is recommended that NDK should be used appropriately according to the specific situation. For example, when performance needs to be greatly improved or confidentiality is required (object files generated by java can be easily decompiled), NDK can be used to generate corresponding native code.

package mobile.android.jni;

......

public class HelloWorldJni extends Activity 

{

public void oncreat(Bundle savedInstanceState){
......

stringFromJNI();

}

public native Strign stringFromJNI();

static{

System.loadLibrary("hello-jni");

}

}


JNI part:


#include <jni.h>

jstring Java_mobile_android_jni_HelloWorldJni_stringFromJNI(JNIEnv* env,jobject thiz)

{

return (*env)->NewStringUTF(env , "hello world");

}

Among them, "Java_mobile_android_jni_HelloWorldJni_stringFromJNI" is the NDK function name, and the naming rules for the function name and its parameters are as follows.

The return value types and parameter types of NDK functions are the types defined in the JNI header file (such as string, jobject, etc.). These types correspond to data types in java. For example, string corresponds to String in java, and jobject corresponds to Object in java. These data types must be used when defining JNI functions called by java, otherwise java cannot successfully call these functions.

从HelloWorldJni类的代码可以看出,调用的方法是stringFromJNI。而NDK源码中的方法却是Java_mobile_android_jni_HelloWorldJni_stringFromJNI。实际上,这里涉及到一个命名规则。JNI方法名的命名规则是Java_xxx_MethodName。从这个命名规则可以看出,JNI方法名分三段,中间是下划线“_”分割。第一段是“Java”,这是固定的,最后一段就是在Java中调用实际方法名,而中间一段“xxx”实际上就是调用JNI方法的类的全名(package + classname),只是将中间的点“.”换成了下划线“_”,所以最终是JNI方法名是Java_mobile_android_jni_HelloWorldJni_stringFromJNI。

上面两个参数:env 和 thiz 。这两个参数必须包含在JNI函数中,而且必须是头两个参数。其中env表示JNI的调用环境,thiz标识定义native方法的java类的对象本身。

Guess you like

Origin blog.csdn.net/xhf_123/article/details/50095575