Unsatisfiedlinkerror in android (eclipse)

13,331

Richard-head you mentioned: "Changing code from C++ to C everything works fine"...

I was tortured by the exact same problem for several days and I did make sure everything typed by me (naming, Android.mk etc.) has no problem. Whenever in C, I'm fine. As long as I change to cpp, UnsatisfiedLinkError.

I finally got the hint from this link: http://markmail.org/message/fhbnprmp2m7ju6lc

It's all because of the C++ name mangling! The same function, if you don't have extern "C" surrounding it in the .cpp file, the name is mangled so JNI can not find the function name so UnsatisfiedLinkError pops up.

Put on and remove the extern "C" { } around your functions, run nm obj/local/armeabi/libnative.so, you will clearly see the same function without and with name mangling.

I hope this helps others with the same problem too.

Share:
13,331
d34th4ck3r
Author by

d34th4ck3r

Updated on June 09, 2022

Comments

  • d34th4ck3r
    d34th4ck3r almost 2 years

    I am trying to run a simple jni code in Android, But all I am getting Unsatisfiedlinkerror .

    Here is my Java code:

    package com.lipcap;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    
    TextView a;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        a=new TextView(this);
    
        String b; 
        MainActivity ob=new MainActivity();
        b=ob.sniff();
    
        a.setText(b);
    
        setContentView(a);
    }
    public native String sniff();
    
        static{
            System.loadLibrary("native");
        }
    
    
    } 
    

    And here is My C++ code(in $PROJECT_PATH/jni/):

    #include<iostream>
    #include<string.h>
    #include<jni.h>
    JNIEXPORT jstring JNICALL Java_com_lipcap_MainActivity_sniff
    (JNIEnv *env, jobject obj){
           return env->NewStringUTF("This is Native");
    }
    

    I have complied java code using javac, and made the header using javah.

    Then I ran ndk-build. And then I ran code from eclipse.(installed apk in android).

    I get this error:

    E/AndroidRuntime(  769): FATAL EXCEPTION: main
    E/AndroidRuntime(  769): java.lang.UnsatisfiedLinkError: sniff
    E/AndroidRuntime(  769):    at com.lipcap.MainActivity.sniff(Native Method)
    E/AndroidRuntime(  769):    at com.lipcap.MainActivity.onCreate(MainActivity.java:36)
    E/AndroidRuntime(  769):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    E/AndroidRuntime(  769):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    E/AndroidRuntime(  769):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    E/AndroidRuntime(  769):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    E/AndroidRuntime(  769):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    E/AndroidRuntime(  769):    at android.os.Handler.dispatchMessage(Handler.java:99)
    E/AndroidRuntime(  769):    at android.os.Looper.loop(Looper.java:123)
    E/AndroidRuntime(  769):    at android.app.ActivityThread.main(ActivityThread.java:4627)
    E/AndroidRuntime(  769):    at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime(  769):    at java.lang.reflect.Method.invoke(Method.java:521)
    E/AndroidRuntime(  769):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    E/AndroidRuntime(  769):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    E/AndroidRuntime(  769):    at dalvik.system.NativeStart.main(Native Method)
    

    I have not set LD_LIBRARY_PATH.

    However,without setting LD_LIBRARY_PATH sample code such as HelloJNI provided by NDK runs absolutely fine.

    Please tell me where i am missing.

    • Francesco Laurita
      Francesco Laurita almost 13 years
      MainActivity ob=new MainActivity(); Why? you are already into an instance of MainActivity. this.sniff() must be used.
    • d34th4ck3r
      d34th4ck3r almost 13 years
      Yea, this.sniff could be used. Anyway, this does not create any difference as far as Unsatisfiedlinkerror is concerned.
    • d34th4ck3r
      d34th4ck3r almost 13 years
      Changing code from C++ to C everything works fine.. :)
  • Maximus
    Maximus almost 13 years
    Check the edit... the module name in the Android.mk should match the library name you're loading.
  • d34th4ck3r
    d34th4ck3r almost 13 years
    Moreover, Order of declaration doesnt matter in Java.
  • d34th4ck3r
    d34th4ck3r almost 13 years
    Yes.. this is in my Android.mk LOCAL_MODULE := native