我在我的项目中使用纯粹的原生NDK(Native Activity)。当我在Android.mk文件中添加带有.so的Prebuilt静态库时,它工作正常。但是当我尝试链接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
答案 0 :(得分:6)
我通过创建Native Activity的子类并按如下所示加载共享库来解决了这个问题:
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 :(得分:1)
1)在您的openal模块定义中,将LOCAL_C_INCLUDES替换为LOCAL_EXPORT_C_INCLUDES。预编译会忽略前者,后者会将值导出到依赖于它的模块。这将允许您删除HelloWorldMobile定义中的相应路径。
2)尝试在APP_MODULES中添加“openal”以确保它位于libs / $ ABI /下,否则库可能永远不会成为你的.apk的一部分,从而阻止了HelloWorldMobile的加载。