Android 开发中有时需要调用本地方法,这就需要注册本地方法,而注册的方法有两种:
- RegisterNatives注册
- 使用javah生成规范的头文件,然后编写对应的源文件。
先看一下java文件 Hello.java,这里面有一个native方法、一个平常方法。
package cn.vn.jniregisternative;
public class Hello {
static{
System.loadLibrary("Hello");
}
private native String natvie_hello();
public String hello(){
return natvie_hello();
}
}
1 使用RegisterNatives。
使用RegisterNatives注册,就需要写JNI_OnLoad方法。文件名为Hello.cpp(不限制文件名)。
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
static jstring jni_hello(JNIEnv *env, jobject thiz){
jstring str = env->NewStringUTF("hello jni RegisterNatives");
return str;
}
static JNINativeMethod gMethods[] = {
{ "natvie_hello", "()Ljava/lang/String;", (jstring *)jni_hello },
};
jint JNI_OnLoad(JavaVM* vm, void* reserved){
JNIEnv* env = NULL;
jint result = -1;
jclass clazz;
if ((*vm).GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
return result;
}
assert(env != null);
clazz = (*env).FindClass("cn/vn/jniregisternative/Hello");
if (clazz == NULL) {
return result;
}
// 注册native方法到java中
if ((*env).RegisterNatives(clazz, gMethods, sizeof(gMethods)/sizeof(gMethods[0])) < 0) {
return result;
}
// 返回jni的版本
return JNI_VERSION_1_4;
}
Android.mk如下
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Hello
LOCAL_SRC_FILES := Hello.cpp
include $(BUILD_SHARED_LIBRARY)
2 使用javah。
使用javah方式,对方法名有一定的要求。
命令行进入该工程的\bin\classes目录下面,确保已经生成了Hello.java对应的Hello.class文件。如果没有就在eclipse 上点击工程 build project。
在此目录下面输入:
javah -jni cn.vn.jniregisternative.Hello
如下图所示
然后会在该目录下生成 头文件。
将该文件拷贝到jni目录下面。
cn_vn_jniregisternative_Hello.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class cn_vn_jniregisternative_Hello */
#ifndef _Included_cn_vn_jniregisternative_Hello
#define _Included_cn_vn_jniregisternative_Hello
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: cn_vn_jniregisternative_Hello
* Method: natvie_hello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_cn_vn_jniregisternative_Hello_natvie_1hello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
编写对应的cpp文件。
cn_vn_jniregisternative_Hello.cpp
#include "cn_vn_jniregisternative_Hello.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: cn_vn_jniregisternative_Hello
* Method: natvie_hello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_cn_vn_jniregisternative_Hello_natvie_1hello
(JNIEnv *env, jobject thiz){
jstring str = env->NewStringUTF("hello jni hello");
return str;
}
#ifdef __cplusplus
}
#endif
该方式不需要调用RegisterNatives方法。
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Hello
LOCAL_SRC_FILES := cn_vn_jniregisternative_Hello.cpp
include $(BUILD_SHARED_LIBRARY)
编译过程
09:44:22 **** Build of configuration Default for project JniRegisterNative ****
"G:\\tools\\Android\\android-ndk-r10e\\ndk-build.cmd" all
Android NDK: WARNING: APP_PLATFORM android-21 is larger than android:minSdkVersion 8 in ./AndroidManifest.xml
[armeabi] Install : libHello.so => libs/armeabi/libHello.so
09:44:22 Build Finished (took 345ms)