Android之NDK开发的简单实例

NDK全称为Native Development Kit,是本地开发工具集。在Android开发中,有时为了能更好的重用以前的C/C++的代码,需要将这些代码编译成相应的so,然后通地JNI以供上层JAVA调用。当然,也有的是为了更高的保护性和安全性。下面是实现的过程。

1、下载NDK TOOL

可以从http://developer.android.com/tools/sdk/ndk/index.html下载NDK TOOL,我下的是Windows 64-bit。


下载完后,直接安装,路径建议在根目录下,比如D:android-ndk-windows,然后将该路径设置成环境变量。

2、测试下ndk的示例

NDK TOOL中自带了很多例子,可以用ECLIPSE导入,示例位置在D:android-ndk-windowssamples。刚刚的下载网站后面有示例的说明(见下图)。


注:

(1)、<ndk-root>指的就是ndk的根目录,D:android-ndk-windows.

(2)、jni文件夹是必须的,如果自己新建项目,一定要记得把C代码和mk文件放到jni文件夹下。

(3)、打开CMD命令行,定位到工程所在的jni文件夹,直接输入ndk-build回车即可生成相应的so.如果没有将D:android-ndk-windows设置成环境变量,需要输入D:android-ndk-windows dk-build。

(4)、执行完后,会在lib下的armeabi和armeabi-v7a下生成*.so文件。见下图。


3、新建一个Android工程

(1)、在eclipse中建一个SharedObjectTest的Android工程,MainActivity.java的代码如下:

MainActivity.java

package com.ex.sot;

import android.app.Activity;
import android.os.Bundle;

import com.example.sumcalculator.R;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);

		int cs = NativeMachine.calculate(1,2);	
		setTitle(cs + "");
	}

}

(2)、增加一个NativeMachine.java的类。代码如下:

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调用

(1)、执行genHeader.bat脚本,生成相应的头。

javah -classpath ../src com.ex.sot.NativeDataManage

(2)、调用的实现

com_ex_sot_NativeMachine.c

#include "Machine.h"
#include "com_ex_sot_NativeMachine.h"
extern void* MachineNew();
JNIEXPORT jint JNICALL
	Java_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、用C实现功能

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 <malloc.h>
#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 */

注:lw_oopc.h是c的面向对象实现,即常说的oopc。

5、编译动态库

定位到工程目录下,执行ndk-build,生成*.so。

注:Android.mk和Application.mk是固定的名称。其中Android.mk中的LOCAL_MODULE、LOCAL_SRC_FILES、LOCAL_CFLAGS是需要作相应的修改。

Android.mk(部分代码)

LOCAL_MODULE := NativeMachine

LOCAL_SRC_FILES := Machine.c

ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
    LOCAL_CFLAGS := -DHAVE_MACHINE=1
    LOCAL_SRC_FILES += com_ex_sot_NativeMachine.c
endif

6、安装APK并运行


源码下载

转载请注明出处:

原文地址:https://www.cnblogs.com/sparkleDai/p/7605015.html