HarmonyOS鸿蒙Next中Cocos无法调用DevEco项目中的方法
HarmonyOS鸿蒙Next中Cocos无法调用DevEco项目中的方法 刚在cocos项目中生成了harmonyOS next,用deveco打开,测试是否可以在cocos调用arkTS方法。
问题:云调试输出的日志显示无法调用arkTS的方法。
AI分析日志的结论:Cocos Creator 默认的 native.reflection 桥接方式在 HarmonyOS NEXT 上不兼容,导致 JS 层无法获取原生 XComponent 对象(__NATIVE_XCOMPONENT_OBJ__ 缺失),进而引发一系列 API 调用失败。
cocos的场景中挂载的脚本主要代码如下:
@ccclass('PlayerController')
export class PlayerController extends Component {
start() {
console.log("===Cocos 启动成功");
// 延迟 1 秒等待引擎稳定
this.scheduleOnce(() => {
this.callArkTS();
}, 2.0);
}
callArkTS() {
try {
let result = native.reflection.callStaticMethod(
"entry/src/main/ets/CocosTest",
"testSync",
"hello",
true
);
console.log("===调用成功,返回:" + result);
} catch (e) {
console.error("===调用失败:", e);
}
}
}
构建发布的平台是harmonyOS next,没有勾选“调试模式”。
deveco中代码如下,
export class CocosTest {
public static testSync(msg: string): string {
console.info("===ArkTS 收到:" + msg);
return "===返回:" + msg;
}
}
arkOptions: {
runtimeOnly: {
sources: [
'./src/main/ets/CocosTest.ets',
],
},
}
entry目录下build-profile.json添加的配置如上。
cocos版本3.8.8,HarmonyOS 6.0.2 Release SDK,原样包含OpenHarmony SDK Ohos_sdk_public 6.0.2.130 (API Version 22 Release)。
"targetSdkVersion": "5.0.3(15)"// 6.0.2(22)也尝试过,同样无法解决
希望有大佬能帮忙解决这个问题!!!!
更多关于HarmonyOS鸿蒙Next中Cocos无法调用DevEco项目中的方法的实战教程也可以访问 https://www.itying.com/category-93-b0.html
【解决方案】
开发者您好,因您的问题与三方Cocos相关,为了更快解决您的问题,建议前往Cocos论坛反馈咨询。
更多关于HarmonyOS鸿蒙Next中Cocos无法调用DevEco项目中的方法的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
你这类“Cocos 里调用 ArkTS 方法失败、后端日志提示找不到/拿不到模块”的情况,99% 不是 HarmonyOS NEXT 随机拦截,而是 native.reflection.callStaticMethod 的用法/打包配置不符合 Cocos 的 ArkTS 反射规则。你目前有 3 个关键点不匹配:
1)callStaticMethod 的参数格式你传错了
在 HarmonyOS NEXT 上,Cocos 的 ArkTS 反射调用签名是:
native.reflection.callStaticMethod(clsPath, methodName, paramStr, isSync)
clsPath:ets 脚本路径(例如:entry/src/main/ets/Test)methodName:模块名/函数名(示例里是entry/test,注意带entry/)paramStr:入参字符串(建议 JSON.stringify,空参传'')isSync:true 同步 / false 异步(异步会阻塞等待返回)
你现在这样写:
native.reflection.callStaticMethod(
"entry/src/main/ets/CocosTest",
"testSync",
"hello",
true
);
问题在于:第二个参数缺少模块前缀。建议改成:
let result = native.reflection.callStaticMethod(
"entry/src/main/ets/CocosTest",
"entry/testSync",
JSON.stringify({ msg: "hello" }),
true
);
2)ArkTS 侧“export class + static 方法”很可能不被反射识别
Cocos 官方示例是 导出函数(export { test }),不是导出 class 静态方法。你现在:
export class CocosTest {
public static testSync(msg: string): string { ... }
}
建议改成 直接导出函数(最稳):
// entry/src/main/ets/CocosTest.ets
export function testSync(paramStr: string): string {
console.info("===ArkTS 收到 paramStr:" + paramStr);
return "===返回:" + paramStr;
}
然后 JS 侧照第 1 点那样调用。
如果你非要传多个参数,统一用 JSON 字符串传,ArkTS 再 JSON.parse 即可。
3)你写的 build-profile.json5 配置位置/文件名很可能不生效
Cocos 文档强调:必须把 ets 文件加入 entry/build-profile.json5 的 buildOption.arkOptions.runtimeOnly.sources,否则 callStaticMethod 调不到。
正确形态(注意层级是 buildOption -> arkOptions -> runtimeOnly -> sources,且文件一般是 build-profile.json5 不是 .json):
{
"buildOption": {
"arkOptions": {
"runtimeOnly": {
"sources": [
"./src/main/ets/CocosTest.ets"
]
}
}
}
}
你贴的片段是:
arkOptions: {
runtimeOnly: {
sources: [
'./src/main/ets/CocosTest.ets',
],
},
},
如果它不在 buildOption 下,或者你改错了文件(例如改了 build-profile.json 而工程实际读取 build-profile.json5),Release 包里就不会包含这个 ets,最终表现就是“云调试日志显示找不到/调用失败”。
4)你 AI 提到的 __NATIVE_XCOMPONENT_OBJ__ 不是这条链路的问题
那个通常是 XComponent/渲染组件桥接相关(例如拿原生 XComponent 对象做纹理、渲染回调等)。 你这里走的是 ArkTS 反射 callStaticMethod,优先按上面 1~3 点修正,别被带偏。
建议你按这个“最小可跑用例”验证
ArkTS:entry/src/main/ets/CocosTest.ets
export function testSync(paramStr: string): string {
console.info("===ArkTS 收到:" + paramStr);
return "===返回:" + paramStr;
}
JS:Cocos 脚本
import { _decorator, Component, native } from 'cc';
const { ccclass } = _decorator;
@ccclass('PlayerController')
export class PlayerController extends Component {
start() {
this.scheduleOnce(() => {
const result = native.reflection.callStaticMethod(
"entry/src/main/ets/CocosTest",
"entry/testSync",
JSON.stringify({ msg: "hello" }),
true
);
console.log("===调用成功,返回:", result);
}, 2);
}
}
有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html,
感谢您的建议,我根据建议重新进行了测试,但仍显示“无法调用”
Cocos 2d-x和ArkTS之间的交互,可以参考这篇帖子看看:游戏与 ArkTS 接口交互(Cocos 2d-x)
在HarmonyOS Next中,Cocos无法直接调用DevEco项目方法,因为Cocos使用C++/TypeScript,而DevEco项目默认导出ArkTS接口。需通过NAPI或JSB(JS Binding)显式桥接:将DevEco方法注册为Native模块,并在Cocos侧通过import或requireNativePlugin加载。检查Cocos引擎版本是否支持鸿蒙Next的NAPI规范,另需确认方法签名和线程模型一致。
HarmonyOS NEXT 已经移除对安卓 JNI/反射的兼容,Cocos 默认为 Android/iOS 设计的 native.reflection 通道在纯血鸿蒙上不可用,所以 __NATIVE_XCOMPONENT_OBJ__ 缺失,导致调用失败。
正确的做法是改用 Cocos 为 HarmonyOS 提供的 native.bridge 桥接机制,无需依赖 XComponent 注入对象。
操作步骤
- 在 DevEco 工程的
entry/src/main/ets/pages/Index.ets中,找到XComponent,在它的onLoad回调里注册一个插件:
import { CocosTest } from '../CocosTest'; // 确保路径正确
// 在 XComponent 属性中添加 onLoad
XComponent({
id: 'cocos', // 保持与 Cocos 构建配置中的 XComponent ID 一致
type: XComponentType.SURFACE,
libraryname: 'cocos',
onLoad: (xcomponent) => {
xcomponent.registerPlugin({
nativePlugin: {
callNative: (param: string) => {
// param 为 JS 侧传来的 JSON 字符串
let data = JSON.parse(param);
if (data.cmd === 'testSync') {
let result = CocosTest.testSync(data.msg);
return JSON.stringify({ ret: result });
}
return '';
}
}
});
}
})
- 在 Cocos Creator 的 TS 脚本中,替换
native.reflection为native.bridge:
callArkTS() {
try {
let param = JSON.stringify({ cmd: 'testSync', msg: 'hello' });
let resultStr = native.bridge.sendToNative(param);
console.log("===调用成功,返回:" + resultStr);
} catch (e) {
console.error("===调用失败:", e);
}
}
- 如有需要,将
CocosTest.ets的console.info改为hilog.info以便查看日志。编译运行即可。
注意:
native.bridge.sendToNative的参数和返回值都是字符串,需要自行 JSON 序列化/反序列化。onLoad中的callNative返回值会直接返回给 JS 侧,同样为字符串。- 无需勾选调试模式,也不需要修改
targetSdkVersion。

