Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException: Unable to load native library

I'm using purely native NDK in my project (Native Activity). It works fine when I add Prebuilt static libraries with my .so in Android.mk file. But when I try linking Prebuilt shared library, it shows the below exception:

03-27 16:42:09.982: E/AndroidRuntime(1275): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.irrlicht.example/android.app.NativeActivity}: java.lang.IllegalArgumentException: Unable to load native library: /data/app-lib/com.irrlicht.example-1/libHelloWorldMobile.so

Here is my Android.mk file:

....

include $(CLEAR_VARS)
LOCAL_C_INCLUDES := /Users/karthi/Android/openal-soft-master/jni/OpenAL/include
LOCAL_MODULE := openal
LOCAL_SRC_FILES := /Users/karthi/Android/openal-soft-master/libs/armeabi/libopenal.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := HelloWorldMobile

LOCAL_CFLAGS := -D_IRR_ANDROID_PLATFORM_ -pipe -fno-exceptions -fno-rtti -fstrict-aliasing

LOCAL_C_INCLUDES := -I ../../include -I /Users/karthi/Android/openal-soft-master/jni/OpenAL/include

LOCAL_SRC_FILES := main.cpp android_tools.cpp game.cpp

LOCAL_LDLIBS := -lEGL -llog -lGLESv1_CM -lGLESv2 -lz -landroid

LOCAL_SHARED_LIBRARIES := openal

include $(BUILD_SHARED_LIBRARY)

and in my AndroidManifest.xml:

<activity android:name="android.app.NativeActivity"
        android:label="HelloWorldMobile"
        android:launchMode="singleTask"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="landscape"
        android:clearTaskOnLaunch="true">
        <meta-data android:name="android.app.lib_name" android:value="HelloWorldMobile" />                            
        <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

finally in Application.mk:

APP_PLATFORM := android-18
APP_MODULES  := HelloWorldMobile
APP_STL      := stlport_static

I have also tried replacing the last line by,

APP_STL := stlport_shared

What else should I do to link a prebuilt shared library with my native library ? What am I missing here ? Any help will be appreciated.

EDIT:

03-28 11:57:51.469: D/dalvikvm(824): Debugger has detached; object registry had 1 entries
03-28 11:57:51.479: W/dalvikvm(836): PR_CAPBSET_DROP 1 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.479: W/dalvikvm(836): PR_CAPBSET_DROP 2 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.479: W/dalvikvm(836): PR_CAPBSET_DROP 3 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.479: W/dalvikvm(836): PR_CAPBSET_DROP 4 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.489: W/dalvikvm(836): PR_CAPBSET_DROP 5 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.489: W/dalvikvm(836): PR_CAPBSET_DROP 6 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.489: W/dalvikvm(836): PR_CAPBSET_DROP 7 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.489: W/dalvikvm(836): PR_CAPBSET_DROP 8 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.489: W/dalvikvm(836): PR_CAPBSET_DROP 9 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.489: W/dalvikvm(836): PR_CAPBSET_DROP 10 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.489: W/dalvikvm(836): PR_CAPBSET_DROP 11 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.489: W/dalvikvm(836): PR_CAPBSET_DROP 12 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.489: W/dalvikvm(836): PR_CAPBSET_DROP 13 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.489: W/dalvikvm(836): PR_CAPBSET_DROP 14 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.499: W/dalvikvm(836): PR_CAPBSET_DROP 15 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.499: W/dalvikvm(836): PR_CAPBSET_DROP 16 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.499: W/dalvikvm(836): PR_CAPBSET_DROP 17 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.499: W/dalvikvm(836): PR_CAPBSET_DROP 18 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.499: W/dalvikvm(836): PR_CAPBSET_DROP 19 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.499: W/dalvikvm(836): PR_CAPBSET_DROP 20 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.499: W/dalvikvm(836): PR_CAPBSET_DROP 21 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.499: W/dalvikvm(836): PR_CAPBSET_DROP 22 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.499: W/dalvikvm(836): PR_CAPBSET_DROP 23 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.509: W/dalvikvm(836): PR_CAPBSET_DROP 24 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.509: W/dalvikvm(836): PR_CAPBSET_DROP 25 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.519: W/dalvikvm(836): PR_CAPBSET_DROP 26 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.519: W/dalvikvm(836): PR_CAPBSET_DROP 27 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.519: W/dalvikvm(836): PR_CAPBSET_DROP 28 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.519: W/dalvikvm(836): PR_CAPBSET_DROP 29 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.519: W/dalvikvm(836): PR_CAPBSET_DROP 30 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.529: W/dalvikvm(836): PR_CAPBSET_DROP 31 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.529: W/dalvikvm(836): PR_CAPBSET_DROP 32 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.529: W/dalvikvm(836): PR_CAPBSET_DROP 33 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
03-28 11:57:51.529: D/dalvikvm(836): Not late-enabling CheckJNI (already on)
03-28 11:57:51.539: I/ActivityManager(275): Start proc com.irrlicht.example for activity com.irrlicht.example/android.app.NativeActivity: pid=836 uid=10061 gids={50061, 1015, 1028}
03-28 11:57:51.929: I/ActivityManager(275): Config changes=1480 {1.0 310mcc260mnc en_US ldltr sw320dp w533dp h294dp 240dpi nrml long land finger qwerty/v/v -nav/h s.7}
03-28 11:57:51.954: I/InputReader(275): Reconfiguring input devices.  changes=0x00000004
03-28 11:57:51.954: I/InputReader(275): Device reconfigured: id=0, name='qwerty2', size 480x800, orientation 1, mode 1, display id 0
03-28 11:57:51.969: I/InputReader(275): Reconfiguring input devices.  changes=0x00000004
03-28 11:57:51.969: I/InputReader(275): Device reconfigured: id=0, name='qwerty2', size 480x800, orientation 1, mode 1, display id 0
03-28 11:57:52.529: D/dalvikvm(524): GC_FOR_ALLOC freed 9228K, 70% free 4038K/13380K, paused 192ms, total 216ms
03-28 11:57:52.569: I/dalvikvm-heap(524): Grow heap (frag case) to 10.166MB for 6410576-byte allocation
03-28 11:57:52.690: D/dalvikvm(524): GC_FOR_ALLOC freed 11K, 24% free 10287K/13380K, paused 118ms, total 118ms
03-28 11:57:52.699: D/AndroidRuntime(836): Shutting down VM
03-28 11:57:52.709: W/dalvikvm(836): threadid=1: thread exiting with uncaught exception (group=0x41465700)
03-28 11:57:52.739: E/AndroidRuntime(836): FATAL EXCEPTION: main
03-28 11:57:52.739: E/AndroidRuntime(836): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.irrlicht.example/android.app.NativeActivity}: java.lang.IllegalArgumentException: Unable to load native library: /data/app-lib/com.irrlicht.example-1/libHelloWorldMobile.so
03-28 11:57:52.739: E/AndroidRuntime(836):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-28 11:57:52.739: E/AndroidRuntime(836):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-28 11:57:52.739: E/AndroidRuntime(836):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-28 11:57:52.739: E/AndroidRuntime(836):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-28 11:57:52.739: E/AndroidRuntime(836):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 11:57:52.739: E/AndroidRuntime(836):  at android.os.Looper.loop(Looper.java:137)
03-28 11:57:52.739: E/AndroidRuntime(836):  at android.app.ActivityThread.main(ActivityThread.java:5103)
03-28 11:57:52.739: E/AndroidRuntime(836):  at java.lang.reflect.Method.invokeNative(Native Method)
03-28 11:57:52.739: E/AndroidRuntime(836):  at java.lang.reflect.Method.invoke(Method.java:525)
03-28 11:57:52.739: E/AndroidRuntime(836):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-28 11:57:52.739: E/AndroidRuntime(836):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-28 11:57:52.739: E/AndroidRuntime(836):  at dalvik.system.NativeStart.main(Native Method)
03-28 11:57:52.739: E/AndroidRuntime(836): Caused by: java.lang.IllegalArgumentException: Unable to load native library: /data/app-lib/com.irrlicht.example-1/libHelloWorldMobile.so
03-28 11:57:52.739: E/AndroidRuntime(836):  at android.app.NativeActivity.onCreate(NativeActivity.java:184)
03-28 11:57:52.739: E/AndroidRuntime(836):  at android.app.Activity.performCreate(Activity.java:5133)
03-28 11:57:52.739: E/AndroidRuntime(836):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-28 11:57:52.739: E/AndroidRuntime(836):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-28 11:57:52.739: E/AndroidRuntime(836):  ... 11 more
03-28 11:57:52.779: W/ActivityManager(275):   Force finishing activity com.irrlicht.example/android.app.NativeActivity
like image 824
Karthik Sivam Avatar asked Mar 27 '14 11:03

