作者都是各自领域经过审查的专家,并撰写他们有经验的主题. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.
Ivan Dimoski
Verified Expert in Engineering

Ivan是一位成功的Android开发人员和顾问,拥有六年开发用户友好应用程序的经验.

PREVIOUSLY AT

Truecaller
Share

Making a basic Android app is easy. 另一方面,制作一款可靠的、可扩展的、健壮的Android应用程序是可行的 quite challenging.

With thousands 从大量不同的制造商生产出来的可用设备, 假设一段代码就能在不同的手机上可靠地工作是天真的.

分割是拥有开放平台的最大权衡, and we pay the price in the currency of code maintenance, 在一款应用通过制作阶段后,这种情况还会持续很长时间.

Why Android error reporting matters

So, what happens when an Android app crashes or becomes non-responsive? 这时会弹出“强制关闭”对话框,让用户知道出错了. If the app was downloaded through Google Play, 系统会提示用户发送一份详细的Android崩溃报告(包括时间)来报告崩溃情况, phone model, Android version, stack trace, etc.),您(开发人员)可以在开发人员控制台中查看, allowing you to address the culprit bug.

这一切听起来都很好——但是Android的默认错误报告存在一个主要问题:用户倾向于不使用它, 让开发者对他们的应用状态一无所知.

这一切听起来都很好,但使用Android默认的错误报告存在一个主要问题:用户倾向于 not to take action when their apps crash; in fact, the majority choose not to send in Android error reports. How then, can you, as a conscientious developer,获得关于应用崩溃和失败的可靠见解?

当用户不发送崩溃报告时,可靠的Android崩溃日志尤为重要.

Introducing ACRA

ACRA stands for “Automated Crash Reporting for Android”. 这是一个免费的库,可以让你用几行代码解决“手动错误报告”的问题. 一旦您实现了库,并且所有内容都已正确初始化, 你将能够自动提取与Google默认相同的Android错误日志(加上一堆添加的自定义选项),而无需用户采取行动.

Beyond that, ACRA允许你选择如何通知用户Android系统崩溃, with the default being silent background reporting, and alternatives including customized dialogs.

Until recently, ACRA was backed by Google Spreadsheet, 也就是说你能在一个文件里收到所有的报告, hosted for free on your Google Drive account. Unfortunately, Google requested that we not use this option in the future, 所以我们剩下了几个发送崩溃报告数据的选择, some of which we will cover in this tutorial:

  • Standard email (still requires user interaction).
  • Custom email/HTTP client (requires extensive setup).
  • Custom back-end (with options ranging from free to commercial solutions).

在本文中,我们将分析其中一种解决方案:将ACRA报告托管在 Cloudant back-end and visualizing the data with acralyzer.

Setting up a Cloudant back-end

The first thing we need to do is register a Cloudant account. 当然,这里有一个陷阱:Cloudant的服务并不是完全免费的,而是根据他们的需求 pricing page 你不太可能超过每月5美元的限额(除非你有庞大的用户群和大量的代码漏洞)。.

一旦我们注册了,我们需要了解事情是如何运作的. 在高层次上,我们的后端将由两个组件组成:

  1. A storage database or, to be more precise, an Apache CouchDB. CouchDB stores its data in JSON format, 这意味着从Android设备发送的所有报告必须匹配格式才能作为条目插入. A database insert is a simple HTTP POST or PUT request.
  2. A web app (for analysis) or, to be more precise, a CouchApp. 这是一个简单的JavaScript应用程序,它允许您运行查询并显示存储在CouchDB实例中的数据.

为了使后端正常工作,我们需要设置这两个组件. In theory, we could build the database and the app from source, 然后使用一个工具将它们部署到我们的后端——但是ACRA的优秀人员已经为我们完成了这些工作. 因此,最简单的方法是复制远程数据库和远程应用程序.

Let’s go ahead and replicate an empty ACRA CouchDB:

  • 选择clouddant仪表板中的“Replication”部分.
  • As the source database, select ‘Remote database’ with http://get.acralyzer.com/distrib-acra-storage as the URL.
  • As the target database, 选择' New database '并命名为" acra-{myapp} "(不带引号). 注意,{myapp}参数对于你的应用来说应该是唯一的, and that the database name must start with “acra-“.
  • Click ‘Replicate’.

