从 Java 文件调用 C++ 文件中的 C++ 方法时出现 UnsatisfiedLinkError [英] UnsatisfiedLinkError when calling C++ method in C++ file from Java file

查看:25
本文介绍了从 Java 文件调用 C++ 文件中的 C++ 方法时出现 UnsatisfiedLinkError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来这是一个流行的问题,

It looks like it is the popular problem,

我还是没有找到解决办法.

And I still not find out the solution.

包名: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

I run application but still get the error exception UnsatisfiedLinkError : getString

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

People who know the how to fix this problem,

请告诉我,

谢谢

更新关注@dextor 的回答.对不起,因为我弄错了.对于这个问题,我唯一需要的是从 public native int getString() 更改为 public native String getString().

UPDATE Follow @dextor answer. Sorry because I get the mistake. Only thing I need for this question is change from public native int getString() to public native String getString().

现在可以了.

推荐答案

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

Not sure (didn't actually try), but the only wrong thing I've noticed is the return type of your method declarations.

Java 端

public native int getString()

NDK 端

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

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

In Java, you have an int. On the C-side, you have a jstring.

这篇关于从 Java 文件调用 C++ 文件中的 C++ 方法时出现 UnsatisfiedLinkError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