Karthik Sivam


People also ask

Why can't I load a library in Java?

This happens due to missing dependencies (or even implementations). You may be getting the "unable to load library: libFoo.so" but the real dependency missing may be libBar.so that is depended by libFoo.so. Or you may simply have declared functions and called them but have no implementation for them.

What is illegalargumentexception in Java?

But exceptions will arise only when exceptional situations occurred like invalid inputs, null values, etc. They can be handled using try catch blocks. java.lang.IllegalArgumentException will raise when invalid inputs passed to the method. This is most frequent exception in java.

Why does load() fail to work with JNA?

So you built a simple java (not jna) and use load () with the full path and it failed to load? Then either it is NOT built correctly or there is a dependency (library) that the OS cannot find. And you must figure out what it is and set up the shard library path so that the OS can find it. Or alternatively you can explicitly load that as well.

Why can't I load libfoo?

You may be getting the "unable to load library: libFoo.so" but the real dependency missing may be libBar.so that is depended by libFoo.so. Or you may simply have declared functions and called them but have no implementation for them. The problem is due to the way building an so file works.


2 Answers

I have solved this issue by creating a subclass of Native Activity and loaded the shared library as below:

package com.irrlicht.example;

public class MyActivity extends android.app.NativeActivity {

    static {
       System.loadLibrary("HelloWorldMobile");
       System.loadLibrary("openal");
    }
 }

and in AndroidManfest.xml, I'v included MyActivity instead of android.app.NativeActivity. Got this solution from here . But is there a way to completely solve this issue without using java class?

like image 74
Karthik Sivam Avatar answered Oct 11 '22 16:10

Karthik Sivam


1) In your openal module definition, replace LOCAL_C_INCLUDES with LOCAL_EXPORT_C_INCLUDES instead. The former is ignored for prebuilts, the latter will export the value to modules that depend on it. That will allow you to remove the corresponding path in the HelloWorldMobile definition.

2) Try adding "openal" to your APP_MODULES to ensure it is placed under libs/$ABI/, otherwise the library may never be part of your .apk, preventing the load of HelloWorldMobile.

like image 41
Digit Avatar answered Oct 11 '22 14:10

Digit