这演示了如何设置Cloudant后端来管理Android崩溃报告.

因此,我们已经成功地复制了用于报表存储的数据库. 接下来,我们需要复制裂解器CouchApp,以便我们可以可视化数据:

  • 选择clouddant仪表板中的“Replication”部分.
  • As the source database, select ‘Remote database’ with http://get.acralyzer.com/distrib-acralyzer as the URL.
  • 作为目标数据库,选择“New database”并将其命名为“acralyzer”。.
  • Click ‘Replicate’.

这一步包括复制acra应用程序来可视化Android崩溃日志数据.

Note: replicating the acralyzer app is optional. 如果你只对存储Android崩溃报告感兴趣,你就不需要它了, 而不是可视化数据(我们将在本Android教程的下一节中更深入地了解acralyzer). 如果您对自己的JavaScript技能有足够的信心, you could even write your own analytics app! But that’s outside the scope of this blog post.

初始设置过程的最后一步是添加安全权限. Cloudant在CouchDB上提供了自己的安全层,对个人权限进行了更精细的控制, 因此,为了向数据库写入报告,我们需要创建一个具有写入权限的用户帐户:

  • Select the ‘Database’ section in your Cloudant dashboard.
  • 单击acra-{myapp}数据库的权限部分(锁定图标).
  • Click ‘Generate API keys’.
  • 写下生成的用户名和密码(我们将在后面使用它们).
  • Add write permissions for the generated username.

添加安全权限,以便以后可以访问Android崩溃日志和报告.

Visualizing Android crash reports with acralyzer

一旦复制,可以通过以下操作轻松访问化粪池仪表板 http://{myapp}.cloudant.com/acralyzer/_design/acralyzer/index.html#/dashboard. 我承认:它不是最漂亮的分析工具,但它达到了它的目的.

From the top menu, you can select which database you want to visualize (it is possible to host multiple databases for different apps in a single project; this will affect your usage quota) and preview the data in the main dashboard. For example, you can:

  • 按时间单位(小时、日、月等)绘制报告数量.).
  • 通过Android特定的指标(Android版本)查看报告的分布, SDK version, app version, device, etc.).
  • 列出所有崩溃报告(带有详细的堆栈跟踪)或查看所有bug(在这里), “bug”是一组来自不同用户的相同报告).
  • 预览单个bug的详细信息,并将其状态设置为已解决(如果已修复).
  • Purge old or obsolete entries.

可视化Android崩溃日志数据可以帮助你更有策略地改进应用.

请注意,用于可视化的Android崩溃指标将取决于我们选择从应用程序发送的报告. ACRA offers a variety of report fields,其中一些可能相当大,或者与bug修复不完全相关. 对于大多数项目,必需的报告字段就足够了. These include:

  • APP_VERSION_CODE
  • APP_VERSION_NAME
  • ANDROID_VERSION
  • PACKAGE_NAME
  • REPORT_ID
  • BUILD
  • STACK_TRACE

Implementing ACRA in your Android project

As previously mentioned in this tutorial, 实施ACRA非常简单,只需要几个快速的步骤.

Add dependency

首先,我们需要通过以下方式之一将库作为依赖项包含进来:

  • As a .jar file in your /libs folder.
  • As a maven dependency:

    
        ch.acra
        acra
        X.Y.Z
    
    
  • As a gradle dependency:

    compile 'ch.acra:acra:X.Y.Z'
    

Add Application class

Next, 我们需要在我们的项目中添加一个Android Application类(或者更新一个现有的类), 因为只能有一个实例)并在AndroidManifest中声明它.xml:


    ...

And setup ACRA there:

@ReportsCrashes(
    formUri = "http://{myusername}.cloudant.com/acra-{myapp}/_design/acra-storage/_update/report",
    reportType = HttpSender.Type.JSON,
    httpMethod = HttpSender.Method.POST,
    formUriBasicAuthLogin = "GENERATED_USERNAME_WITH_WRITE_PERMISSIONS",
    formUriBasicAuthPassword = "GENERATED_PASSWORD",
    formKey = "", //这是向后兼容所必需的,但没有使用
    customReportContent = {
            ReportField.APP_VERSION_CODE,
            ReportField.APP_VERSION_NAME,
            ReportField.ANDROID_VERSION,
            ReportField.PACKAGE_NAME,
            ReportField.REPORT_ID,
            ReportField.BUILD,
            ReportField.STACK_TRACE
    },
    mode = ReportingInteractionMode.TOAST,
    resToastText = R.string.toast_crash
)

