cocoscreator3.7.2接入bugly

1.先去bugly官网阅读文档home - Bugly 文档

2.andorid接入选择自动集成

在Module的build.gradle文件中添加依赖和属性配置:

android {
    defaultConfig {
        ndk {
            // 设置支持的SO库架构
            abiFilters 'armeabi' //, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
        }
    }
}

dependencies {
    implementation 'com.tencent.bugly:crashreport:latest.release' //其中latest.release指代最新Bugly SDK版本号,也可以指定明确的版本号,例如4.0.3
}

参数配置

  • 在AndroidManifest.xml中添加权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  • 请避免混淆Bugly,在Proguard混淆文件中增加以下配置:
-dontwarn com.tencent.bugly.**
-keep public class com.tencent.bugly.**{*;}

创建BuglyAgent类

package cn.xianyou.bugly;

import android.content.Context;
import android.util.Log;

import com.cocos.game.Configuration;
import com.tencent.bugly.crashreport.CrashReport;


import java.util.Map;

public class BuglyAgent {


    public static void initSDK(Context context, String appId) {
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setAppPackageName(context.getPackageName());
        strategy.setAppChannel(Configuration.channelName);
        strategy.setAppVersion("3.7.2");
        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){
        Log.d("BuglyAgent","reason: " +reason);
        CrashReport.postException(category, name, reason, stack, extraInfo);
    }

}
3.在AppActivity的onCreate初始化bugly
4.BuglyAgent.initSDK(this,"bugly的appid");

5.修改CocosApplication.cpp,路径在:ProgramData\cocos\editors\3.7.2\resources\resources\3d\engine\native\cocos\application

//文件内插入下面这段代码
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <cocos/platform/java/jni/JniHelper.h>
#elif(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "CrashReport.h"
#endif

//修改handleException
void CocosApplication::handleException(const char *location, const char *message, const char *stack) {
    // Send exception information to server like Tencent Bugly.
    CC_LOG_ERROR("\nUncaught Exception:\n - location :  %s\n - msg : %s\n - detail : \n      %s\n", location, message, stack);
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    JniHelper::callStaticVoidMethod("cn/xianyou/bugly/BuglyAgent", "postException", 5, location, message, stack);
#elif(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    CrashReport::reportException(5, location,message, stack);
#endif
}


ios接入

通过CocoaPods集成

在工程的Podfile里面添加以下代码:

pod 'Bugly'

保存并执行pod install,然后用后缀为.xcworkspace的文件打开工程。

创建BuglyAgent.h和BuglyAgent.mm

@interface BuglyAgent : NSObject

+ (void)initSdk:(NSString *)appId;
+ (void)reportExceptionWithCategory:(NSUInteger)category name:(NSString *)name reason:(NSString *)reason callStack:(NSArray *)stackArray  extraInfo:(NSDictionary *)info terminateApp:(BOOL)terminate;

@end


#import "BuglyAgent.h"
#import <Bugly/Bugly.h>

@implementation BuglyAgent

+ (void)initSdk:(NSString *)appId
{
    [Bugly startWithAppId:appId];
}

+ (void)reportExceptionWithCategory:(NSUInteger)category name:(NSString *)name reason:(NSString *)reason callStack:(NSArray *)stackArray  extraInfo:(NSDictionary *)info terminateApp:(BOOL)terminate
{
    [Bugly reportExceptionWithCategory:category name:name reason:reason callStack:stackArray extraInfo:info terminateApp:terminate];
}

@end

创建CrashReport.h和CrashReport.mm


class CrashReport
{
public:
    /**
     *    @brief 上报自定义错误
     *
     *    @param category  错误的分类,5表示Lua错误,6表示JS错误
     *    @param type      错误类型的名称
     *    @param msg       错误原因
     *    @param traceback 错误堆栈
     */
	static void reportException(int category, const char* type, const char* msg, const char* traceback);
    
    static void reportException(int category, const char* type, const char* msg, const char* traceback, bool quit);
    
    CrashReport();
};


#include "CrashReport.h"
#import <Foundation/Foundation.h>


#define BUGLY_CLASS @"BuglyAgent"
#define BUGLY_METHOD_EXCEPTION @"reportExceptionWithCategory:name:reason:callStack:extraInfo:terminateApp:"


CrashReport::CrashReport(){

}

