如何从我的 Android 应用程序获取崩溃数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/601503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I obtain crash-data from my Android application?
提问by pupeno
How can I get crash data (stack traces at least) from my Android application? At least when working on my own device being retrieved by cable, but ideally from any instance of my application running on the wild so that I can improve it and make it more solid.
如何从我的 Android 应用程序获取崩溃数据(至少是堆栈跟踪)?至少在我自己的设备上工作时通过电缆检索,但理想情况下是从我的应用程序的任何实例中运行,以便我可以改进它并使其更可靠。
回答by Kevin Gaudin
You might try the ACRA (Application Crash Report for Android)library:
您可以尝试ACRA(Android 应用程序崩溃报告)库:
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 是一个库,使 Android 应用程序能够自动将其崩溃报告发布到 GoogleDoc 表单。它面向 android 应用程序开发人员,帮助他们在应用程序崩溃或出现错误时从他们的应用程序中获取数据。
It's easy to install in your app, highly configurable and don't require you to host a server script anywhere... reports are sent to a Google Doc spreadsheet !
它很容易安装在您的应用程序中,高度可配置,并且不需要您在任何地方托管服务器脚本......报告被发送到 Google Doc 电子表格!
回答by Mihai Chintoanu
For sample applications and debugging purposes, I use a simple solution that allows me to write the stacktrace to the sd card of the device and/or upload it to a server. This solution has been inspired by Project android-remote-stacktrace(specifically, the save-to-device and upload-to-server parts) and I think it solves the problem mentioned by Soonil. It's not optimal, but it works and you can improve it if you want to use it in a production application. If you decide to upload the stacktraces to the server, you can use a php script (index.php
) to view them. If you're interested, you can find all the sources below - one java class for your application and two optional php scrips for the server hosting the uploaded stacktraces.
对于示例应用程序和调试目的,我使用了一个简单的解决方案,该解决方案允许我将堆栈跟踪写入设备的 SD 卡和/或将其上传到服务器。这个解决方案的灵感来自 Project android-remote-stacktrace(特别是保存到设备和上传到服务器部分),我认为它解决了 Soonil 提到的问题。它不是最佳的,但它有效,如果你想在生产应用程序中使用它,你可以改进它。如果您决定将堆栈跟踪上传到服务器,您可以使用 php 脚本 ( index.php
) 来查看它们。如果您有兴趣,您可以在下面找到所有来源 - 一个用于您的应用程序的 java 类和两个用于托管上传的堆栈跟踪的服务器的可选 php 脚本。
In a Context (e.g. the main Activity), call
在上下文(例如主活动)中,调用
if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof CustomExceptionHandler)) {
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(
"/sdcard/<desired_local_path>", "http://<desired_url>/upload.php"));
}
CustomExceptionHandler
CustomExceptionHandler
public class CustomExceptionHandler implements UncaughtExceptionHandler {
private UncaughtExceptionHandler defaultUEH;
private String localPath;
private String url;
/*
* if any of the parameters is null, the respective functionality
* will not be used
*/
public CustomExceptionHandler(String localPath, String url) {
this.localPath = localPath;
this.url = url;
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
}
public void uncaughtException(Thread t, Throwable e) {
String timestamp = TimestampFormatter.getInstance().getTimestamp();
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
e.printStackTrace(printWriter);
String stacktrace = result.toString();
printWriter.close();
String filename = timestamp + ".stacktrace";
if (localPath != null) {
writeToFile(stacktrace, filename);
}
if (url != null) {
sendToServer(stacktrace, filename);
}
defaultUEH.uncaughtException(t, e);
}
private void writeToFile(String stacktrace, String filename) {
try {
BufferedWriter bos = new BufferedWriter(new FileWriter(
localPath + "/" + filename));
bos.write(stacktrace);
bos.flush();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendToServer(String stacktrace, String filename) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("filename", filename));
nvps.add(new BasicNameValuePair("stacktrace", stacktrace));
try {
httpPost.setEntity(
new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
}
}
upload.php
upload.php
<?php
$filename = isset($_POST['filename']) ? $_POST['filename'] : "";
$message = isset($_POST['stacktrace']) ? $_POST['stacktrace'] : "";
if (!ereg('^[-a-zA-Z0-9_. ]+$', $filename) || $message == ""){
die("This script is used to log debug data. Please send the "
. "logging message and a filename as POST variables.");
}
file_put_contents($filename, $message . "\n", FILE_APPEND);
?>
index.php
index.php
<?php
$myDirectory = opendir(".");
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}
closedir($myDirectory);
$indexCount = count($dirArray);
sort($dirArray);
print("<TABLE border=1 cellpadding=5 cellspacing=0 \n");
print("<TR><TH>Filename</TH><TH>Filetype</th><th>Filesize</TH></TR>\n");
for($index=0; $index < $indexCount; $index++) {
if ((substr("$dirArray[$index]", 0, 1) != ".")
&& (strrpos("$dirArray[$index]", ".stacktrace") != false)){
print("<TR><TD>");
print("<a href=\"$dirArray[$index]\">$dirArray[$index]</a>");
print("</TD><TD>");
print(filetype($dirArray[$index]));
print("</TD><TD>");
print(filesize($dirArray[$index]));
print("</TD></TR>\n");
}
}
print("</TABLE>\n");
?>
回答by PanosJee
You can also try [BugSense]Reason: Spam Redirect to another url. BugSense collects and analyzed all crash reports and gives you meaningful and visual reports. It's free and it's only 1 line of code in order to integrate.
您也可以尝试[BugSense] Reason: Spam Redirect to another url。BugSense 收集和分析所有崩溃报告,并为您提供有意义和直观的报告。它是免费的,只需 1 行代码即可集成。
Disclaimer: I am a co-founder
免责声明:我是联合创始人
回答by RoflcoptrException
In Android 2.2 it's now possible to automatically get Crash Reports from Android Market Applications:
在 Android 2.2 中,现在可以从 Android Market 应用程序自动获取崩溃报告:
New bug reporting feature for Android Market apps enables developers to receive crash and freeze reports from their users. The reports will be available when they log into their publisher account.
Android Market 应用程序的新错误报告功能使开发人员能够从用户那里接收崩溃和冻结报告。当他们登录到他们的发布者帐户时,这些报告将可用。
http://developer.android.com/sdk/android-2.2-highlights.html
http://developer.android.com/sdk/android-2.2-highlights.html
回答by sooniln
It is possible to handle these exceptions with Thread.setDefaultUncaughtExceptionHandler()
, however this appears to mess with Android's method of handling exceptions. I attempted to use a handler of this nature:
可以使用 处理这些异常Thread.setDefaultUncaughtExceptionHandler()
,但这似乎与 Android 处理异常的方法相混淆。我试图使用这种性质的处理程序:
private class ExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable ex){
Log.e(Constants.TAG, "uncaught_exception_handler: uncaught exception in thread " + thread.getName(), ex);
//hack to rethrow unchecked exceptions
if(ex instanceof RuntimeException)
throw (RuntimeException)ex;
if(ex instanceof Error)
throw (Error)ex;
//this should really never happen
Log.e(Constants.TAG, "uncaught_exception handler: unable to rethrow checked exception");
}
}
However, even with rethrowing the exceptions, I was unable to get the desired behavior, ie logging the exception while still allowing Android to shutdown the component it had happened it, so I gave up on it after a while.
然而,即使重新抛出异常,我也无法获得所需的行为,即在记录异常的同时仍然允许 Android 关闭发生它的组件,所以我在一段时间后放弃了它。
回答by lingareddyk
I see that the question is too old, and hope my answer is helpful for others having the same issue...
我看到这个问题太老了,希望我的回答对其他有同样问题的人有所帮助......
Give Crashlyticsa try. It will give indepth insight into all the crashes on all the devices having your application and send a notification to you through email..And the best part is its completely free to use..
给Crashlytics一试。它将深入了解所有拥有您的应用程序的设备上的所有崩溃情况,并通过电子邮件向您发送通知......最好的部分是它完全免费使用..
回答by lingareddyk
Ok, well I looked at the provided samples from rrainn and Soonil, and I found a solution that does not mess up error handling.
好吧,我查看了 rrainn 和 Soonil 提供的示例,我找到了一个不会弄乱错误处理的解决方案。
I modified the CustomExceptionHandler so it stores the original UncaughtExceptionHandler from the Thread we associate the new one. At the end of the new "uncaughtException"- Method I just call the old function using the stored UncaughtExceptionHandler.
我修改了 CustomExceptionHandler 以便它存储来自我们关联新线程的原始 UncaughtExceptionHandler 。在新的“uncaughtException” - 方法结束时,我只是使用存储的 UncaughtExceptionHandler 调用旧函数。
In the DefaultExceptionHandler class you need sth. like this:
在 DefaultExceptionHandler 类中,您需要某物。像这样:
public class DefaultExceptionHandler implements UncaughtExceptionHandler{
private UncaughtExceptionHandler mDefaultExceptionHandler;
//constructor
public DefaultExceptionHandler(UncaughtExceptionHandler pDefaultExceptionHandler)
{
mDefaultExceptionHandler= pDefaultExceptionHandler;
}
public void uncaughtException(Thread t, Throwable e) {
//do some action like writing to file or upload somewhere
//call original handler
mStandardEH.uncaughtException(t, e);
// cleanup, don't know if really required
t.getThreadGroup().destroy();
}
}
With that modification on the code at http://code.google.com/p/android-remote-stacktraceyou have a good working base for logging in the field to your webserver or to sd-card.
通过对http://code.google.com/p/android-remote-stacktrace上的代码进行修改, 您就有了一个很好的工作基础,可以在现场登录到您的网络服务器或 SD 卡。
回答by Eefret
Google Play Developers Console actually gives you the Stack traces from those apps that have crashed and had sent the reports, it has also a very good charts to help you see the information, see example below:
Google Play Developers Console 实际上为您提供了那些崩溃并发送报告的应用程序的堆栈跟踪,它还有一个非常好的图表来帮助您查看信息,请参见下面的示例:
回答by Julie
I've been using Crittercismfor my Android and iOS apps -- heard about them on techcrunch. Pretty happy with them so far!
我一直在将Crittercism用于我的 Android 和 iOS 应用程序——在 techcrunch 上听说过它们。到目前为止对他们很满意!
回答by alocaly
I made my own version here : http://androidblogger.blogspot.com/2009/12/how-to-improve-your-application-crash.html
我在这里制作了自己的版本:http: //androidblogger.blogspot.com/2009/12/how-to-improve-your-application-crash.html
It's basically the same thing, but I'm using a mail rather than a http connexion to send the report, and, more important, I added some informations like application version, OS version, Phone model, or avalaible memory to my report...
基本上是一样的,但我使用邮件而不是 http 连接来发送报告,更重要的是,我在报告中添加了一些信息,如应用程序版本、操作系统版本、手机型号或可用内存。 .