Skip to content Skip to sidebar Skip to footer

Unsatisfiedlinkerror For Native Cpp Function In Android App (ndk)

I am trying to run an easy Android ndk app in cpp, but I get UnsatisfiedLink Error for the Generate() function. Any help would be appreciated. I am quite fluent in c++, but my jav

Solution 1:

You are mostly likely running into C++ name mangling. For example, here is what objdump -T on the library gives when I build hello-jni.c:

00000c28 g DF .text 0000002c Java_com_example_hellojni_HelloJni_stringFromJNI

And here's what I get if I translate to C++ in the way you did:

00000c94 g DF .text00000024 _Z48Java_com_example_hellojni_HelloJni_stringFromJNIP7_JNIEnvP8_jobject

To prevent the mangling and make them visible to jni, declare your native functions within an extern "C" {} block, ie

#include<string.h>#include<jni.h>extern"C" {
    jstring Java_com_optimuse_app_OptimuseAppActivity_generate(JNIEnv* env, 
                                                               jobject thiz);
}

jstring Java_com_optimuse_app_OptimuseAppActivity_generate(JNIEnv* env, 
                                                           jobject thiz){
    return env->NewStringUTF("Hello from JNI !");
}

Solution 2:

You have probably tried these, but anyways it looked very similar to your question and I thought it might help:

How to resolve the java.lang.UnsatisfiedLinkError in NDK in Android?

android ndk UnsatisfiedLinkError when using a prebuilt shared library

Solution 3:

Excellent! It works like this:

extern "C" {
    JNIEXPORT jstring JNICALL Java_com_optimuse_app_OptimuseAppActivity_generate(JNIEnv* env, jobject thiz) {

        returnenv->NewStringUTF("Hello from JNI cpp!");
        //return (*env)->NewStringUTF(env, "Hello from JNI cpp!");

    }
}

Post a Comment for "Unsatisfiedlinkerror For Native Cpp Function In Android App (ndk)"