Unsatisfiedlinkerror in android (eclipse)

后端 未结 3 1923
北恋
北恋 2021-01-01 00:36

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 androi         


        
相关标签:
3条回答
  • 2021-01-01 00:59

    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.

    0 讨论(0)
  • 2021-01-01 01:05

    This isn't really been called quite right...

    Try:

    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;
    
        public native String sniff();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            a=new TextView(this);
    
            String b = sniff(); 
    
            a.setText(b);
    
            setContentView(a);
        }
    
        static{
            System.loadLibrary("native");
        }
    
    }
    

    Lastly... is this in your Android.mk?

    LOCAL_MODULE    := native
    
    0 讨论(0)
  • 2021-01-01 01:12

    I will give an another advice.I got this same error before but I solved this problem via "Android Native Development Kit Cookbook".Please note these statements;

    The native function must follow a specific pattern for a package name, class name, and method name.The package and class name must agree with the package and class name of the Java class from which the native method is called, while the method name must be the same as the method name declared in that Java class. This helps the Dalvik VM to locate the native function at runtime.Failing to follow the rule will result in UnsatisfiedLinkError at runtime.

    For example for above

    You need to change your function name like (don't use com.bla in the package names if you focus on NDK)

    #include<iostream>
    #include<string.h>
    #include<jni.h>
    
    JNIEXPORT jstring JNICALL Java_lipcap_example_MainActivity_sniff
    (JNIEnv *env, jobject obj){
           return env->NewStringUTF("This is Native");
    }
    
    0 讨论(0)
提交回复
热议问题