Search code examples
androidc++android-ndkunsatisfiedlinkerrorandroid.mk

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


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.


Solution

  • 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.