Ray Tayek Ray Tayek - 1 year ago 68
Java Question

android access log file from pc

My 6.0.1 app behaves strangely on 2013 nexus (which is not rooted) when power cycles, so I am attempting to write a log file to the internal storage. I have socket logging over the WiFi working to the PC, but the problem occurs when the power is recycled, and my app starts when it receives a boot complete, so I thought I would write a log file, so I am trying:

String filename="tablet.log";
File logFile = new File(getFilesDir(), filename);
p("log file: "+logFile);
try {
OutputStream outputStream = new FileOutputStream(logFile);
p("wrote to log file: ");
} catch (Exception e) {
try {
InputStream inputStream = new FileInputStream(logFile);
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String string=bufferedReader.readLine();
p("read from log file: "+string);
} catch (Exception e) {
Logger global=Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
try {
p("log file get name: "+logFile.getPath());
Handler handler=new FileHandler(logFile.getPath());
l.warning("added file handler: "+handler);
} catch(Exception e) {
l.warning("file handler caught: "+e);

I seem to be writing the file and the file handler seems to be added, but I can not see the file when I use adb:

D:\AndroidStudioProjects\Cb7>adb -s 0a9196e8 shell ls /data/user/0/com.tayek.tablet.gui.android.cb7

gets: opendir failed, Permission denied

Is there any way to get the file to the PC?


edit: jared's suggestion works: adb shell run-as com.tayek.tablet.gui.android.cb7 ls files - will show me the files!

also, adb exec-out run-as package.name cat files/file > file gets the file to the PC.

looks like one needs: adb -s %device% shell run-as %package% ls -l /data/data/%package%/files

Answer Source

Files in /data are not readable by the shell user. You need to use the run-as command, which will work as long as the debug build is installed.

You can check if the file exists:

adb shell run-as com.tayek.tablet.gui.android.cb7 ls files

To copy the file to your PC, you will need to copy the log file to your SD card and then use adb pull to retrieve the file. On Android 5.0+ you could simply do:

adb shell exec-out run-as package.name cat files/tablet.log > tablet.log

This should work on previous (and current) Android versions:

> adb shell
$ run-as package.name
$ cp files/tablet.log $EXTERNAL_STORAGE/tablet.log
$ exit
> adb pull /sdcard/tablet.log

Obviously, replace package.name with your package name (com.tayek.tablet.gui.android.cb7).

See: android adb, retrieve database using run-as

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download