异常捕获 ACRA 崩溃报告

时间:2022-06-06 13:52:54

介绍

GitHub  

ACRA:Application Crash Report for Android,即 一个Android异常日志收集的开源框架

ACRA is a library enabling是能够 Android Application to automatically post their crash reports to a report server. It is targeted to android applications developers to help them get data from their applications when they crash or behave erroneously行为错误.

特点:

ACRA's notification systems are clean. If a crash occurs, your application does not add user notifications over existing system's crash notifications or reporting features. By default, the "force close" dialog is not displayed anymore, to enable it set alsoReportToAndroidFramework to true.

The user is notified of an error only once, and you might enhance the perceived quality提高感知质量 of your application by defining your own texts in the notifications/dialogs.

Please do not hesitate犹豫 to open defects/enhancements requests in the issue tracker.在问题跟踪器中打开缺陷/增强请求。

配置

基本配置

  • com.android.tools.build:gradle:3.0.0 or later.
  • Acra requires java 8 (native, not RetroLambda or similar):
android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
x
1
android {
2
    compileOptions {
3
        sourceCompatibility JavaVersion.VERSION_1_8
4
        targetCompatibility JavaVersion.VERSION_1_8
5
    }
6
}
def acraVersion = '5.1.3'
1
def acraVersion = '5.1.3'
implementation "ch.acra:acra-http:$acraVersion"//Http
implementation "ch.acra:acra-mail:$acraVersion"//Email
implementation "ch.acra:acra-core:$acraVersion"//Custom
x
 
1
implementation "ch.acra:acra-http:$acraVersion"//Http
2
implementation "ch.acra:acra-mail:$acraVersion"//Email
3
implementation "ch.acra:acra-core:$acraVersion"//Custom
implementation "ch.acra:acra-dialog:$acraVersion"//Dialog
implementation "ch.acra:acra-notification:$acraVersion"//Notification
implementation "ch.acra:acra-toast:$acraVersion"//Toast
//Silent:Add nothing.
x
1
implementation "ch.acra:acra-dialog:$acraVersion"//Dialog
2
implementation "ch.acra:acra-notification:$acraVersion"//Notification
3
implementation "ch.acra:acra-toast:$acraVersion"//Toast
4
//Silent:Add nothing.

可选配置

  • Limiter: (limits how many reports acra sends from one device)
implementation "ch.acra:acra-limiter:$acraVersion"
 
1
implementation "ch.acra:acra-limiter:$acraVersion"
  • Advanced Scheduler: [since 5.2.0-rc1] (controls when reports are sent (e.g. only on wifi) and can restart an application after a crash)
implementation "ch.acra:acra-advanced-scheduler:$acraVersion"
1
1
 
1
implementation "ch.acra:acra-advanced-scheduler:$acraVersion"

编译时配置

Add annotations to your Application class and override the attachBaseContext() method to add ACRA.init(this)
@AcraCore(buildConfigClass = BuildConfig.class)
public class MyApplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        ACRA.init(this);
    }
}
x
1
@AcraCore(buildConfigClass = BuildConfig.class)
2
public class MyApplication extends Application {
3
    @Override
4
    protected void attachBaseContext(Context base) {
5
        super.attachBaseContext(base);
6
        ACRA.init(this);
7
    }
8
}

I addition to除了 the @AcraCore annotation, each plugin you added in the dependencies step provides another annotation, which you have to add to activate and configure that plugin:

PS:如果时重度用户,个人不建议通过注解配置各个插件,因为易读性太差;当然,如果只是简单的使用,不需要进行复杂的 配置,则通过注解配置即可。

运行时配置

Construct a CoreConfigurationBuilder and pass it to ACRA.init:
CoreConfigurationBuilder builder = new CoreConfigurationBuilder(this);
builder.setBuildConfigClass(BuildConfig.class).setReportFormat(StringFormat.JSON);
builder.getPluginConfigurationBuilder(ToastConfigurationBuilder.class).setResText(R.string.acra_toast_text);
ACRA.init(this, builder);
x
1
CoreConfigurationBuilder builder = new CoreConfigurationBuilder(this);
2
builder.setBuildConfigClass(BuildConfig.class).setReportFormat(StringFormat.JSON);
3
builder.getPluginConfigurationBuilder(ToastConfigurationBuilder.class).setResText(R.string.acra_toast_text);
4
ACRA.init(this, builder);
Please note that plugins are disabled if their respective各自的 annotation is not present. You can activate them by calling:
builder.getPluginConfigurationBuilder(ToastConfigurationBuilder.class).setEnabled(true);
1
1
 
1
builder.getPluginConfigurationBuilder(ToastConfigurationBuilder.class).setEnabled(true);
If you use both run- and compile-time configuration, compile-time values will serve as defaults for runtime configuration.

案例

def acraVersion = '5.1.3'
implementation "ch.acra:acra-http:$acraVersion"//Http
implementation "ch.acra:acra-mail:$acraVersion"//Email
implementation "ch.acra:acra-core:$acraVersion"//Custom
implementation "ch.acra:acra-dialog:$acraVersion"//Dialog
implementation "ch.acra:acra-notification:$acraVersion"//Notification
implementation "ch.acra:acra-toast:$acraVersion"//Toast
1
def acraVersion = '5.1.3'
2
implementation "ch.acra:acra-http:$acraVersion"//Http
3
implementation "ch.acra:acra-mail:$acraVersion"//Email
4
implementation "ch.acra:acra-core:$acraVersion"//Custom
5
implementation "ch.acra:acra-dialog:$acraVersion"//Dialog
6
implementation "ch.acra:acra-notification:$acraVersion"//Notification
7
implementation "ch.acra:acra-toast:$acraVersion"//Toast
@AcraCore(buildConfigClass = BuildConfig.class)
public class App extends Application {

