UnsatisfiedLinkError when calling C++ method in C++ file from Java file

别来无恙 提交于 2019-11-30 09:44:33

问题


It looks like it is the popular problem,

And I still not find out the solution.

package name : app.cloudstringers

Java file : 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++ file : 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 file

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

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

People who know the how to fix this problem,

Please tell me,

Thanks

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().

It works now.


回答1:


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

Java-side

public native int getString()

NDK-side

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

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



来源:https://stackoverflow.com/questions/22607042/unsatisfiedlinkerror-when-calling-c-method-in-c-file-from-java-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!