I've written a NativeActivity that's crashing on launch because (my theory...) I'm doing something wrong naming the shared library or the module or something in AndroidManifest.xml. Below is the output of LogCat when my app is launched, along with the contents of my manifest file.
Any ideas where I've gone wrong in setting this up?
Thanks, ALF
===================MANIFEST=============================================
<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Mythica.SpiderGame"
android:versionCode="1"
android:versionName="1.0">
<!-- This is the platform API where NativeActivity was introduced. -->
<uses-sdk android:minSdkVersion="12" />
<!-- This .apk has no Java code itself, so set hasCode to false. -->
<application android:label="@string/app_name" android:hasCode="false">
<!-- Our activity is the built-in NativeActivity framework class.
This will take care of integrating with our NDK code. -->
<activity android:name="android.app.NativeActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<!-- Tell NativeActivity the name of or .so -->
<meta-data android:name="android.app.lib_name"
android:value="SpiderGame" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<!-- END_INCLUDE(manifest) -->
===================LOGCAT OUTPUT=============================================
07-31 23:13:11.770: WARN/dalvikvm(1384): threadid=1: thread exiting with uncaught exception (group=0x4016d760) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): FATAL EXCEPTION: main 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Mythica.SpiderGame/android.app.NativeActivity}: java.lang.IllegalArgumentException: Unable to load native library: /data/data/com.Mythica.SpiderGame/lib/libSpiderGame.so 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at android.app.ActivityThread.access$1500(ActivityThread.java:122) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at android.os.Handler.dispatchMessage(Handler.java:99) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at android.os.Looper.loop(Looper.java:132) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at android.app.ActivityThread.main(ActivityThread.java:4028) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at java.lang.reflect.Method.invokeNative(Native Method) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at java.lang.reflect.Method.invoke(Method.java:491) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at dalvik.system.NativeStart.main(Native Method) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): Caused by: java.lang.IllegalArgumentException: Unable to load native library: /data/data/com.Mythica.SpiderGame/lib/libSpiderGame.so 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at android.app.NativeActivity.onCreate(NativeActivity.java:199) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at android.app.Instrumentation.callActivityOnCreat开发者_运维知识库e(Instrumentation.java:1048) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715) 07-31 23:13:11.780: ERROR/AndroidRuntime(1384): ... 11 more 07-31 23:13:11.780: WARN/ActivityManager(288): Force finishing activity com.Mythica.SpiderGame/android.app.NativeActivity 07-31 23:13:11.800: ERROR/android.os.Debug(288): Dumpstate > /data/log/dumpstate_app_error
The solution to my problem turned out to be building my game library as a static object rather than a shared one. MyApp references MyLibrary.so and I guess that shared library has to be copied onto the device manually before runtime, though I haven't tested that theory by doing so since static linking is doing the job.
If I'm correct, the invalid assumption was thinking any shared libraries of my own would be built into my .apk just like NativeActivity was built-in.
You should read docs/PREBUILTS.html in the NDK. You can add a section looking like this to your Android.mk file:
#
# Include prebuilt shared library
#
include $(CLEAR_VARS)
LOCAL_MODULE := MyLibrary-prebuilt
LOCAL_SRC_FILES := relative/path/to/MyLibrary.so
LOCAL_EXPORT_C_INCLUDES := relative/path/to/includes
include $(PREBUILT_SHARED_LIBRARY)
and then just before you call BUILD_SHARED_LIBRARY at the end of your Android.mk, you put this:
LOCAL_SHARED_LIBRARIES := MyLibrary-prebuilt
This is assuming you have somehow managed to cross-compile your library for ARM outside of the ndk build system. If you want to build your shared library from within ndk-build, you do it the same way you build the .so for your native activity.
精彩评论