我已经在cocos2dx v3.10中开发了游戏并希望将其部署到Android平台中我更喜欢使用android studio。
首先我使用下面的命令编译项目
$ cocos compile -p android --android-studio
当我在android studio中加载项目并运行它时,游戏直接崩溃并在log cat中触发错误。
我的java代码是
protected void onLoadNativeLibraries() {
try {
ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String libName = bundle.getString("android.app.lib_name");
System.loadLibrary(libName);
} catch (Exception e) {
e.printStackTrace();
}
}
Log cat正在显示此错误
04-07 13:05:30.112 17236-17236/? I/art: Late-enabling -Xcheck:jni
04-07 13:05:30.220 17236-17236/? D/JniHelper: JniHelper::setJavaVM(0xb770bb20), pthread_self() = -1225532620
04-07 13:05:30.220 17236-17236/? D/main: cocos_android_app_init
04-07 13:05:30.220 17236-17236/? D/cocos2d: android build version:23
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: jmethodID was NULL
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] in call to CallObjectMethodV
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] from java.lang.String java.lang.Runtime.nativeLoad(java.lang.String, java.lang.ClassLoader, java.lang.String)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] "main" prio=5 tid=1 Runnable
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] | group="main" sCount=0 dsCount=0 obj=0x742042a0 self=0xb770dc60
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] | sysTid=17236 nice=0 cgrp=default sched=0/0 handle=0xb6f3db34
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] | state=R schedstat=( 53423960 14681877 62 ) utm=2 stm=3 core=3 HZ=100
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] | stack=0xbe402000-0xbe404000 stackSize=8MB
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] | held mutexes= "mutator lock"(shared held)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #00 pc 0035c6d5 /system/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, char const*, art::ArtMethod*, void*)+116)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #01 pc 0033d7cf /system/lib/libart.so (art::Thread::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const+138)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #02 pc 0024f6a1 /system/lib/libart.so (art::JavaVMExt::JniAbort(char const*, char const*)+760)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #03 pc 0024fd3f /system/lib/libart.so (art::JavaVMExt::JniAbortV(char const*, char const*, std::__va_list)+54)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #04 pc 000fc0b3 /system/lib/libart.so (art::ScopedCheck::AbortF(char const*, ...)+30)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #05 pc 00101bb5 /system/lib/libart.so (art::ScopedCheck::Check(art::ScopedObjectAccess&, bool, char const*, art::JniValueType*) (.constprop.95)+8164)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #06 pc 0010f043 /system/lib/libart.so (art::CheckJNI::CallMethodV(char const*, _JNIEnv*, _jobject*, _jclass*, _jmethodID*, std::__va_list, art::Primitive::Type, art::InvokeType)+498)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #07 pc 001105e9 /system/lib/libart.so (art::CheckJNI::CallObjectMethodV(_JNIEnv*, _jobject*, _jmethodID*, std::__va_list)+28)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #08 pc 003a1dd8 /data/app/com.demo.test1-1/lib/arm/libcocos2dcpp.so (_JNIEnv::CallObjectMethod(_jobject*, _jmethodID*, ...)+92)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #09 pc 003a1f58 /data/app/com.demo.test1-1/lib/arm/libcocos2dcpp.so (_getClassID(char const*)+108)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #10 pc 003a24f0 /data/app/com.demo.test1-1/lib/arm/libcocos2dcpp.so (cocos2d::JniHelper::getStaticMethodInfo(cocos2d::JniMethodInfo_&, char const*, char const*, char const*)+136)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #11 pc 0039f194 /data/app/com.demo.test1-1/lib/arm/libcocos2dcpp.so (getPackageNameJNI()+132)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #12 pc 0050d578 /data/app/com.demo.test1-1/lib/arm/libcocos2dcpp.so (cocos2d::UserDefault::initXMLFilePath()+72)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #13 pc 0050d464 /data/app/com.demo.test1-1/lib/arm/libcocos2dcpp.so (cocos2d::UserDefault::getInstance()+36)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #14 pc 00395d88 /data/app/com.demo.test1-1/lib/arm/libcocos2dcpp.so (AppDelegate::AppDelegate()+76)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #15 pc 00395a5c /data/app/com.demo.test1-1/lib/arm/libcocos2dcpp.so (cocos_android_app_init(_JNIEnv*)+64)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #16 pc 0039d7e4 /data/app/com.demo.test1-1/lib/arm/libcocos2dcpp.so (JNI_OnLoad+40)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #17 pc 0025026b /system/lib/libart.so (art::JavaVMExt::LoadNativeLibrary(_JNIEnv*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, _jobject*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)+1174)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #18 pc 002c2397 /system/lib/libart.so (art::Runtime_nativeLoad(_JNIEnv*, _jclass*, _jstring*, _jobject*, _jstring*)+178)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] native: #19 pc 0021bb8d /system/framework/arm/boot.oat (Java_java_lang_Runtime_nativeLoad__Ljava_lang_String_2Ljava_lang_ClassLoader_2Ljava_lang_String_2+144)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at java.lang.Runtime.nativeLoad(Native method)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at java.lang.Runtime.doLoad(Runtime.java:435)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] - locked <0x05e716ad> (a java.lang.Runtime)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at java.lang.Runtime.loadLibrary(Runtime.java:370)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at java.lang.System.loadLibrary(System.java:1076)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at org.cocos2dx.lib.Cocos2dxActivity.onLoadNativeLibraries(Cocos2dxActivity.java:248)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at org.cocos2dx.lib.Cocos2dxActivity.onCreate(Cocos2dxActivity.java:263)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at android.app.Activity.performCreate(Activity.java:6245)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread.-wrap11(ActivityThread.java:-1)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at android.os.Looper.loop(Looper.java:148)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread.main(ActivityThread.java:5443)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at java.lang.reflect.Method.invoke!(Native method)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-07 13:05:30.303 17236-17236/? A/art: art/runtime/java_vm_ext.cc:410]
我的android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/external)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos)
LOCAL_MODULE := cocos2dcpp_shared
LOCAL_MODULE_FILENAME := libcocos2dcpp
PROJECT_FILES := hellocpp/main.cpp \
PROJECT_FILES += $(wildcard $(LOCAL_PATH)/../../../Classes/*.cpp)
PROJECT_FILES := $(PROJECT_FILES:$(LOCAL_PATH)/%=%)
LOCAL_SRC_FILES := $(PROJECT_FILES)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../Classes
# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END
LOCAL_STATIC_LIBRARIES := cocos2dx_static
# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END
include $(BUILD_SHARED_LIBRARY)
$(call import-module,.)
# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END