UnsatisfiedLinkError when calling C++ method in C++ file from Java file
Asked Answered
P

1

1

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.

Psaltery answered 24/3, 2014 at 10:42 Comment(3)
Post enough of the stack trace to include the messages generated by your attempt to load the library, and the full unsatisfied link error. Also provide the name of the Java file in which the native method is defined.Petronel
And make sure to include e.printStackTrace() in your catch blocks so that you get meaningful error reports.Petronel
try-catch of System.loadLibrary("ffmpeg"); not run. It's okay.Psaltery
P
2

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.

Pericynthion answered 24/3, 2014 at 14:0 Comment(1)
Yes, that's definitely going to cause problems before the code works, though it may not cause the problem which is posted.Petronel

© 2022 - 2024 — McMap. All rights reserved.