如何在Android中以编程方式执行adb shell bugreportz并获取Bug报告
Android 编程方式获取bugreportz并上传服务器的可行方案
问题描述
手动执行adb shell bugreportz可以从Android设备获取Bug报告,但使用以下代码编程实现时失败:
public static String executeBugreportz() { try { Log.d( "BugReport" , "Bug report triggered" ); // Execute the adb shell command Process process = Runtime.getRuntime().exec("adb shell bugreportz"); // Get the input stream to read the output from the executed command BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); StringBuilder output = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { output.append(line).append("\n"); } // Wait for the command to finish executing int exitCode = process.waitFor(); // Return the output if the execution was successful if (exitCode == 0) { Log.d( "BugReport" , "Bug report generated successfully" ); return output.toString(); } else { Log.d( "BugReport" , "Failed to execute adb shell bugreportz" ); return "Failed to execute adb shell bugreportz"; } } catch (Exception e) { e.printStackTrace(); Log.d( "BugReport" , "Exception occurred while executing adb shell bugreportz:" + e.getMessage() ); return "Exception occurred while executing adb shell bugreportz: " + e.getMessage(); } }
核心失败原因
你的代码存在三个致命问题:
- 环境不支持:Android应用沙箱内没有
adb可执行文件,adb是PC端调试工具,设备内部无法直接调用 - 权限缺失:
bugreportz需要android.permission.DUMP系统权限,普通应用默认无法获取,仅系统应用或root权限应用可调用 - 命令路径错误:即使设备内有
bugreportz,直接写命令名会因环境变量问题找不到可执行文件,需指定完整路径
可行实现方案
方案一:系统API调用(仅适用于系统应用)
如果你的应用是系统应用(已签名并入系统分区),Android 11及以上可直接使用官方BugreportManager API:
import android.app.BugreportManager; import android.content.Context; import android.os.Handler; import android.os.Looper; import android.os.OutcomeReceiver; public void generateBugReport(Context context) { BugreportManager bugreportManager = context.getSystemService(BugreportManager.class); bugreportManager.requestBugreport( BugreportManager.BUGREPORT_MODE_FULL, new Handler(Looper.getMainLooper()), new OutcomeReceiver<Void, BugreportManager.BugreportFailure>() { @Override public void onResult(Void result) { Log.d("BugReport", "Bug report generated via API"); // 后续可通过API获取文件路径并上传服务器 } @Override public void onError(BugreportManager.BugreportFailure error) { Log.e("BugReport", "Bug report failed: " + error); } } ); }
方案二:Root权限调用设备内bugreportz
如果设备已root,可直接调用设备内置的bugreportz(路径通常为/system/bin/bugreportz):
public static String executeBugreportzWithRoot() { try { // 用su获取root权限后调用bugreportz Process process = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/bugreportz"}); BufferedReader outputReader = new BufferedReader(new InputStreamReader(process.getInputStream())); BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); StringBuilder output = new StringBuilder(); String line; while ((line = outputReader.readLine()) != null) { output.append(line).append("\n"); } // 读取错误输出用于调试 StringBuilder errorOutput = new StringBuilder(); while ((line = errorReader.readLine()) != null) { errorOutput.append(line).append("\n"); } if (!errorOutput.isEmpty()) { Log.e("BugReport", "Error: " + errorOutput); } int exitCode = process.waitFor(); if (exitCode == 0) { Log.d("BugReport", "Bug report generated"); // 输出内容包含压缩文件路径,如/storage/emulated/0/bugreport-xxxx.zip return output.toString(); } else { Log.d("BugReport", "Failed, exit code: " + exitCode); return "Failed with code: " + exitCode; } } catch (Exception e) { e.printStackTrace(); return "Exception: " + e.getMessage(); } }
方案三:PC端ADB辅助生成并上传
如果应用无法获取足够权限,可在PC端编写脚本,通过ADB桥接完成操作:
# 触发设备生成bugreportz adb shell bugreportz # 提取生成的文件路径 REPORT_PATH=$(adb shell bugreportz | grep OK | cut -d' ' -f2) # 拉取到PC本地 adb pull $REPORT_PATH ./ # 上传到服务器(示例用curl) curl -X POST -F "file=@$(basename $REPORT_PATH)" https://your-server/upload
上传注意事项
- 生成的压缩文件通常存放在内部存储根目录,需申请
READ_EXTERNAL_STORAGE权限(Android 10及以下)或通过MediaStore访问 - 大文件建议使用分块上传,避免网络超时
- 上传前需确认文件生成完成,
bugreportz生成过程可能耗时数分钟
内容的提问来源于stack exchange,提问作者user_8275
相关产品推荐
相关产品推荐

