如何在Android的第二个项目中使用.so?

时间:2014-01-23 13:44:23

标签: android java-native-interface native

我在Android中使用JNI的本机代码,我有一个问题。

我用c ++构建了一些代码来创建.so库。然后我在我的项目和我的Nexus 5中使用这个.so工作正常,但当我尝试在其他设备应用程序崩溃时。

我做了很多测试并且失败是由于在我的项目中使用这个.so但我不知道因为在nexus 5中是好的而在其他设备中没有。

这是我的Android.mk:

LOCAL_PATH := $(call my-dir)

# Create `DSP-prebuilt` local prebuilt library from `DSP.so`
include $(CLEAR_VARS)
LOCAL_MODULE            := DSP-prebuilt
LOCAL_SRC_FILES         := lib/libDSP.so
LOCAL_EXPORT_C_INCLUDES := C:\libreriasSISTOLE\SP++3.0\SP++3.0\include
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_SRC_FILES  := nativo.cpp Parameters.cpp Pre_proc_mono.cpp Calc_ToF_low.cpp Runnable.cpp Trilateracion.cpp Tracking.cpp qr_solve.cpp r8lib.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS     += -llog -ldl 
LOCAL_MODULE     := native_code
# Reference the local prebuilt:
LOCAL_SHARED_LIBRARIES := DSP-prebuilt
include $(BUILD_SHARED_LIBRARY)

这是我的logcat:

01-23 14:42:12.556: E/AndroidRuntime(19380): FATAL EXCEPTION: main
01-23 14:42:12.556: E/AndroidRuntime(19380): java.lang.ExceptionInInitializerError
01-23 14:42:12.556: E/AndroidRuntime(19380):    at com.sistoleaudiocapture.Sistole_main.onCreate(Sistole_main.java:62)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at android.app.Activity.performCreate(Activity.java:5244)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2098)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at android.app.ActivityThread.access$600(ActivityThread.java:138)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at android.os.Looper.loop(Looper.java:137)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at android.app.ActivityThread.main(ActivityThread.java:4905)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at java.lang.reflect.Method.invokeNative(Native Method)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at java.lang.reflect.Method.invoke(Method.java:511)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at dalvik.system.NativeStart.main(Native Method)
01-23 14:42:12.556: E/AndroidRuntime(19380): Caused by: java.lang.UnsatisfiedLinkError: Cannot load library: link_image[1891]:   217 could not load needed library 'libDSP.so' for 'libnative_code.so' (load_library[1093]: Library 'libDSP.so' not found)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at java.lang.Runtime.loadLibrary(Runtime.java:370)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at java.lang.System.loadLibrary(System.java:535)
01-23 14:42:12.556: E/AndroidRuntime(19380):    at com.sistoleaudiocapture.Processing.<clinit>(Processing.java:11)
01-23 14:42:12.556: E/AndroidRuntime(19380):    ... 15 more

最后这是我的Processing类,我加载本机库:

package com.sistoleaudiocapture;
import android.util.Log;
public class Processing {

    private long retorno;
    private double prueba[];

    static {
        System.loadLibrary("native_code");
    }

    public Processing() {
        retorno = init_variables();

    }

    public double[] prueba(byte[] data, int lenbytes) {

        prueba = prueba_nativa(retorno, data, lenbytes);

        return (prueba);

    }

    private static native long init_variables();

    private static native double[] prueba_nativa(long retorno, byte[] data,
            int lenbytes);

}

[解决]

我在加载的类中添加了System.loadLibrary("DSP");,然后加载了本机库,并且很好。

1 个答案:

答案 0 :(得分:0)

217 could not load needed library 'libDSP.so' for 'libnative_code.so' (load_library[1093]: Library 'libDSP.so' not found

您需要预加载依赖项。确保它们也存在。

static {
    System.loadLibrary("DSP");
    System.loadLibrary("native_code");
}