关于cocos2.0.9anysdk在cc.game.restart()之后anysdk变量未定义的解决方案

  • Creator 版本:2.0.9 rc3

  • 目标平台: iOS / Android

    1,构建模板:default
    2, 在构建出来的安卓项目中修改jsb-default\frameworks\runtime-src\Classes文件夹中的AppDelegate.cpp如下:
    #include "AppDelegate.h"

#include "cocos2d.h"

#include "cocos/scripting/js-bindings/manual/jsb_module_register.hpp"
#include "cocos/scripting/js-bindings/manual/jsb_global.h"
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
#include "cocos/scripting/js-bindings/event/EventDispatcher.h"
#include "cocos/scripting/js-bindings/manual/jsb_classtype.hpp"

/新增开始*/
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && PACKAGE_AS
#include "SDKManager.h"
#include "jsb_anysdk_protocols_auto.hpp"
#include "manualanysdkbindings.hpp"
using namespace anysdk::framework;
#endif
/新增结束*/
USING_NS_CC;

AppDelegate::AppDelegate(int width, int height) : Application(“Cocos Game”, width, height)
{
}

AppDelegate::~AppDelegate()
{
/新增开始*/
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && PACKAGE_AS
SDKManager::getInstance()->purge();
** #endif**
/新增结束*/
}

bool AppDelegate::applicationDidFinishLaunching()
{
/新增开始*/
** #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS && PACKAGE_AS**
** SDKManager::getInstance()->loadAllPlugins();**
** #endif**
/新增结束*/
se::ScriptEngine* se = se::ScriptEngine::getInstance();

jsb_set_xxtea_key("crnet2018-666-88");
jsb_init_file_operation_delegate();

#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
** // Enable debugger here**
** jsb_enable_debugger(“0.0.0.0”, 6086, false);**
#endif

se->setExceptionCallback([](const char* location, const char* message, const char* stack){
    // Send exception information to server like Tencent Bugly.

});

jsb_register_all_modules();

/新增开始*/
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && PACKAGE_AS
** se->addRegisterCallback(register_all_anysdk_framework);**
** se->addRegisterCallback(register_all_anysdk_manual);**
#endif
/新增结束*/
se->start();

se::AutoHandleScope hs;
jsb_run_script("jsb-adapter/jsb-builtin.js");
jsb_run_script("main.js");

se->addAfterCleanupHook([](){
    JSBClassType::destroy();
});

return true;

}

// This function will be called when the app is inactive. When comes a phone call,it’s be invoked too
void AppDelegate::applicationDidEnterBackground()
{
EventDispatcher::dispatchEnterBackgroundEvent();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
EventDispatcher::dispatchEnterForegroundEvent();
}
同时修改SDKManager.cpp文件如下只是删除了两行代码

/****************************************************************************
Copyright © 2017-2018 Xiamen Yaji Software Co., Ltd.

http://www.cocos.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the “Software”), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that’s
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.

The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

#include "SDKManager.h"
#include "jsb_anysdk_protocols_auto.hpp"
#include "manualanysdkbindings.hpp"
#include "cocos2d.h"
USING_NS_CC;
using namespace anysdk::framework;

SDKManager *SDKManager::_pInstance = nullptr;

SDKManager::SDKManager()
{
}

SDKManager::~SDKManager()
{
AgentManager::getInstance()->unloadAllPlugins();
AgentManager::end();
}

SDKManager *SDKManager::getInstance()
{
if (_pInstance == nullptr)
{
_pInstance = new SDKManager();
}
return _pInstance;
}

void SDKManager::purge()
{
if (_pInstance != nullptr)
{
delete _pInstance;
_pInstance = nullptr;
}
}

void SDKManager::loadAllPlugins()
{
se::ScriptEngine se = se::ScriptEngine::getInstance();
/
*
* appKey, appSecret and privateKey are the only three parameters generated
* after the packing tool client finishes creating the game.
* The oauthLoginServer parameter is the API address provided by the game service
* to login verification
*/
std::string oauthLoginServer = “http://app.game.morethantech.cn/player/loginWeChat”;
std::string appKey = “02C822E4-434E-ECD4-EAE3-8224AB3605BE”;
std::string appSecret = “6efb53145007659da8262327d80849df”;
std::string privateKey = “CB773983185EFD6C4CD7364061A5B275”;

AgentManager *pAgent = AgentManager::getInstance();
pAgent->init(appKey, appSecret, privateKey, oauthLoginServer);

//Initialize plug-ins, including SDKs.
pAgent->loadAllPlugins();
/*******删除底下两行*******/
// se->addRegisterCallback(register_all_anysdk_framework);
// se->addRegisterCallback(register_all_anysdk_manual);

}
上述是争对安卓的,实质就是照着2.0.6版本的修改,苹果的类似。
亲测有效。

##官方解决方案已出
首先将上述临时解决方案中的在 AppDelegate.cpp 中添加的代码删除,在服务面板关闭 AnySDK 服务。重新构建一次项目,将旧的 AnySDK 服务从项目中清除。

注:若是对构建出的原生项目未做自定义修改可直接删除 build/jsb-link 或 build/jsb-default 文件夹。
###

  • 2.0.9 版本

    关闭服务后,使用 (Command + R)(Ctrl+R) 刷新服务面板,选择更新 AnySDK 服务,重新开启服务之后重新构建项目即可解决问题。

    ###

  • 2.0.7 和 2.0.8 版本

    关闭服务后,删除 CocosCteator 配置目录( Windows: 当前用户目录/.CocosCreator ; macOS: ~/.CocosCreator })下的 services/anysdk 文件夹

    //Windows 
    C:/Users/用户名/.CocosCreator/services/anysdk
    // macOS
    ~/.CocosCreator/services/anysdk
    

    完成上述操作之后,重新启动 CocosCreator ,在服务面板重新开启服务即可,确认插件版本是否是 1.0.1_2.2.5 版本,开启服务后重新构建即可解决问题。