我已经完成了这个链接
How to link any library in ndk application
我已经完成了文件夹sstructure,如下所示
项目(项目根目录)
| - > SRC
| - > com.apparmtest
| ---> MainActivity
| - > JNI
| - > Android.mk
| - > Application.mk
| - > AppARMTest.c
| - > com_apparmtest_MainActivity.h
| - > MYLIB
| - > FileTest.h
| - > libFRead.so
我的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 :(得分:0)
这是运行时链接问题。运行时环境无法找到库libFRead.so,这是libAppARMTest.so所需的
执行ndk-build时不会出现此问题,因为您已在makefile中明确声明了libFRead.so的路径,因此编译工作正常。
尝试确保dalvik能够在运行时加载libFRead.so。您可能需要将库libFRead.so与libAppARMTest.so一起放在libs / armeabi文件夹中。
答案 1 :(得分:0)
在MainActivity类的静态构造函数中,调用 loadLibrary()两次:
static {
System.loadLibrary("FRead");
System.loadLibrary("AppARMTest");
}