Customized Android crash reporting library and background system based on open-source project Acra

Source: Internet
Author: User

Starting point:Open-source Acra crash reporting Library (http://code.google.com/p/acra/) Disadvantages: 1. Crash reported to Google doc, because of the wall, so do not see the data. 2. If the crash is reported by email, it is not convenient to calculate the crash rate. 3. There are too many fields reported by AcrA. You need to filter useless fields.
Purpose:When the android application crashes, you can view the collapsed stack information in the browser in time, and the background can also calculate the daily crash rate to facilitate application experience improvement.
Practice process:
1. First download the Acra source code SVN checkout http://acra.googlecode.com/svn/trunk, import the project in eclipse, in AcrA. Java found that the default reported field content are:

public static final ReportField[] DEFAULT_REPORT_FIELDS = { REPORT_ID, APP_VERSION_CODE, APP_VERSION_NAME,PACKAGE_NAME, FILE_PATH, PHONE_MODEL, BRAND, PRODUCT, ANDROID_VERSION, BUILD, TOTAL_MEM_SIZE,AVAILABLE_MEM_SIZE, CUSTOM_DATA, IS_SILENT, STACK_TRACE, INITIAL_CONFIGURATION, CRASH_CONFIGURATION,DISPLAY, USER_COMMENT, USER_EMAIL, USER_APP_START_DATE, USER_CRASH_DATE, DUMPSYS_MEMINFO, LOGCAT,DEVICE_ID, INSTALLATION_ID, DEVICE_FEATURES, ENVIRONMENT, SHARED_PREFERENCES, SETTINGS_SYSTEM,SETTINGS_SECURE };

2. filter out useless fields. When the android application crashes, only 11 fields need to be counted, as shown below:
Field description

Field

Description

Platform_id

1 = apad, 2 = aphone

Android_version

Android version

App_version_code

The application version code is as follows: 9

App_version_name

The application version name is 1.2.2.

Device_id

Unique device ID

Model

Mobile phone/tablet models, such as bcm63

Brand

Android device brand, such as Samsung

Product

Android product information

Stack_trace

Collapsed stack information

Crash_date

Crash time point

Package_name

Application Package name

Firgure 1 reported field
Add the ing of reported fields in Figure1 to the addreportsenders function in AcrA. Java:

if (conf.formUri() != null && !"".equals(conf.formUri())) {Map<ReportField, String> mapField = new HashMap<ReportField,String>();mapField.put(ReportField.ANDROID_VERSION, "android_version");mapField.put(ReportField.APP_VERSION_CODE, "app_version_code");mapField.put(ReportField.APP_VERSION_NAME, "app_version_name");mapField.put(ReportField.DEVICE_ID, "device_id");mapField.put(ReportField.PHONE_MODEL, "model");mapField.put(ReportField.BRAND, "brand");mapField.put(ReportField.PRODUCT, "product");mapField.put(ReportField.STACK_TRACE, "stack_trace");mapField.put(ReportField.USER_CRASH_DATE, "crash_date");mapField.put(ReportField.PACKAGE_NAME, "package_name");errorReporter.addReportSender(new HttpPostSender(conf.formUri(), mapField));return;}

Modify the remap function in httppostsender. Java as follows:

for (ReportField field : fields) {/* if (mMapping == null || mMapping.get(field) == null) {finalReport.put(field.toString(), report.get(field));} else {finalReport.put(mMapping.get(field), report.get(field));}*/if(mMapping!=null && mMapping.get(field) != null){finalReport.put(mMapping.get(field), report.get(field));}}

Finalreport. Put ("platform_id", AcrA. getconfig (). platformid ());

3. After modification, you only need to add the following lines of code to the application to report the collapsed stack information in real time (this is more convenient than the iOS platform)

@ReportsCrashes(formKey = "", // will not be usedplatformId="1",formUriBasicAuthLogin = "user",formUriBasicAuthPassword = "password",formUri = "http://weiwangzi.com:8080/CrashReport/CrashServerlet")public class TestApplication extends Application {@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();ACRA.init(this);}

4. Implement serverlet to collect the reported crash data to the database

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Connection conn = null;PreparedStatement pstmt = null;try{conn = DBUtil.getConnection();String platformId=request.getParameter("platform_id");String androidVersion=request.getParameter("android_version");String appVersionCode=request.getParameter("app_version_code");String appVersionName=request.getParameter("app_version_name");String deviceId=request.getParameter("device_id");String model=request.getParameter("model");String brand=request.getParameter("brand");String product=request.getParameter("product");String stackTrace=request.getParameter("stack_trace");String crashDate=request.getParameter("crash_date");String packageName=request.getParameter("package_name");pstmt = conn.prepareStatement("insert into report(platform_id, android_version, app_version_code,appversion_name,device_id,model,brand,product,stack_trace,crash_date,package_name) values (?, ?, ?,?,?,?,?,?,?,?,?)");pstmt.setString(1, platformId);pstmt.setString(2, androidVersion);pstmt.setString(3, appVersionCode);pstmt.setString(4, appVersionName);pstmt.setString(5, deviceId);pstmt.setString(6, model);pstmt.setString(7, brand);pstmt.setString(8, product);pstmt.setString(9, stackTrace);pstmt.setString(10, crashDate);pstmt.setString(11, packageName);pstmt.executeUpdate();}catch(Exception e){e.printStackTrace();}finally{if(pstmt!=null){try {pstmt.close();} catch (SQLException e) {// TODO Auto-generated catch block//e.printStackTrace();}}if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch block//e.printStackTrace();}}}}

Practice:You can use a database to export an Excel crash report, or directly view the report in a browser.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.