gpt4 book ai didi

android - 从 Java 文件调用 C++ 文件中的 C++ 方法时出现 UnsatisfiedLinkError

转载 作者:行者123 更新时间:2023-11-30 04:07:09 25 4
gpt4 key购买 nike

看来是普遍问题,

我仍然没有找到解决方案。

包名 : app.cloudstringers

Java 文件:Completed.java

static {
try {
System.loadLibrary("ffmpeg");
} catch (UnsatisfiedLinkError e) {
Log.d("", "Error : " + e.toString());
}

}

// Define native method
public native int getString();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_completed);

// Call native method
Log.d("", "" + getString());

C++ 文件:ffmpeg.cpp

#include <jni.h>
#include <android/log.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jstring JNICALL Java_app_cloudstringers_Completed_getString(JNIEnv* env, jobject thiz)
{
jstring strRet = env->NewStringUTF("HelloWorld from JNI !");
return strRet;
}

#ifdef __cplusplus
}
#endif

Android.mk文件

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := ffmpeg.cpp
include $(BUILD_SHARED_LIBRARY)

我运行应用程序但仍然收到错误异常 UnsatisfiedLinkError : getString

知道如何解决这个问题的人,

请告诉我,

谢谢

更新按照@dextor 回答。对不起,因为我弄错了。对于这个问题,我只需要将 public native int getString() 更改为 public native String getString()

现在可以了。

最佳答案

不确定(实际上没有尝试),但我注意到的唯一错误是方法声明的返回类型。

Java 端

public native int getString()

NDK 端

JNIEXPORT jstring JNICALL Java_app_cloudstringers_Completed_getString(JNIEnv* env, jobject thiz)

在 Java 中,您有一个 int。在 C 端,您有一个 jstring

关于android - 从 Java 文件调用 C++ 文件中的 C++ 方法时出现 UnsatisfiedLinkError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22607042/

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