2012-10-19 57 views
1

我已經follwed此鏈接的Android NDK運行時錯誤

How to link any library in ndk application

,我已經做了我的文件夾S結構的波紋管

項目(項目根目錄)
| - > SRC
| - - > com.apparmtest
| ---> MainActivity
| - > jni
| - > Android.mk
| - > Application.mk
| - > AppARMTest.c
| - > com_apparmtest_MainActivity.h
| - > MYLIB
| - > FileTest.h
| - > libFRead。所以

和我的Android.mk文件內容

Android.mk

LOCAL_PATH := $(call my-dir) 


### include FileTest.so as a prebuilt lib ### 

include $(CLEAR_VARS) 


LOCAL_MODULE   := my-prebuilt-lib 

LOCAL_SRC_FILES   := ../myLib/libFRead.so 

LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../myLib 


include $(PREBUILT_SHARED_LIBRARY) 

### end prebuilt### 

include $(CLEAR_VARS) 

LOCAL_MODULE := AppARMTest 

LOCAL_C_INCLUDES := $(LOCAL_PATH) \ 
       $(LOCAL_PATH)/../myLib 

LOCAL_SRC_FILES := AppARMTest.c 

LOCAL_LDLIBS := -llog 

LOCAL_STATIC_LIBRARIES := my-prebuilt-lib 

include $(BUILD_SHARED_LIBRARY) 


and my Application.mk contains 


APP_MODULES := AppARMTest 

APP_PLATFORM := ANDROID-8 


and this project is compiling properly as well as ndk-build is not giving any error. 

But while running on emmulator I am getting this error 



    10-19 10:25:40.704: E/AndroidRuntime(334): FATAL EXCEPTION: main 

    10-19 10:25:40.704: E/AndroidRuntime(334): java.lang.ExceptionInInitializerError 

    10-19 10:25:40.704: E/AndroidRuntime(334): at java.lang.Class.newInstanceImpl(Native Method) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at java.lang.Class.newInstance(Class.java:1409) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at 

    android.app.Instrumentation.newActivity(Instrumentation.java:1021) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 


    10-19 10:25:40.704: E/AndroidRuntime(334): at 

    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at 

    android.app.ActivityThread.access$1500(ActivityThread.java:117) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at 

    android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at 

    android.os.Handler.dispatchMessage(Handler.java:99) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at android.os.Looper.loop(Looper.java:123) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at 

    android.app.ActivityThread.main(ActivityThread.java:3683) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at java.lang.reflect.Method.invokeNative(Native Method) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at 

    java.lang.reflect.Method.invoke(Method.java:507) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at 

    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at 

    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at dalvik.system.NativeStart.main(Native Method) 

    10-19 10:25:40.704: E/AndroidRuntime(334): Caused by: java.lang.UnsatisfiedLinkError: Cannot load library: link_image[1962]: 33 could not load needed library 

    './obj/local/armeabi/libFRead.so' for 'libAppARMTest.so' (load_library[1104]: Library './obj/local/armeabi/libFRead.so' not found) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at java.lang.Runtime.loadLibrary(Runtime.java:434) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at java.lang.System.loadLibrary(System.java:554) 

    10-19 10:25:40.704: E/AndroidRuntime(334): at ivz.apparmtest.MainActivity.<clinit>(MainActivity.java:11) 

    10-19 10:25:40.704: E/AndroidRuntime(334): ... 15 more 

回答

0

這是一個運行時鏈接問題。運行時環境無法找到libAppARMTest.so所需的庫libFRead.so

在執行ndk-build時不會出現此問題,因爲您明確指出了libFRead.so的路徑在你的makefile中,編譯工作正常。

嘗試確保dalvik能夠在運行時加載libFRead.so。您可能需要將庫libFRead.so與libAppARMTest.so一起放在libs/armeabi文件夾中。

+0

謝謝你的朋友,我使用日食和月食項目瀏覽器我已經看到,在這兩個庫/ armeabi以及作爲obj/local/armeabi文件夾,兩個.so文件都存在。如何解決此問題 – user1241903

+0

當您嘗試使用System.load(libFRead.so)時會發生什麼?它能否加載獨立的.so文件,而不會引發錯誤? –

+0

我也測試過,它也給錯誤,但有點不同的錯誤「10-19 14:53:05.588:E/AndroidRuntime(695):引起:java.lang.UnsatisfiedLinkError:無法加載libFRead:findLibrary返回null「 – user1241903

0

在MainActivity類調用調用LoadLibrary(靜態構造函數)兩次:

static { 
    System.loadLibrary("FRead"); 
    System.loadLibrary("AppARMTest"); 
}