diff --git a/native/cocos/platform/android/jni/JniImp.cpp b/native/cocos/platform/android/jni/JniImp.cpp index fe91cb9d5d..24fcff3dda 100644 --- a/native/cocos/platform/android/jni/JniImp.cpp +++ b/native/cocos/platform/android/jni/JniImp.cpp @@ -104,6 +104,8 @@ namespace } } +void cocos_jni_env_init (JNIEnv* env); + Application* cocos_android_app_init(JNIEnv* env, int width, int height); extern "C" @@ -111,6 +113,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { JniHelper::setJavaVM(vm); + cocos_jni_env_init(JniHelper::getEnv()); return JNI_VERSION_1_4; } diff --git a/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp b/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp index d7a60e7b7c..3a78ffb04e 100755 --- a/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -34,6 +34,13 @@ #include "cocos/scripting/js-bindings/event/CustomEventTypes.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) @@ -42,11 +49,17 @@ AppDelegate::AppDelegate(int width, int height) : Application("Cocos Game", widt 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(""); @@ -64,6 +77,11 @@ bool AppDelegate::applicationDidFinishLaunching() 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; diff --git a/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.h b/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.h index 97fc8b4e8a..47bad3646d 100755 --- a/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.h +++ b/native/templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.h @@ -34,7 +34,7 @@ class AppDelegate : public cocos2d::Application { public: AppDelegate(int width, int height); - ~AppDelegate(); + virtual ~AppDelegate(); /** @brief Implement Director and Scene init code here. diff --git a/native/templates/js-template-default/frameworks/runtime-src/proj.android-studio/app/build.gradle b/native/templates/js-template-default/frameworks/runtime-src/proj.android-studio/app/build.gradle index 5d4a69075d..54747452aa 100755 --- a/native/templates/js-template-default/frameworks/runtime-src/proj.android-studio/app/build.gradle +++ b/native/templates/js-template-default/frameworks/runtime-src/proj.android-studio/app/build.gradle @@ -7,7 +7,7 @@ android { buildToolsVersion PROP_BUILD_TOOLS_VERSION defaultConfig { - applicationId "org.cocos2d.helloworld" + applicationId "org.cocos2dx.hellojavascript" minSdkVersion PROP_MIN_SDK_VERSION targetSdkVersion PROP_TARGET_SDK_VERSION versionCode 1 diff --git a/native/templates/js-template-default/frameworks/runtime-src/proj.android-studio/app/jni/hellojavascript/main.cpp b/native/templates/js-template-default/frameworks/runtime-src/proj.android-studio/app/jni/hellojavascript/main.cpp index ef71144175..caeac73cd8 100755 --- a/native/templates/js-template-default/frameworks/runtime-src/proj.android-studio/app/jni/hellojavascript/main.cpp +++ b/native/templates/js-template-default/frameworks/runtime-src/proj.android-studio/app/jni/hellojavascript/main.cpp @@ -41,23 +41,29 @@ using namespace cocos2d; using namespace anysdk::framework; #endif -Application* cocos_android_app_init (JNIEnv* env, int width, int height) { - LOGD("cocos_android_app_init"); - auto app = new AppDelegate(width, height); +//called when JNI_OnLoad() +void cocos_jni_env_init (JNIEnv* env) { + LOGD("cocos_jni_env_init"); #if PACKAGE_AS - JavaVM* vm; + JavaVM *vm; env->GetJavaVM(&vm); PluginJniHelper::setJavaVM(vm); #endif +} + +//called when onSurfaceCreated() +Application* cocos_android_app_init (JNIEnv* env, int width, int height) { + LOGD("cocos_android_app_init"); + auto app = new AppDelegate(width, height); return app; } extern "C" { - void Java_org_cocos2dx_javascript_SDKWrapper_nativeLoadAllPlugins(JNIEnv* env, jobject thiz) - { + void Java_org_cocos2dx_javascript_SDKWrapper_nativeLoadAllPlugins(JNIEnv* env, jobject thiz) + { #if PACKAGE_AS - SDKManager::getInstance()->loadAllPlugins(); + SDKManager::getInstance()->loadAllPlugins(); #endif - } + } } diff --git a/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp b/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp index a669b8cd1b..3a78ffb04e 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -34,12 +34,12 @@ #include "cocos/scripting/js-bindings/event/CustomEventTypes.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 +#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; @@ -49,16 +49,16 @@ AppDelegate::AppDelegate(int width, int height) : Application("Cocos Game", widt AppDelegate::~AppDelegate() { -//#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && PACKAGE_AS -// SDKManager::getInstance()->purge(); -//#endif +#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 +#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS && PACKAGE_AS + SDKManager::getInstance()->loadAllPlugins(); +#endif se::ScriptEngine* se = se::ScriptEngine::getInstance(); @@ -77,12 +77,12 @@ bool AppDelegate::applicationDidFinishLaunching() jsb_register_all_modules(); - se->start(); +#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 -//#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"); diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.android-studio/app/jni/hellojavascript/main.cpp b/native/templates/js-template-link/frameworks/runtime-src/proj.android-studio/app/jni/hellojavascript/main.cpp index ef71144175..caeac73cd8 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/proj.android-studio/app/jni/hellojavascript/main.cpp +++ b/native/templates/js-template-link/frameworks/runtime-src/proj.android-studio/app/jni/hellojavascript/main.cpp @@ -41,23 +41,29 @@ using namespace cocos2d; using namespace anysdk::framework; #endif -Application* cocos_android_app_init (JNIEnv* env, int width, int height) { - LOGD("cocos_android_app_init"); - auto app = new AppDelegate(width, height); +//called when JNI_OnLoad() +void cocos_jni_env_init (JNIEnv* env) { + LOGD("cocos_jni_env_init"); #if PACKAGE_AS - JavaVM* vm; + JavaVM *vm; env->GetJavaVM(&vm); PluginJniHelper::setJavaVM(vm); #endif +} + +//called when onSurfaceCreated() +Application* cocos_android_app_init (JNIEnv* env, int width, int height) { + LOGD("cocos_android_app_init"); + auto app = new AppDelegate(width, height); return app; } extern "C" { - void Java_org_cocos2dx_javascript_SDKWrapper_nativeLoadAllPlugins(JNIEnv* env, jobject thiz) - { + void Java_org_cocos2dx_javascript_SDKWrapper_nativeLoadAllPlugins(JNIEnv* env, jobject thiz) + { #if PACKAGE_AS - SDKManager::getInstance()->loadAllPlugins(); + SDKManager::getInstance()->loadAllPlugins(); #endif - } + } }