虽然System.loadLibrary似乎没问题,但从本机库调用方法时出现UnsatisfiedLinkError

时间:2015-07-22 13:17:26

标签: android android-studio android-ndk nativelibrary

我有一个Android Studio 1.2.2项目设置,其中包含项目中包含的一些原生* .so文件 - > src - >主要 - > jniLibs - > armeabi(这是他们的正确架构)。我有一个类,它以静态方式加载库,并链接到本机方法(我认为/希望)。

以下是System.loadLibrary的静态:

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

我从未点击catch块或看到错误消息,但我已经验证我输入静态并进入System.loadLibrary行,所以我不认为我遇到了加载它的问题。

我将其导入另一个类:

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

并且有一个本机方法初始化,需要一个字节:

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

这完全基于我正在移植到AS上的另一个eclipse项目。

任何想法/建议或方法我可以确定为什么库加载似乎没问题的根本原因但方法调用不是?

 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 com.<rest-of-package-name>.pppp_api;

1 个答案:

答案 0 :(得分:4)

1)如果更改包含本机方法的类的包名,则应更改本机代码中的方法签名。

2)尝试创建另一个名为armeabi-v7a的fodler并复制* .so因为某些设备无法使用旧的armeabi并忽略该文件中的库

3)在模块graddle脚本中将这些行添加到“android”块中:

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