我正在尝试使用 jni 构建一个演示应用程序(我是新手)。我已经用谷歌搜索了这个问题,但没有任何帮助。
下面是我正在使用的代码:
include $(CLEAR_VARS)
LOCAL_MODULE := example
LOCAL_SRC_FILES := com_example_example_Operacoes.c
include $(BUILD_SHARED_LIBRARY)
Operacoes.java 包 com.example.example;
public class Operacoes {
static {
System.loadLibrary("example");
}
//Versao nativa do metodo soma
public static native int soma(int a, int b);
//Versao nativa do metodo subtracao
public static native int sub(int a, int b);
}
com_example_example_Operacoes.h
# include "com_example_example_Operacoes.c"
JNIEXPORT jint JNICALL Java_com_example_example_Operacoes_soma (JNIEnv *, jclass, jint, jint);
int soma(int a, int b){
return a + b;
}
JNIEXPORT jint JNICALL Java_com_example_example_Operacoes_sub (JNIEnv *, jclass, jint, jint);
int sub(int a, int b){
return a - b;
}
com_example_example_Operacoes.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_example_Operacoes */
#ifndef _Included_com_example_example_Operacoes
#define _Included_com_example_example_Operacoes
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_example_Operacoes
* Method: soma
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_example_example_Operacoes_soma (JNIEnv *, jclass, jint, jint);
/*
* Class: com_example_example_Operacoes
* Method: sub
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_example_example_Operacoes_sub (JNIEnv *, jclass, jint, jint);
#ifdef __cplusplus
}
#endif
#endif
最佳答案
您没有实现这些功能。请注意您的第一行(如下),该行末尾有一个分号。然后,您创建具有不同格式的第二个函数 int soma(int,int)
...Java_com_example_example_Operacoes_soma (JNIEnv *, jclass, jint, jint);<<<wrong
您需要使用与声明完全相同的格式来实现 native 函数。
JNIEXPORT jint JNICALL Java_com_example_example_Operacoes_soma (JNIEnv * env, jclass clazz, jint a, jint b)
{
return a + b;
}
关于java.lang.UnsatisfiedlinkError : Native method not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21935913/