Search code examples
androidloggingcrashdevicelogcat

Read android device logs from my android application


I want to get logs of the whole device, the way logcat shows the processes running in the device. I want to capture it from within my android application.


Solution

  • Your app needs to use the permission android.permission.READ_LOGS. (unsure if this works on unrooted device).

    and you need to grant the permission to your app in runtime to access this permission with:

    adb shell pm grant <pkg> android.permission.READ_LOGS

    (or) programmatically with:

    Runtime.getRuntime().exec("pm grant <pkg> android.permission.READ_LOGS");

    Then you will be able to get the system logs using shell commands for logcat. For example, to get a logcat dump as a txt file in the device:

    Runtime.getRuntime().exec("logcat -d -f" + " /sdcard/Logcat_file.txt");

    will get the logs saved in a txt file in the android device , using which further validation can be done.