我们在加载JNI的.so库的时候,Android Studio报类的错误“java.lang.UnsatisfiedLinkError: JNI_ERR returned from JNI_OnLoad”,该如何解决呢?具体错误内容如下:
java.lang.UnsatisfiedLinkError: JNI_ERR returned from JNI_OnLoad in "/data/app/com.tpyyes.shouxie-8br8qobG6_OylT-eZqypJg==/base.apk!/lib/armeabi-v7a/libdwEngineHw.so"
at java.lang.Runtime.loadLibrary0(Runtime.java:1071)
at java.lang.Runtime.loadLibrary0(Runtime.java:1007)
at java.lang.System.loadLibrary(System.java:1668)
at com.tpyyes.wwengine.hw.WWHandWrite.<clinit>(WWHandWrite.java:26)
at com.tpyyes.shouxie.MainActivity.hwinit(MainActivity.java:107)
at com.tpyyes.shouxie.MainActivity.onCreate(MainActivity.java:67)
at android.app.Activity.performCreate(Activity.java:8121)
at android.app.Activity.performCreate(Activity.java:8109)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3867)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4077)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2458)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
当然了,我们都知道我们错误的代码是出在loadLibrary这一句了:
static { System.loadLibrary("dwEngineHw"); }
错误分析一:
1:出现这个错误的问题,是因为我们loadLibrary这一句代码所在的路径放置不对,例如小编的项目路径如下:
我是放在src/java/com/tpyyes这个包下面的,Android Studio运行时就会造成“java.lang.UnsatisfiedLinkError: JNI_ERR returned from JNI_OnLoad”这种错误。
解决方式:
将wwengine这里面的java类托到src/java/com这个包下,与tpyyes这个包同级即可,如图所示:
错误分析二及其解决方案:
有些人说是因为在app/build.gradle下加入这一串代码即可:
sourceSets { main { jniLibs.srcDirs = ['src/main/libs'] } }
这个确实必须要加入,不加入这几行代码,就会报“java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList”错误,但是仔细看会发现它的错误内容和上面是不一样的,不是小编标题所要解决的内容。