Known as Native Development Kit, NDK is a set of local Development tools. In Android development, sometimes in order to better reuse the previous C/C ++ code, you need to compile the code into the corresponding so, and then use JNI for upper-layer JAVA calls. Of course, some are for higher protection and security. The following is the implementation process.
1. Download The NDK TOOL
You can download the ndk TOOL from http://developer.android.com/tools/sdk/ndk/index.html. I use Windows 64-bit.
After the download, install the SDK directly. The path is recommended in the root directory, for example, D: \ android-ndk-windows. Then, set the path to an environment variable.
2. Examples of ndk Testing
Ndk tool comes with many examples, which can be imported using ECLIPSE. The example is located at D: \ android-ndk-windows \ samples. An example is provided after the download website (SEE ).
Note:
(1 ), It refers to the root directory of ndk, D: \ android-ndk-windows.
(2) The jni folder is required. If you create a project by yourself, remember to put the C code and mk file under the jni folder.
(3) Open the CMD command line, locate the jni folder where the project is located, and enter ndk-build and press enter to generate the corresponding so. if you have not set D: \ android-ndk-windows to an environment variable, enter D: \ android-ndk-windows \ ndk-build.
(4) After the execution, the *. so file is generated under armeabi and armeabi-v7a under lib. See.
3. Create an Android Project
(1) Create an Android project named SharedObjectTest in eclipse. The MainActivity. java code is as follows:
MainActivity. java
package com.ex.sot;import android.app.Activity;import android.os.Bundle;import com.example.sumcalculator.R;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main_activity);int cs = NativeMachine.calculate(1,2);setTitle(cs + "");}}
(2) Add a NativeMachine. java class. The Code is as follows:
NativeMachine. java
package com.ex.sot;import android.util.Log;public class NativeMachine {static {try {Log.i("JNI", "Trying to load libNativeJniAdder.so");System.loadLibrary("NativeMachine");} catch (UnsatisfiedLinkError ule) {Log.e("JNI", "WARNING:Gould not load libNativeJniAdder.so");}}public static native int calculate(int digit_1,int digit_2);}
3. JNI call
(1) execute the genHeader. bat script to generate the corresponding header.
javah -classpath ../src com.ex.sot.NativeDataManage
(2) Implementation of the call
Com_ex_sot_NativeMachine.c
#include "Machine.h"#include "com_ex_sot_NativeMachine.h"extern void* MachineNew();JNIEXPORT jint JNICALLJava_com_ex_sot_NativeMachine_calculate(JNIEnv *env,jclass c,jint digit_1,jint digit_2){Machine* hadder=(Machine*)MachineNew();int newSn=hadder->encodeSn(hadder,digit_1);return newSn;}
4. Use C to implement functions
Machine. h
/* HalfAdder.h */#ifndef HMACHINE_H#define HMACHINE_H#include "lw_oopc.h"CLASS(Machine){int sn;int(*encodeSn)(void*,int);};#endif
Machine. c
/* Machine.c */#include "Machine.h"static int encodeSn(void* t,int sn){Machine* cthis=(Machine*)t;cthis->sn=sn;int newSn=cthis->sn*123;return newSn;}CTOR(Machine)FUNCTION_SETTING(encodeSn,encodeSn)END_CTOR/* end */
Lw_oopc.h
/* lw_oopc.h */#ifndef LOOPC_H#define LOOPC_H#include
#define CLASS(type)\typedef struct type type;\struct type#define CTOR(type)\void* type##New()\{\struct type *t;\t=(struct type*)malloc(sizeof(struct type));#define CTOR2(type,type2)\void* type2##New()\{\struct type *t;\t=(struct type *)malloc(sizeof(struct type));#define END_CTOR return (void*)t; };#define FUNCTION_SETTING(f1,f2) t->f1=f2;#define IMPLEMENTS(type) struct type type#define INTERFACE(type) struct type#endif/* end */
Note: lw_oopc.h is the object-oriented Implementation of c, that is, oopc.
5. Compile the dynamic library
Go to the project directory and run ndk-build to generate *. so.
Note: Android. mk and Application. mk are fixed names. In Android. mk, LOCAL_MODULE, LOCAL_SRC_FILES, and LOCAL_CFLAGS must be modified accordingly.
Android. mk (partial code)
LOCAL_MODULE := NativeMachineLOCAL_SRC_FILES := Machine.cifeq ($(TARGET_ARCH_ABI),armeabi-v7a) LOCAL_CFLAGS := -DHAVE_MACHINE=1 LOCAL_SRC_FILES += com_ex_sot_NativeMachine.cendif
6. Install and run the APK
Source code download
Reprinted please indicate the source: