ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

快速集成崩溃服务SDK和NDK

2022-07-13 09:32:06  阅读:222  来源: 互联网

标签:集成 NDK log getInstance AGConnectCrash 崩溃 btn com SDK


​AppGallery Connect(简称AGC)崩溃服务是一个功能强大、轻量级的崩溃解决方案。它能帮助您快速发现、定位、解决应用崩溃(又称闪退)问题,其使用非常简便,无需开发任何代码即可实现可视化数据报告的实时查看。

下面将为大家介绍如何零代码集成崩溃服务的SDK与NDK来捕捉一般崩溃、native崩溃和异常捕捉,同时简单地创建自定义崩溃报告。

崩溃服务集成准备

1、如果您尚未添加项目,请在AppGallery Connect先创建您的AGC项目并在项目下添加应用,具体请参见Android使用入门

2、在您的项目列表中找到项目,在项目的应用列表中选择需要启用崩溃服务的应用,

3、选择“质量 > 崩溃”,进入崩溃页面。使用崩溃服务需启用华为分析服务。如果您尚未启用,请点击“启动分析服务”,或前往华为分析服务进行启动,具体操作可参考开通华为分析

4、在Android Studio中创建一个工程,将agconnect-services.json文件拷贝到项目的app目录下。

cke_498.png

5、在项目级build.gradle中配置Maven仓地址和AGC插件地址。

buildscript {
    repositories {
        google()
        jcenter()
        maven {url 'https://developer.huawei.com/repo/'}
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.3"
        classpath 'com.huawei.agconnect:agcp:1.6.5.300'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {url 'https://developer.huawei.com/repo/'}
    }
}

6、在应用级build.gradle中添加编译依赖和集成SDK和NDK。

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'

dependencies {
    …
   implementation 'com.huawei.agconnect:agconnect-crash:1.6.5.300'
   implementation 'com.huawei.agconnect:agconnect-crash-native:1.6.4.300'
}

7、同步工程配置

cke_499.png

测试崩溃功能实现

布局设计

参考如下设置布局,具备制造崩溃、制造native崩溃、制造异常、上传自定义报告的功能。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <Button
            android:id="@+id/btn_crash"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="makeCrash" />

        <Button
            android:id="@+id/btn_NDKCrash"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="makeNDKCrash" />

        <Button
            android:id="@+id/btn_exception"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="makeException" />

        <Button
            android:id="@+id/CustomReport"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="CustomReport" />

    </LinearLayout>
</LinearLayout>

效果展示

cke_500.png

功能实现

制造崩溃

Button btn_crash = findViewById(R.id.btn_crash);
btn_crash.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        AGConnectCrash.getInstance().testIt(null);
    }
});

制造native崩溃

Button btn_NDKCrash = findViewById(R.id.btn_NDKCrash);
btn_NDKCrash.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        CrashReport.testNativeCrash();
    }
});

制造并上报异常

Button btn_exception = findViewById(R.id.btn_exception);
btn_exception.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        try{
            throw new Exception();
        }catch (Exception e){
            AGConnectCrash.getInstance().recordException(e);
        } 
    }
});

上报自定义报告

findViewById(R.id.CustomReport).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        AGConnectCrash.getInstance().setUserId("testuser");
        AGConnectCrash.getInstance().log(Log.DEBUG,"set debug log.");
        AGConnectCrash.getInstance().log(Log.INFO,"set info log.");
        AGConnectCrash.getInstance().log(Log.WARN,"set warning log.");
        AGConnectCrash.getInstance().log(Log.ERROR,"set error log.");
        AGConnectCrash.getInstance().setCustomKey("stringKey", "Hello world");
        AGConnectCrash.getInstance().setCustomKey("booleanKey", false);
        AGConnectCrash.getInstance().setCustomKey("doubleKey", 1.1);
        AGConnectCrash.getInstance().setCustomKey("floatKey", 1.1f);
        AGConnectCrash.getInstance().setCustomKey("intKey", 0);
        AGConnectCrash.getInstance().setCustomKey("longKey", 11L);
    }
});

崩溃上报

点击按钮分别触发崩溃、native崩溃、异常捕捉上报和上报自定义崩溃报告。在AGC控制台查看数据情况。

崩溃上报情况:

cke_501.png

Native崩溃上报情况

cke_502.png

异常上报情况:

cke_503.png

自定义崩溃报告上报情况:

cke_504.png

欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

标签:集成,NDK,log,getInstance,AGConnectCrash,崩溃,btn,com,SDK
来源: https://www.cnblogs.com/developer-huawei/p/16472598.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有