写好的Java文件如下:
package com.example.jnitest;
public class JniProxy {
static {
System.loadLibrary("JniTest");
}
public static native int add(int a, int b);
}
当在Java文件中写好Jni接口后,需要生成相应的.h文件实现这些接口。以下为Windows环境下的实现步骤。
1 设置Jdk环境变量
2 打开Cmd命令框(打开方法:同时输入Win+r,在左下角弹出框中输入cmd,回车)
3 设置路径到Android工程目录下:cd workspace\example\JniTest\bin\classes
4 执行编译命令:javah -jni com.example.jnitest.JniProxy
即可生成.h文件在当前目录下,也就是c:\workspace\example\JniTest\bin\classes下。将其拷贝到工程目录的jni文件夹
生成的.h文件如下
com_example_jnitest_JniProxy.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_jnitest_JniProxy */
#ifndef _Included_com_example_jnitest_JniProxy
#define _Included_com_example_jnitest_JniProxy
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_jnitest_JniProxy
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_example_jnitest_JniProxy_add
(JNIEnv *, jclass, jint, jint);
#ifdef __cplusplus
}
#endif
#endif