1. 程式人生 > >安卓崩潰資訊收集框架ACRA

安卓崩潰資訊收集框架ACRA

版權宣告:本文為博主高遠原創文章,轉載請註明出處:http://blog.csdn.net/wgyscsf    https://blog.csdn.net/wgyscsf/article/details/54314899
簡介

ACRA is a library enabling Android Application to automatically post their crash reports to a GoogleDoc form. It is targetted to android applications developers to help them get data from their applications when they crash or behave erroneously. 
ACRA is used in 2.68% (See AppBrain/stats) of all apps on Google Play as of Feb 2016. That’s over 53K apps using ACRA. And since the average US user has 41 apps installed on their phone that means there is a 70% chance that ACRA is running on any phone. That means ACRA is running on over a billion devices.
ACRA是一個自動收集崩潰日誌到谷歌表單的庫。它幫助安卓開發者在他們程式崩潰或者執行錯誤時獲取異常資料。 
截止2016年2月,ACRA在google play中使用率佔2.68%。這意味著有53K款app使用了ACRA。由於美國使用者平均有41的應用程式安裝在手機上,意味著ACRA在每臺手機上執行的概率為70%。這意味著執行著ACRA的裝置超過10億臺。
ACRA是一個開源的Android平臺程式崩潰資訊收集小程式,可以嵌入到Android Project中,當該程式崩潰的時候ACRA能夠在程序徹底結束前收集崩潰狀態時的該應用和裝置的各種資訊,傳送到搭建好的服務端,便於開發者進行程式錯誤資訊的收集,開發者可以更好的改程序序提高相容性。

官網 http://www.acra.ch/

開源地址 https://github.com/ACRA/acra
文件 https://github.com/ACRA/acra/wiki/BasicSetup
ACRA成就


初衷

官方文件為純英文版,並且描述並不是一下子就能看懂。特別是自定義傳送資料到指定伺服器。特測試並記錄。
網上資料沒有自定義資料轉化為表的形式,手動建立了對應資料的表,方便錄入資料。
後端資料庫已處理好,可以直接使用。
演示工程已可以直接使用。
客戶端配置

crash 以toast形式彈出

//傳送到伺服器,Toast形式
@ReportsCrashes(formUri = "http://192.168.1.160/AcraServiceDemo/CrashApiAction",
//formUriBasicAuthLogin = "yourlogin", // optional
//formUriBasicAuthPassword = "y0uRpa$$w0rd", // optional
 mode = ReportingInteractionMode.TOAST,
 reportType= HttpSender.Type.JSON,//配置以json形式傳送
 resToastText=R.string.crash_toast_text)//配置異常時彈出的資訊
1
2
3
4
5
6
7
對話方塊形式形式,會提醒使用者輸入上報資訊。

@ReportsCrashes(formUri = "http://192.168.1.160/AcraServiceDemo/CrashApiAction",
        mode = ReportingInteractionMode.DIALOG,
        resToastText = R.string.crash_toast_text2, // optional, displayed as soon as the crash occurs, before collecting data which can take a few seconds
        resDialogText = R.string.crash_dialog_text,
        resDialogIcon = android.R.drawable.ic_dialog_info, //optional. default is a warning sign
        resDialogTitle = R.string.crash_dialog_title, // optional. default is your application name
        resDialogCommentPrompt = R.string.crash_dialog_comment_prompt, // optional. When defined, adds a user text field input with this text resource as a label
        resDialogOkToast = R.string.crash_dialog_ok_toast, // optional. displays a Toast message when the user accepts to send a report.
        resDialogTheme = R.style.AppTheme_Dialog, //optional. default is Theme.Dialog
        reportType = HttpSender.Type.JSON//配置以json形式傳送
       )
1
2
3
4
5
6
7
8
9
10
11
通知欄形式,點選變為對話方塊形式

// 傳送到伺服器,通知欄形式,點選跳進對話方塊形式收集使用者輸入的資訊,會提醒使用者輸入上報資訊。官方已不再提倡使用:This is a legacy feature, as Dialog mode is generally preferred.
@ReportsCrashes(formUri = "http://192.168.1.160/AcraServiceDemo/CrashApiAction",
       mode = ReportingInteractionMode.NOTIFICATION,
        resToastText = R.string.crash_toast_text2, // optional, displayed as soon as the crash occurs, before collecting data which can take a few seconds
        resDialogText = R.string.crash_dialog_text,
        resDialogIcon = android.R.drawable.ic_dialog_info, //optional. default is a warning sign
        resDialogTitle = R.string.crash_dialog_title, // optional. default is your application name
        resDialogCommentPrompt = R.string.crash_dialog_comment_prompt, // optional. When defined, adds a user text field input with this text resource as a label
        resDialogOkToast = R.string.crash_dialog_ok_toast, // optional. displays a Toast message when the user accepts to send a report.
        resDialogTheme = R.style.AppTheme_Dialog, //optional. default is Theme.Dialog
        resNotifTickerText = R.string.crash_notif_ticker_text,
        resNotifTitle = R.string.crash_notif_title,
        resNotifText = R.string.crash_notif_text,
        resNotifIcon = android.R.drawable.stat_notify_error, // optional. default is a warning sign
        reportType = HttpSender.Type.JSON//配置以json形式傳送
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
傳送郵箱,注意:該操作觸發時會調起使用者客戶端郵箱需要使用者主動傳送。不建議使用。

@ReportsCrashes(mailTo = "[email protected]",
        customReportContent = {ReportField.APP_VERSION_CODE, ReportField.APP_VERSION_NAME,
                ReportField.ANDROID_VERSION, ReportField.PHONE_MODEL, ReportField.CUSTOM_DATA,
                ReportField.STACK_TRACE, ReportField.LOGCAT},//傳送的欄位
        mode = ReportingInteractionMode.TOAST,//異常時彈出資訊的型別
        resToastText = R.string.crash_toast_text)//彈出的文字
1
2
3
4
5
6
服務端配置

// Control
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=UTF-8");
    response.getWriter().print("begin");
    ServletInputStream inputStream = request.getInputStream();
    String str = IOUtils.toString(inputStream, "UTF-8");//json形式接收
    System.out.println(str);
    System.out.println("開始解析");
    processModel(str);
}
1
2
3
4
5
6
7
8
9
10
11
12
資料庫資訊收集


demo程式碼

客戶端多種模式配置 
Toast(可選),然後傳送crash到資料庫
Dialog,然後傳送到伺服器(需要使用者提交資訊,並且手動同意)。
通知欄提醒,然後顯示Dialog,然後傳送到伺服器(需要使用者提交資訊,並且手動同意)。
郵箱形式傳送到指定郵箱
服務端

解析資料為成指定物件
儲存到資料庫
TODO:圖形化介面分析崩潰資訊

工程程式碼:https://github.com/scsfwgy/Acra4CustomService