潜水多年,正好改到了分享一下(chatgpt强力援助)
androidstudioV:2025.1.4
agpV:8.10.0
graldeV:9.0.0
ndkV:28.2.13676358
项目路径\native\engine\android\CMakeLists.txt:
原:cmake_minimum_required(VERSION 3.8)
改:cmake_minimum_required(VERSION 3.22)
在 cc_android_before_target 之后, cc_android_after_target 之前添加
target_link_options(${CC_LIB_NAME} PRIVATE “-Wl,-z,max-page-size=16384”)
项目路径\native\engine\android\app\build.gradle:
externalNativeBuild{
cmake{
cppFlags “-frtti -fexceptions -fsigned-char -std=c++17”
}
}
在 externalNativeBuild 下面添加
buildFeatures{
buildConfig = true
}
到这里打包应该是正常的,下面改引擎
官方给的16kb分支https://github.com/cocos/cocos-engine-external/tree/v3.8.7-16k内android文件夹整个替换掉3.5.0\resources\resources\3d\engine\native\external\android
3.5.0\resources\resources\3d\engine\native\cmake\predefine.cmake:
109行附近
原: set(CMAKE_CXX_STANDARD 14)
改: set(CMAKE_CXX_STANDARD 17)
------------------------------------------------------------分割线-----------------------------------------------------------------------
3.5.0\resources\resources\3d\engine\native\cocos\bindings\manual\jsb_global_init.cpp:
55行附近
原: static std::basic_string xxteaKey;
改: static std::string xxteaKey;
109行附近
原: const_cast<unsigned char *>(xxteaKey.data()),
改: reinterpret_cast<unsigned char >(const_cast<char>(xxteaKey.data())),
150行附近
原: const_cast<unsigned char >(xxteaKey.data()),
改: reinterpret_cast<unsigned char>(const_cast<char *>(xxteaKey.data())),
------------------------------------------------------------分割线-----------------------------------------------------------------------
3.5.0\resources\resources\3d\engine\native\external\sources\boost\container_hash\hash.hpp:
132行附近
原: struct hash_base : std::unary_function<T, std::size_t> {};
改: struct hash_base : std::__unary_function<T, std::size_t> {};
------------------------------------------------------------分割线-----------------------------------------------------------------------
3.5.0\resources\resources\3d\engine\native\cocos\bindings\jswrapper\v8\ObjectWrap.cpp:
103行附近
原: persistent().SetWeak(this, weakCallback, v8::WeakCallbackType::kFinalizer);
改: persistent().SetWeak(this, weakCallback, v8::WeakCallbackType::kInternalFields);
------------------------------------------------------------分割线-----------------------------------------------------------------------
3.5.0\resources\resources\3d\engine\native\cocos\bindings\jswrapper\v8\debugger\env.h:
393行附近
原: inline void ThrowError(v8::Localv8::Value (*fun)(v8::Localv8::String),
const char *errmsg);
改: inline void ThrowError(v8::Localv8::Value (*fun)(v8::Localv8::String,v8::Localv8::Value), const char *errmsg);
537行附近
原: inline void Environment::ThrowError(
v8::Localv8::Value (*fun)(v8::Localv8::String),
const char *errmsg) {
v8::HandleScope handle_scope(isolate());
isolate()->ThrowException(fun(OneByteString(isolate(), errmsg)));
}
改: inline void Environment::ThrowError(
v8::Localv8::Value (*fun)(v8::Localv8::String,v8::Localv8::Value options),
const char *errmsg) {
v8::HandleScope handle_scope(isolate());
isolate()->ThrowException(fun(OneByteString(isolate(), errmsg, strlen(errmsg)),{}));
}
------------------------------------------------------------分割线-----------------------------------------------------------------------
3.5.0\resources\resources\3d\engine\native\cocos\bindings\jswrapper\v8\debugger\inspector_agent.cpp:
394行附近
原: session_ = inspector->connect(1, this, StringView());
改: #if V8_MAJOR_VERSION > 10 || (V8_MAJOR_VERSION == 10 && V8_MINOR_VERSION > 3)
session_ = inspector->connect(1, this, StringView(),v8_inspector::V8Inspector::ClientTrustLevel::kFullyTrusted);
#else
session_ =inspector->connect(1,this,StringView());
#endif
------------------------------------------------------------分割线-----------------------------------------------------------------------
3.5.0\resources\resources\3d\engine\native\cocos\bindings\jswrapper\v8\ScriptEngine.h:
320行附近
原: static void onOOMErrorCallback(const char *location, bool isHeapOom);
改: static void onOOMErrorCallback(const char *location,
#if V8_MAJOR_VERSION > 10 || (V8_MAJOR_VERSION == 10 && V8_MINOR_VERSION > 4)
const v8::OOMDetails &details
#else
bool isHeapOom
#endif
);
------------------------------------------------------------分割线-----------------------------------------------------------------------
3.5.0\resources\resources\3d\engine\native\cocos\bindings\jswrapper\v8\ScriptEngine.cpp:
228行附近
原: v8::V8::ShutdownPlatform();
改: #if V8_MAJOR_VERSION > 9 || (V8_MAJOR_VERSION == 9 && V8_MINOR_VERSION > 7)
v8::V8::DisposePlatform();
#else
v8::V8::ShutdownPlatform();
#endif
258行附近
原: void ScriptEngine::onOOMErrorCallback(const char *location, bool isHeapOom) {
std::string errorStr = "[OOM ERROR] location: ";
errorStr += location;
std::string message;
message = "is heap out of memory: ";
if (isHeapOom) {
message += “true”;
} else {
message += “false”;
}
errorStr += ", " + message;
SE_LOGE("%s\n", errorStr.c_str());
getInstance()->callExceptionCallback(location, message.c_str(), "(no stack information)");
}
改: void ScriptEngine::onOOMErrorCallback(const char *location,
#if V8_MAJOR_VERSION > 10 || (V8_MAJOR_VERSION == 10 && V8_MINOR_VERSION > 4)
const v8::OOMDetails &details
#else
bool isHeapOom
#endif
) {
std::string errorStr = "[OOM ERROR] location: ";
errorStr += location;
std::string message;
message = "is heap out of memory: ";
#if V8_MAJOR_VERSION > 10 || (V8_MAJOR_VERSION == 10 && V8_MINOR_VERSION > 4)
if (details.is_heap_oom) {
#else
if(isHeapOom) {
#endif
message += “true”;
} else {
message += “false”;
}
errorStr += ", " + message;
SE_LOGE("%s\n", errorStr.c_str());
getInstance()->callExceptionCallback(location, message.c_str(), "(no stack information)");
}
694行附近
原: const double kLongIdlePauseInSeconds = 1.0;
_isolate->ContextDisposedNotification();
_isolate->IdleNotificationDeadline(gSharedV8->platform->MonotonicallyIncreasingTime() + kLongIdlePauseInSeconds);
// By sending a low memory notifications, we will try hard to collect all
// garbage and will therefore also invoke all weak callbacks of actually
// unreachable persistent handles.
_isolate->LowMemoryNotification();
改: const double kLongIdlePauseInSeconds = 1.0;
#if V8_MAJOR_VERSION > 10 || (V8_MAJOR_VERSION == 10 && V8_MINOR_VERSION > 4)
_isolate->LowMemoryNotification();
_isolate->RequestGarbageCollectionForTesting(v8::Isolate::GarbageCollectionType::kFullGarbageCollection);
#else
_isolate->ContextDisposedNotification();
_isolate->IdleNotificationDeadline(gSharedV8->platform->MonotonicallyIncreasingTime() + kLongIdlePauseInSeconds);
// By sending a low memory notifications, we will try hard to collect all
// garbage and will therefore also invoke all weak callbacks of actually
// unreachable persistent handles.
_isolate->LowMemoryNotification();
#endif
765行附近
原: v8::ScriptOrigin origin(_isolate, originStr.ToLocalChecked());
改: v8::ScriptOrigin origin(originStr.ToLocalChecked());
855行附近
原: v8::ScriptOrigin origin(_isolate, scriptPath);
改: v8::ScriptOrigin origin(scriptPath);
898行附近
原: v8::ScriptOrigin origin(_isolate, scriptPath, 0, 0, true);
改: v8::ScriptOrigin origin(scriptPath, 0, 0, true);
全部修改完测试正常,androidstudio内16kb模拟器运行正常
额外打包警告(选择修改):
3.5.0\resources\resources\3d\engine\native\cocos\platform\java\jni\JniCocosTouchHandler.cpp:
60行附近
原: jint id[size];
jfloat x[size];
jfloat y[size];
env->GetIntArrayRegion(ids, 0, size, id);
env->GetFloatArrayRegion(xs, 0, size, x);
env->GetFloatArrayRegion(ys, 0, size, y);
改: std::vector id(size);
std::vector x(size);
std::vector y(size);
env->GetIntArrayRegion(ids, 0, size, id.data());
env->GetFloatArrayRegion(xs, 0, size, x.data());
env->GetFloatArrayRegion(ys, 0, size, y.data());
82行附近(同上)
原: jint id[size];
jfloat x[size];
jfloat y[size];
env->GetIntArrayRegion(ids, 0, size, id);
env->GetFloatArrayRegion(xs, 0, size, x);
env->GetFloatArrayRegion(ys, 0, size, y);
改: std::vector id(size);
std::vector x(size);
std::vector y(size);
env->GetIntArrayRegion(ids, 0, size, id.data());
env->GetFloatArrayRegion(xs, 0, size, x.data());
env->GetFloatArrayRegion(ys, 0, size, y.data());
