java.lang.IllegalArgumentException:无法加载本机库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java.lang.IllegalArgumentException:无法加载本机库相关的知识,希望对你有一定的参考价值。

我在项目中使用纯本地的NDK(本地活动)。当我在android.mk文件中使用.so添加预构建的静态库时,它可以正常工作。但是,当我尝试链接Prebuilt共享库时,它显示以下异常:

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

这是我的Android.mk文件:

....

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)

以及在我的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" />

最终在Application.mk中:

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

我也尝试过将最后一行替换为,

APP_STL := stlport_shared

将预置的共享库与本机库链接起来,我还应该做什么?我在这里想念什么?任何帮助将不胜感激。

编辑:

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
答案

我已经通过创建本机活动的子类解决了此问题,并按如下所示加载了共享库:

package com.irrlicht.example;

public class MyActivity extends android.app.NativeActivity {

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

并且在AndroidManfest.xml中,我包括了MyActivity而不是android.app.NativeActivity。从here获得了此解决方案。但是有没有一种方法可以完全解决此问题而无需使用Java类?

另一答案

1)在您的开放模块定义中,将LOCAL_C_INCLUDES替换为LOCAL_EXPORT_C_INCLUDES。对于预构建,前者将被忽略,而后者会将值导出到依赖于此的模块。这样您就可以删除HelloWorldMobile定义中的相应路径。

2)尝试在您的APP_MODULES中添加“ openal”,以确保将其放置在libs / $ ABI /下,否则该库可能永远不会成为您的.apk的一部分,从而阻止了HelloWorldMobile的加载。

另一答案
 externalNativeBuild {
            cmake {
                arguments "-DANDROID_STL=c++_shared" //add this in the build.gradle

            }
        }

以上是关于java.lang.IllegalArgumentException:无法加载本机库的主要内容,如果未能解决你的问题,请参考以下文章

IllegalArgumentException:此 NavController 未知导航目的地 xxx