	@Override
	protected void attachBaseContext(Context base) {
		super.attachBaseContext(base);
		
		CoreConfigurationBuilder builder = new CoreConfigurationBuilder(this)
				.setBuildConfigClass(BuildConfig.class)
				.setReportFormat(StringFormat.JSON);
		
		builder.getPluginConfigurationBuilder(ToastConfigurationBuilder.class)//吐司
				.setLength(Toast.LENGTH_SHORT)
				.setResText(R.string.acra_crash_tips)//或者setText
				.setEnabled(true);
		
		builder.getPluginConfigurationBuilder(DialogConfigurationBuilder.class)//弹窗
				.setResIcon(R.drawable.icon)
				.setResTheme(R.style.AppTheme)
				.setTitle("标题")//setResTitle
				.setText("内容\n\n呵呵呵呵呵呵呵呵")//setResText
				.setCommentPrompt("CommentPrompt")//注释输入提示符的标签。setResCommentPrompt
				.setEmailPrompt("EmailPrompt")//	setResEmailPrompt
				.setNegativeButtonText("NegativeButtonText")//	setResNegativeButtonText
				.setPositiveButtonText("PositiveButtonText")//	setResPositiveButtonText
				//.setReportDialogClass(BaseCrashReportDialog.class)//自定义CrashReportDialog
				.setEnabled(true);
		
		builder.getPluginConfigurationBuilder(MailSenderConfigurationBuilder.class)//发邮件。测试失败
				.setMailTo("0909082401@163.com")
				.setSubject("主题(崩溃日志)")//或者setResSubject
				.setReportAsFile(true)
				.setReportFileName("附件名称")
				.setEnabled(false);//是通过跳到系统邮箱中,在用户预览并确认后由用户发出去的,而不是在后台默默发送的
		
		builder.getPluginConfigurationBuilder(HttpSenderConfigurationBuilder.class)//HTTP请求。不知道怎么测试
				.setHttpMethod(HttpSender.Method.POST)
				.setConnectionTimeout(30 * 1000)
				.setSocketTimeout(30 * 1000)
				.setHttpHeaders(new HashMap<>())
				.setBasicAuthLogin("")//认证
				.setBasicAuthPassword("")
				.setCertificatePath("")//证书地址。或者setResCertificate
				.setCertificateType("")//证书类型
				.setDropReportsOnTimeout(false)//超时是否删除报告
				.setKeyStoreFactoryClass(KeyStoreFactory.class)//该类创建了可以包含可信证书的密钥库
				.setEnabled(false);
		
		builder.getPluginConfigurationBuilder(NotificationConfigurationBuilder.class)//通知栏。测试失败
				.setResIcon(R.drawable.icon)
				.setTitle("标题")//setResTitle
				.setText("内容\n\n呵呵呵呵呵呵呵呵")
				.setResSendButtonIcon(R.drawable.icon)
				.setResSendButtonText(R.string.acra_crash_tips)
				.setEnabled(false);
		ACRA.init(this, builder);
	}
}
x
1
@AcraCore(buildConfigClass = BuildConfig.class)
2
public class App extends Application {
3
4
 @Override
5
 protected void attachBaseContext(Context base) {
6
  super.attachBaseContext(base);
7
  
8
  CoreConfigurationBuilder builder = new CoreConfigurationBuilder(this)
9
    .setBuildConfigClass(BuildConfig.class)
10
    .setReportFormat(StringFormat.JSON);
11
  
12
  builder.getPluginConfigurationBuilder(ToastConfigurationBuilder.class)//吐司
13
    .setLength(Toast.LENGTH_SHORT)
14
    .setResText(R.string.acra_crash_tips)//或者setText
15
    .setEnabled(true);
16
  
17
  builder.getPluginConfigurationBuilder(DialogConfigurationBuilder.class)//弹窗
18
    .setResIcon(R.drawable.icon)
19
    .setResTheme(R.style.AppTheme)
20
    .setTitle("标题")//setResTitle
21
    .setText("内容\n\n呵呵呵呵呵呵呵呵")//setResText
22
    .setCommentPrompt("CommentPrompt")//注释输入提示符的标签。setResCommentPrompt
23
    .setEmailPrompt("EmailPrompt")// setResEmailPrompt
24
    .setNegativeButtonText("NegativeButtonText")// setResNegativeButtonText
25
    .setPositiveButtonText("PositiveButtonText")// setResPositiveButtonText
26
    //.setReportDialogClass(BaseCrashReportDialog.class)//自定义CrashReportDialog
27
    .setEnabled(true);
28
  
29
  builder.getPluginConfigurationBuilder(MailSenderConfigurationBuilder.class)//发邮件。测试失败
30
    .setMailTo("0909082401@163.com")
31
    .setSubject("主题(崩溃日志)")//或者setResSubject
32
    .setReportAsFile(true)
33
    .setReportFileName("附件名称")
34
    .setEnabled(false);//是通过跳到系统邮箱中,在用户预览并确认后由用户发出去的,而不是在后台默默发送的
35
  
36
  builder.getPluginConfigurationBuilder(HttpSenderConfigurationBuilder.class)//HTTP请求。不知道怎么测试
37
    .setHttpMethod(HttpSender.Method.POST)
38
    .setConnectionTimeout(30 * 1000)
39
    .setSocketTimeout(30 * 1000)
40
    .setHttpHeaders(new HashMap<>())
41
    .setBasicAuthLogin("")//认证
42
    .setBasicAuthPassword("")
43
    .setCertificatePath("")//证书地址。或者setResCertificate
44
    .setCertificateType("")//证书类型
45
    .setDropReportsOnTimeout(false)//超时是否删除报告
46
    .setKeyStoreFactoryClass(KeyStoreFactory.class)//该类创建了可以包含可信证书的密钥库
47
    .setEnabled(false);
48
  
49
  builder.getPluginConfigurationBuilder(NotificationConfigurationBuilder.class)//通知栏。测试失败
50
    .setResIcon(R.drawable.icon)
51
    .setTitle("标题")//setResTitle
52
    .setText("内容\n\n呵呵呵呵呵呵呵呵")
53
    .setResSendButtonIcon(R.drawable.icon)
54
    .setResSendButtonText(R.string.acra_crash_tips)
55
    .setEnabled(false);
56
  ACRA.init(this, builder);
57
 }
58
}
2018-6-8