【发布时间】:2018-12-16 13:36:05
【问题描述】:
为了调试我无法重现的用户问题,我试图在我的应用程序中放置一个按钮,以收集 logcat 输出并通过电子邮件发送它。我遇到了麻烦。
首先,我尝试使用acra。我在一个鼠洞里掉了一会儿。我得到了
java.lang.RuntimeException: com.android.build.api.transform.TransformException: Error while generating the main dex list.
at ...
Caused by: com.android.tools.r8.errors.CompilationError: Program type already present: android.support.v4.widget.EdgeEffectCompat
并阅读有关尝试解决来自不同库的冲突类的信息(例如,this,但我找不到更多)。
我决定尝试一种更简单的方法,概述here:使用 logcat 和电子邮件获取日志的单个函数。一个问题是newer versions of android don't let you grab that file directly,你必须使用 FileProvider。
我最终得到了这个:
/**
* Save logcat in a file and return an Intent that emails it.
*
* @return intent to email log file
*/
private static Intent sendLogcatMail() {
// save logcat in file
File outputFile = new File(Environment.getExternalStorageDirectory(),
"logcat.txt");
try {
// last 2000 lines of the log, in 'time' format
Runtime.getRuntime().exec(
"logcat -v time -t 2000 -f " + outputFile.getAbsolutePath());
} catch (IOException e) {
Log.e(LOGGER_TAG, "Error in logcat:" + Tools.getStackTrace(e));
}
Log.i(LOGGER_TAG, "logcat size: " + outputFile.length());
//send file using email
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Set type to "email"
emailIntent.setType("vnd.android.cursor.dir/email");
String to[] = {SEND_LOGS_EMAIL};
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
// FileProvider
// See https://developer.android.com/reference/android/support/v4/content/FileProvider
// See https://stackoverflow.com/a/38858040/34935
// See https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en
Uri logUri = FileProvider.getUriForFile(Tools.getContext(),
BuildConfig.APPLICATION_ID + ".provider",
outputFile);
// the attachment
emailIntent.putExtra(Intent.EXTRA_STREAM, logUri);
// the mail subject
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "App logs");
return Intent.createChooser(emailIntent , "Send email...");
}
现在,当我在模拟器中运行时,该调试行显示 logcat 文件的大小始终为零,即使我在 Android Studio 中看到了 logcat。我试图在我的 build.gradle 中强调我的应用是可调试的:
release {
// ...
// make sure logcat gets debug output
debuggable true
}
什么都没做。
我如何收集调试日志以通过电子邮件发送给他们,或者以其他方式将它们从远程应用程序获取到我可以看到它们的地方?我完全糊涂了。
【问题讨论】:
-
我建议使用 firebase 崩溃报告,它真的让生活更轻松
-
感谢您的建议。首先,它已被 Crashalytics 弃用:firebase.googleblog.com/2018/03/…。其次,我没有崩溃。该应用程序无法正常运行(在 wifi 上时无法上传内容)。