androidshelladbbug-reporting

How to run "adb shell bugreportz" and get bug report programatically in android


I have to get the bug report and upload from my android device to server. I can get the bug report by manually running adb command "adb shell bugreportz". However I am trying to get the report programmatically I have tried below code

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();
        }
    }

This code didn't work for me. Please let me know how can I achieve this if someone has explored this part.


Solution

  • I am able to generate the report programatically using DevicePolicyManager as follows without running the adb command. The above approach by running the command using runtime worked for system apps.

        private fun requestBugReport(context: Context) {
                val dpm = context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
                val componentName = ComponentName(context, SampleReceiver::class.java)
                val result = dpm.requestBugreport(componentName);
        }