public class MainApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        //下面一行触发ACRA的初始化
        ACRA.init(this);
    }
}

That’s it! Of course, 您需要将所有{myapp}占位符替换为实际值, as well as values for formUriBasicAuthLogin and formUriBasicAuthPassword.

从上面的代码片段中可以看到,我们只使用了必需的报表字段. 请随意添加与您的申请相关的任何其他字段.

You can also choose to use PUT instead of POST. In that case, the REPORT_ID will be appended to the end of the former as a parameter.

Finally, 你还可以选择如何通知用户Android应用程序崩溃, the default being a silent background report. In our case, 我们选择显示一条Toast消息,让用户知道已经报告了崩溃,并且应该很快就可以修复错误.

Need help? Here’s a sample project

To see ACRA in action, I’ve setup the acra_example repo on GitHub. 它的特点是一个简单的应用程序,在启动时初始化ACRA,让你通过按下一个按钮(然后触发空指针异常)来崩溃它。. 崩溃数据被发送到一个可以可视化的示例Cloudant数据库 here.

要查看数据,请使用以下凭据登录:

  • Username: medo
  • Password: acraexample

Alternatives to ACRA

ACRA并不是Android自动错误报告的唯一选择. Since crashes are bound to happen, 有一个巨大的企业对开发者(B2D)市场试图通过他们的解决方案赚钱.

Crittercism例如,是一个非常成熟的崩溃报告平台. 它看起来很棒,为数据分析提供了很多选项,而且非常容易集成. The only downside: price, 免费试用版的活跃用户数量相当有限, days of data retention, and support). BugSense is a similar service.

In my opinion, however, Crashlytics is a superior solution. Until recently, Crashlytics had a freemium model (with a paid premium tier); but now (after their acquisition by Twitter), all the formerly-premium features are available for free. There are no usage costs, fees, or limits. 这是许多高知名度和高排名公司和开发人员的首选错误报告方法, 因为它非常容易使用,并提供了强大的分析和可视化工具. It even integrates with most popular IDEs as a plugin (e.g., Eclipse, Android Studio), 所以在你的应用中添加Crashlytics就像选择一个项目并按下一个按钮一样简单. 这些插件还使您无需打开浏览器就可以从IDE跟踪崩溃报告.

So, 为什么要使用ACRA呢?既然有其他看起来更好的替代方案,并且可以为相同的实现工作提供更多的功能? I’ll give you two reasons.

  1. All these other options are closed source, proprietary software. 即使有满满一篮子的eula,你也不能完全确定 how your data is gathered and handled. On the other hand, ACRA和acralyzer是托管在GitHub上的开源项目,你可以很容易地分叉和定制你的需求.

  2. Data mobility. Let’s say you’re unsatisfied with Cloudant. 将数据复制和迁移到另一个后端非常简单. You’re guaranteed that the data stays yours.

与许多选择一样,这一选择归结为个人偏好和熟悉程度. Check out this Google+ thread 有关使您的应用程序更可靠的各种替代方案的更多讨论.

In conclusion

ACRA is a highly robust and highly customizable 库,可以与Cloudant和acralyzer一起使用,以实现免费, 自动崩溃报告和应用程序的基本分析, all for minimal implementation effort.

编写可靠的Android代码需要大量的经验和远见, but none of us are truly omniscient. 为意外的崩溃和错误做好准备,并做好准备 fix the unexpected as soon as possible. 这是一种为伟大的产品和伟大的用户体验所做的工作.

Hire a Toptal expert on this topic.
Hire Now
Ivan Dimoski

Ivan Dimoski

Verified Expert in Engineering

Stockholm, Sweden

Member since December 11, 2013

About the author

Ivan是一位成功的Android开发人员和顾问,拥有六年开发用户友好应用程序的经验.

作者都是各自领域经过审查的专家,并撰写他们有经验的主题. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.

PREVIOUSLY AT

Truecaller

World-class articles, delivered weekly.

By entering your email, you are agreeing to our privacy policy.

World-class articles, delivered weekly.

By entering your email, you are agreeing to our privacy policy.

Toptal Developers

Join the Toptal® community.