UnsatisfiedLinkError when calling method from nati

2019-02-27 19:15发布

问题:

I have an Android Studio 1.2.2 project setup with some native *.so files included in project -> src -> main -> jniLibs -> armeabi (that's the correct architecture for them). I have a class that loads the library in a static and links to the native methods (I think/hope).

Here is the static with System.loadLibrary:

static {
    try {
        System.loadLibrary("<SOME LIB>");
    } catch(UnsatisfiedLinkError ule){
        Log.e(TAG, "Error while loading library <SOME LIB>", ule);
    }
}

I never hit the catch block or see the error message but I have verified I enter the static and get to the System.loadLibrary line so I don't assume I'm hitting an issue loading it.

I have this imported in another class:

import com.<rest-of-pacakage-name>.<SOME LIB>

and has a native method initialize that takes a byte:

public native static int <SOME LIB>_Initialize(byte[] Parameter);

This was all based off of another eclipse project that I'm porting over the AS.

Any ideas/suggestions or ways I can identify the root cause of why the library load seems to be fine but the method call not?

 07-22 08:38:51.810  27119-27119/com.<rest-of-package-name>.staging E/art﹕ No implementation found for int com.<rest-of-package-name>.pppp_api.PPPP_APIs.PPPP_Initialize(byte[]) (tried Java_com_<rest-of-package-name>_pppp_1api_PPPP_1APIs_PPPP_1Initialize and Java_com_<rest-of-package-name>_pppp_1api_PPPP_1APIs_PPPP_1Initialize___3B)
07-22 08:38:52.360  27119-27119/com.<rest-of-package-name>.staging E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.<rest-of-package-name>.staging, PID: 27119
    java.lang.UnsatisfiedLinkError: No implementation found for int com.<rest-of-package-name>.pppp_api.PPPP_APIs.PPPP_Initialize(byte[]) (tried Java_com_<rest-of-package-name>_pppp_1api_PPPP_1APIs_PPPP_1Initialize and Java_com_<rest-of-package-name>_pppp_1api_PPPP_1APIs_PPPP_1Initialize___3B)
            at com.<rest-of-package-name>.pppp_api.PPPP_APIs.PPPP_Initialize(Native Method)
            at com.<rest-of-package-name>.ui.base.BaseDrawerActivity.setupDrawer(BaseDrawerActivity.java:150)
            at com.<rest-of-package-name>.ui.base.BaseDrawerActivity.onCreate(BaseDrawerActivity.java:99)
            at com.<rest-of-package-name>.ui.main.MainActivity.onCreate(MainActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:6221)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5835)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Package name for class that contains the static and native method:

package com.<rest-of-package-name>.pppp_api;

回答1:

1) If you change package name for class that contains native methods, you should change method signature in native code.

2) Try create one more fodler with name armeabi-v7a and copy *.so there because some devices can't use old armeabi and ignore libs in that fodler

3) in module graddle script add this lines inside "android" block:

sourceSets.main {
    jni.srcDirs = []
    jniLibs.srcDir 'src/main/jniLibs'
}