void CrashReport::reportException(int category, const char* type, const char* msg, const char* traceback) {
    reportException(category, type, msg, traceback, false);
}

void CrashReport::reportException(int category, const char* type, const char* msg, const char* traceback, bool quit) {

	NSUInteger pCategory = category;
	NSString * pType = [NSString stringWithUTF8String:type];
	NSString * pMsg = [NSString stringWithUTF8String:msg];
	NSString * pTraceStack = [NSString stringWithUTF8String:traceback];
    NSArray *trackArray = [pTraceStack componentsSeparatedByString:@"\n"];
    NSDictionary * nullObject = nil;
    BOOL terminateApp = quit ? YES : NO;

	Class clazz = NSClassFromString(BUGLY_CLASS);
    
    if (clazz) {
        SEL selector = NSSelectorFromString(BUGLY_METHOD_EXCEPTION);
        NSMethodSignature* signature = [clazz methodSignatureForSelector:selector];
        
        if (signature) {
            //NSLog("Report exception: %@\n%@", msg, traceback);
            
            NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature];
            if (invocation) {
                [invocation setTarget:clazz];
                [invocation setSelector:selector];
                
                [invocation setArgument:&pCategory atIndex:2];
                [invocation setArgument:&pType atIndex:3];
                [invocation setArgument:&pMsg atIndex:4];
                [invocation setArgument:&trackArray atIndex:5];
                [invocation setArgument:&nullObject atIndex:6];
                [invocation setArgument:&terminateApp atIndex:7];
                
                [invocation invoke];
            }
        }
    } else {
        //NSLog("Fail to get class by NSClassFromString(%@)", BUGLY_CLASS.UTF8String);
    }
}

如果android修改过CocosApplication.cpp就不需要修改,如果没有跟加入下面代码

//文件内插入下面这段代码
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <cocos/platform/java/jni/JniHelper.h>
#elif(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "CrashReport.h"
#endif

//修改handleException
void CocosApplication::handleException(const char *location, const char *message, const char *stack) {
    // Send exception information to server like Tencent Bugly.
    CC_LOG_ERROR("\nUncaught Exception:\n - location :  %s\n - msg : %s\n - detail : \n      %s\n", location, message, stack);
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    JniHelper::callStaticVoidMethod("cn/xianyou/bugly/BuglyAgent", "postException", 5, location, message, stack);
#elif(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    CrashReport::reportException(5, location,message, stack);
#endif
}


在AppDelegate.mm的didFinishLaunchingWithOptions初始化bugly

[BuglyAgent initSdk:@"bugly的appid"];

上面android和ios的bugly接入以及完成,如果需要在ts代码上报错误信息,可以另外加入监听


     if (sys.isNative) {
            console.error = function () {
                console.log("console.error=======>", JSON.stringify(arguments));
     native.jsbBridgeWrapper.dispatchEventToNative("doBugly",JSON.stringify(arguments));
            }
        }
        if (sys.isBrowser) {
            window.onerror = function(message, source, lineno, colno, error) {
                let exception : any = {};
                exception.errorMessage = message;
                exception.source = source;
                exception.lineno = lineno;
                exception.colno = colno;
                exception.error = error;

                console.log("window.onerror=======>", message);
native.jsbBridgeWrapper.dispatchEventToNative("doBugly",JSON.stringify(exception));
            }
        }

在andorid创建监听

     JsbBridgeWrapper jbw = JsbBridgeWrapper.getInstance();
        //注册js回调
        jbw.addScriptEventListener("doBugly", jsonString ->{
           BuglyAgent.postException(5,"JSError",jsonString,"");
        });

在ios创建监听

  JsbBridgeWrapper* m = [JsbBridgeWrapper sharedInstance];
    [m addScriptEventListener:@"registerNativeSDKEvent" listener:jslistener];


static OnScriptEventListener jslistener = ^void(NSString* jsonStr){
        NSUInteger pCategory = 5;
        NSString * pType = @"JSError";
        NSString *message = jsonStr;
        NSString *stack = "";
        NSArray *trackArray = [stack componentsSeparatedByString:@"\n"];
        [BuglyAgent reportExceptionWithCategory:pCategory name:pType reason:message     callStack:trackArray extraInfo:nil terminateApp:NO];
}

以上就是cocoscreator3.7.2接入bugly的全部

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值