1

Logcat works fine when device is connected to Android Studio. But how to log data when device is disconnected from Android Studio, but I still need to capture some information for later analysis?

vico
  • 17,051
  • 45
  • 159
  • 315

1 Answers1

-2

This does NOT require rooting your device, NOT changing the code of your app, NOT subscribing to a third party service, NOT having your app in Google Play. If you can or are willing to do some of these things, there are other solutions.

However this solution DOES require to have the computer nearby, turned on and connected to the same network where the device is connected (not necessarily on the internet).

Start off by plugging the device into your computer via USB, then run adb devices to make sure the device is there. If it finds more than one device (e.g. emulator and actual phone), disconnect/close everything but the one you want to debug to keep things simpler and not mistaken one device for another. Then, run adb tcpip 5555 to change the android to tcp mode, which will allow debugging over the network (instead of USB).

At that point you need to find the IP address of your phone, and you can do that in various ways. The easiest is running adb shell ip -f inet addr show wlan0 which will respond with something like:

11: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 192.168.x.x/yy brd 192.168.0.255 scope global wlan0
       valid_lft forever preferred_lft forever

Disconnect the device from the computer's USB in the usual way. Connect it again "over the air" by typing adb connect 192.168.x.x, replacing the x with the IP address found above (right after inet). If the device and your computer are on the same network (e.g. same wifi, or mix of wifi and ethernet from the same router) it should connect. Otherwise, you might need to debug the networking, a whole different problem (but usually not necessary).

Sometimes you can connect to the device but it is shown as "offline". Most of the time just waiting one minute will automatically fix the issue. If in a minute or two the issue does not fix itself, you have to debug it, e.g. as described in this question

At this point the device should appear in the drop list of available devices in Android Studio and you should be able to debug it as if it were attached over the USB. However in some cases I have found that the logcat in Android Studio is not updated in this setup. If that happens, run adb logcat and you will see the logcat on the computer. That will have too much information, because includes everything is running on your device, not just your app -- so you will need to filter it, for example with adb shell logcat --pid NNNN where NNNN is the PID of your app. To find such a PID, run adb shell "ps -A | head -1; ps -A | grep -i com.example" (use the java package of your main Activity instead of com.example) which will respond with something like

USER           PID  PPID     VSZ    RSS WCHAN            ADDR S NAME                       
u0_a105       NNNN  1511 4970004 182688 0                   0 S com.example

When done, run adb usb to restore the phone to regular USB debugging.

Davide
  • 17,098
  • 11
  • 52
  • 68