Unsatisfiedlinkerror in android (eclipse)

I am trying to run simple jni code in Android, but all I get is 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 executed java code using javac and made a header using javah.

Then I launched ndk-build. And then I ran the 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 did not set LD_LIBRARY_PATH.

However, without setting the LD_LIBRARY_PATH sample code, such as HelloJNI provided by the NDK, it is absolutely normal.

Please tell me where I am absent.

+6
source share
3 answers

Richard, the leader you mentioned: "Changing code from C ++ to C, everything works fine" ...

For several days I was tortured on the same problem, and I was convinced that everything I entered (names, Android.mk, etc.) had no problems. Whenever I'm in C, I'm fine. So far I am switching to cpp, UnsatisfiedLinkError .

I finally got a hint at this link: http://markmail.org/message/fhbnprmp2m7ju6lc

All this is due to the fact that the name of the language is C ++! The same function, if you do not have extern "C" surrounding it in the .cpp file, the name is distorted, so JNI cannot find the name of the function, so UnsatisfiedLinkError appears.

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

I hope this helps others with the same problem.

+12
source

In fact, this is not entirely correct ...

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"); } } 

Finally ... is it in your Android.mk?

 LOCAL_MODULE := native 
0
source

I will give one more tip. I got the same error before, but I solved this problem through the “Android Native Development Kit Cookbook”. Pay attention to these statements;

The native function must follow a specific pattern for the package name, class name, and method name. The name of the package and class must match the name of the package and Java class from which the native method is called, and the method name must match the name of the method declared in this Java class. This helps the Dalvik virtual machine find the built-in function at runtime. Following the rule will result in an UnsatisfiedLinkError at runtime.

For example above

You need to change your function name (do not use com.bla in 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
source

Source: https://habr.com/ru/post/890549/


All Articles