问题
I am doing an app in Android which uses native code with JNI and I have a question for you.
I built some code in c++ to create .so library. then i use this .so in my project and in my Nexus 5 works fine but when I try in other devices app crashes.
I did a lot of test and fail is caused by use this .so in my project, but i don't know because in nexus 5 is Ok and in other devices no.
This is my 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)
and this my 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
and finally this is my Processing class where i load native library:
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);
}
[SOLVED]
I added System.loadLibrary("DSP");
in my Processing class before loaded native library and its fine.
回答1:
217 could not load needed library 'libDSP.so' for 'libnative_code.so' (load_library[1093]: Library 'libDSP.so' not found
You need to preload your dependencies. Make sure they exist too.
static {
System.loadLibrary("DSP");
System.loadLibrary("native_code");
}
来源:https://stackoverflow.com/questions/21309926/how-to-use-so-in-a-second-project-in-android