如题
CCFileUtils::sharedFileUtils()
静态调用,也就是在cpp文件的函数外面调用
Android平台会无故崩溃,跟踪调试发现在CCFileUtilsAndroid.cpp的42行过不去即
CCFileUtils* CCFileUtils::sharedFileUtils()
{
if (s_sharedFileUtils == NULL)
{
s_sharedFileUtils = new CCFileUtilsAndroid();
s_sharedFileUtils->init();
std::string resourcePath = getApkPath();
s_pZipFile = new ZipFile(resourcePath, "assets/");
}
return s_sharedFileUtils;
}
std::string resourcePath = getApkPath();
这行 然后查看getApkPath()函数,里面也就返回string g_apkPath 的c_str()
最后没查出别的有用信息,不过此时g_apkPath应该是null,因为g_apkPath是依靠java那边回调才设置的值
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetApkPath(JNIEnv* env, jobject thiz, jstring apkPath) {
g_apkPath = JniHelper::jstring2string(apkPath);
}
而静态调用了CCFileUtils::sharedFileUtils(),发生在静态区
而java加载libgame.so的方法如下
static {
System.loadLibrary("game");
}
可想而知getApkPath的调用就会在nativeSetApkPath之前
所以CCFileUtils::sharedFileUtils()就不要在全局区调用了(也就是静态调用)
但是ios平台,是没有问题
ps:遇到问题就先记录,以免以后再次遇到,耗费时间去找问题