gpt4 book ai didi

java - 在android中使用JNI对 'multiply'的 undefined reference

转载 作者:行者123 更新时间:2023-11-30 18:12:55 25 4
gpt4 key购买 nike

我是 android JNI 的新手,正在尝试以下代码:

包名称: com.example.jnitest

Java 类:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainActivity mainActivity = new MainActivity();


System.out.println("Sum of two variables : "+ sumOfTwovariable((int)mainActivity.multiply(10, 25), 10));
}

public native long sumOfTwovariable(int v1, int v2);

static {
System.loadLibrary("JNITest");
}

public native long multiply(int v1, int v2);
static{
System.loadLibrary("com_example_pdemo_MainActivity");
}

}
<小时/>

Android.mk 文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := JNITest
LOCAL_SRC_FILES := JNITest.cpp
LOCAL_SHARED_LIBRARIES := Prebuild_com_example_pdemo_MainActivity
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/includes
LOCAL_STATIC_LIBRARIES := Prebuild_com_example_pdemo_MainActivity
include $(BUILD_SHARED_LIBRARY)


include $(CLEAR_VARS)
LOCAL_MODULE := Prebuild_com_example_pdemo_MainActivity
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libcom_example_pdemo_MainActivity.so

include $(PREBUILT_SHARED_LIBRARY)

我已将预构建的 com_example_pdemo_MainActivity.so lib 放置在 JNI->armeabi(写入多个函数)文件夹中,包含在 Android.mk 文件中

<小时/>

JNI 内部:

使用以下命令创建头文件:javah -jni -classpath....

Header file created using javah : com_example_jnitest_MainActivity.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_jnitest_MainActivity */

#ifndef _Included_com_example_jnitest_MainActivity
#define _Included_com_example_jnitest_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_jnitest_MainActivity
* Method: sumOfTwovariable
* Signature: (II)J
*/
JNIEXPORT jlong JNICALL Java_com_example_jnitest_MainActivity_sumOfTwovariable(JNIEnv *, jobject, jint, jint);

#ifdef __cplusplus
}
#endif

#endif

原生函数的定义:JNITest.cpp

  #include "com_example_jnitest_MainActivity.h"

extern "C" {
long multiply(int val1,int val2);
}

JNIEXPORT jlong JNICALL Java_com_example_jnitest_MainActivity_sumOfTwovariable(
JNIEnv *env, jobject obj, jint val1, jint val2) {
int val=multiply(val1,val2);
return (val+val1);

}

当我编译它时,我得到了对“乘法”的 undefined reference 。让我知道我犯了什么错误。

提前致谢..

最佳答案

multiply 也是 native 方法。它的定义应与 sumOfTwovariable 相同:

JNIEXPORT jlong JNICALL    Java_com_example_jnitest_MainActivity_multiply(JNIEnv *, jobject, jint, jint);

关于java - 在android中使用JNI对 'multiply'的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29115193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com