java.lang.UnsatisfiedLinkError:由于库未加载,未找到任何实现

时间:2016-06-27 20:40:42

标签: java android android-ndk java-native-interface jniwrapper

我正在尝试将本机编写的共享库集成到我的应用程序中,因为它说jniLibs和其他库中的.so文件无法加载。

这是C文件

#include <string.h>
#include <jni.h>
#include <dummy.h>

JNIEXPORT jstring
Java_com_example_hellojni_HelloJni_DummyInit(JNIEnv*env, jobject thiz) {

    dummy *handle;
    char *msg;
    int rc = dummy_init_from_id("ml", &handle, &msg);
    if (rc == DUMMY_SUCCESS) {
        return (*env)->NewStringUTF(env, "Init was successful");
    } else {
        return (*env)->NewStringUTF(env, msg);
    }

}

JNIEXPORT void 
Java_com_example_hellojni_HelloJni_DummySetSymbolsDir(JNIEnv *env, jobject thiz,
                                                      jstring dir) {
    dummy_set_symbols_dir(dir);

}

这是Java文件

package com.example.hellojni;

import android.app.Activity;
import android.util.Log;
import android.widget.TextView;
import android.os.Bundle;
import java.io.File;


public class HelloJni extends Activity
{


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);

        File f = new File(getAssets() + "/ml");
        if (f.exists()) try {

            System.out.println("Scheme exists");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        String dir = f.getPath();

        DummySetSymbolsDir(dir);

        TextView tv = new TextView(this);
        tv.setText(DummyInit());
        setContentView(tv);
    }


    /*** Native Methods ***/
    public native String DummyInit();
    public native void DummySetSymbolsDir(String dir);

    static {
        try {
            System.loadLibrary(("dummy"));
        } catch (UnsatisfiedLinkError e) {
            Log.e("JNI", "Warning:Could not use dummy library");
        }
        try {
            System.loadLibrary("libs/libpthead.so");
        } catch (UnsatisfiedLinkError e) {
            Log.e("JNI", "Warning:Could not use lipthread library");
        }
        try {
            System.loadLibrary("hello-jni");
        } catch (UnsatisfiedLinkError e) {
            Log.e("JNI", "Warning:Could not use hello-jni library");
        }
    }
}

下面给出了确切的错误

FATAL EXCEPTION: main
Process: com.example.hellojni, PID: 2558
java.lang.UnsatisfiedLinkError: No implementation found for void com.example.hellojni.HelloJni.DummySetSymbolsDir(java.lang.String) (tried Java_com_example_hellojni_HelloJni_DummySetSymbolsDir and Java_com_example_hellojni_HelloJni_DummySetSymbolsDir__Ljava_lang_String_2)
at com.example.hellojni.HelloJni.DummySetSymbolsDir(Native Method)
at com.example.hellojni.HelloJni.onCreate(HelloJni.java:34)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Android.mk文件如下

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)

如果需要更多信息,请告诉我。 感谢。

2 个答案:

答案 0 :(得分:0)

您可能需要使用extern&#34; C&#34;块:

extern "C" {
    Your JNIEXPORT functions
}

对于C和C ++,你可以这样做:

#ifdef __cplusplus
extern "C" {
#endif

    Your JNIEXPORT functions

#ifdef __cplusplus
}
#endif

答案 1 :(得分:0)

将代码放入try catch块(或类似的东西)中检索文件。此外,在将代码定向到文件时,请确保整个目录以C:开头(在Windows中)。