CocosCreatorAndroid平台快速接入Bugly指南
发表于|更新于
|阅读量:
摘要
主要是针对打包App后的错误收集,使用腾讯的Bugly进行收集,主要进行记录,方便以后操作,来源cocos中文社区
开始
一、Java层SDK集成
- 创建Bugly代理类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| public class BuglyAgent {
public static void initSDK(Context context, String appId) { CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context.getApplicationContext()); strategy.setDeviceID("userdefinedId"); CrashReport.initCrashReport( context.getApplicationContext(), appId, false, strategy ); }
public static void postException(int category, String name, String reason, String stack) { postException(category, name, reason, stack, null); }
public static void postException( int category, String name, String reason, String stack, Map<String, String> extraInfo) { CrashReport.postException(category, name, reason, stack, extraInfo); } }
|
二、C++层接口绑定
- 修改Cocos Creator项目文件 打开项目路径:
build/jsb-<platform>/frameworks/runtime-src/Classes/AppDelegate.cpp
- 添加Java接口绑定代码
1 2 3 4 5
| se->setExceptionCallback([](const char* location, const char* message, const char* stack){ JniHelper::callStaticVoidMethod("org/cocosTest/BuglyAgent", "postException", 5, "JSError", message, stack); cocos2d::log("\nUncaught Exception:\n - location : %s\n - msg : %s\n - detail : \n %s\n", location, message, stack); });
|
三、关键注意事项
- 包名格式规范
- Java类路径必须使用
/
分隔符(如com/example/bugly/BuglyAgent
)
- 确保与AndroidManifest.xml中声明的包名一致
- 方法签名匹配
- 需严格遵循Java方法签名规则(参数类型+返回值)
- 特别注意
Map<String, String>
需要转换为java.util.Map
- 异常处理
- 建议在调用
postException
前添加空值校验
- 对于C++异常,建议使用try-catch块捕获并转换为Java异常
- 性能优化
- 避免在高频调用场景使用此接口
- 建议设置合理的上报阈值(如每分钟最多上报5次)
该方案通过C++层直接调用Java接口,避免了传统JNI开发的复杂性,同时保持了良好的可维护性。建议在正式环境部署前进行多设备兼容性测试,并根据业务需求调整异常上报策略。