Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read android device logs from my android application

Tags:

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.

like image 930
surbhi verma Avatar asked Aug 23 '18 09:08

surbhi verma


1 Answers

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.

like image 99
srm Avatar answered Sep 28 '22 17:09

srm