1

I have follwed this link

How to link any library in ndk application

and I have done my folder sstructure as bellow

Project (Project root dir)
|->src
|-->com.apparmtest
|--->MainActivity
|->jni
|-->Android.mk
|-->Application.mk
|-->AppARMTest.c
|-->com_apparmtest_MainActivity.h
|->myLib
|-->FileTest.h
|-->libFRead.so

and my Android.mk file content is

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
Community
  • 1
  • 1
user1241903
  • 129
  • 2
  • 14

2 Answers2

0

This is a runtime linking problem. The runtime environment is not able to find the library libFRead.so, which is required by libAppARMTest.so

This problem does not arise at the time you execute ndk-build, because you have explicitly stated the path to libFRead.so in your makefile, so the compilation works fine.

Try to make sure that dalvik is able to load libFRead.so at run-time. You may need to put the library libFRead.so in the libs/armeabi folder alongwith libAppARMTest.so.

Sumeet Khullar
  • 463
  • 1
  • 4
  • 12
  • thank you friend, I am using eclipse and in eclipse project explorer I have seen that in both libs/armeabi as well as obj/local/armeabi folder both the .so files are present. how to fix this issue – user1241903 Oct 19 '12 at 09:06
  • What happens when you try to use System.load(libFRead.so)? Is it able to load the stand-alone .so file, without throwing errors? – Sumeet Khullar Oct 19 '12 at 09:18
  • I have tested that too, it is also giving error but little bit different error"10-19 14:53:05.588: E/AndroidRuntime(695): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load libFRead: findLibrary returned null" – user1241903 Oct 19 '12 at 09:24
  • This error indicates that it is your runtime environment is not able to find libFRead.so, (also the reason why loading libAppARMTest.so is failing, since it requires libFRead.so). I am not sure why it is not able to find the library, since you have them present in the directories. Maybe you can try to load it by giving some absolute path and see if that works. – Sumeet Khullar Oct 19 '12 at 09:30
  • is it like this? System.load((project root)/myLib/libFRead.so) – user1241903 Oct 19 '12 at 09:33
  • Currently, are you using System.loadLibrary or System.load, both are slightly different. See the answer to http://stackoverflow.com/questions/9220984/how-to-load-libandroid-runtime – Sumeet Khullar Oct 19 '12 at 09:37
  • i am using this System.loadLibrary("/home/admin/ndk_workspace/AppARMTest/jni/libFRead.so"); – user1241903 Oct 19 '12 at 09:43
  • and one more doubt i am having is, in my project one external .so(libFRead.so) file i am using. And in java(Activity) i am having one static block for loading lib. in this block which lib i need to load? do i need to load libAppARMTest.so or libFRead.so or both? – user1241903 Oct 19 '12 at 10:28
  • Try to load both libraries and see it helps. I am not sure what else can resolve your problems, but playing around with the paths, giving absolute/relative paths etc. can help you figure it out. – Sumeet Khullar Oct 19 '12 at 16:43
0

In the static constructor of the MainActivity class call loadLibrary() twice:

static {
    System.loadLibrary("FRead");
    System.loadLibrary("AppARMTest");
}
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307