Java – Android accesses log files from a PC

Android accesses log files from a PC… here is a solution to the problem.

Android accesses log files from a PC

My 6.0.1 app behaves strangely on reboot on 2013 nexus (not root), so I tried writing log files to internal storage. I do socket logging via WiFi connected to the PC, but the problem appears when the power reclamation is there, my application starts when it receives startup completion, so I thought I’d write a log file, so I’m trying:

    String filename="tablet.log";
    File logFile = new File(getFilesDir(), filename);
    p("log file: "+logFile);
    try {
        OutputStream outputStream = new FileOutputStream(logFile);
        outputStream.write("foo\n".getBytes());
        outputStream.close();
        p("wrote to log file: ");
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        InputStream inputStream = new FileInputStream(logFile);
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
        String string=bufferedReader.readLine();
        inputStream.close();
        p("read from log file: "+string);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Logger global=Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
    LoggingHandler.init();
    LoggingHandler.setLevel(Level.WARNING);
    try {
        p("log file get name: "+logFile.getPath());
        Handler handler=new FileHandler(logFile.getPath());
        handler.setLevel(Level.ALL);
        l.addHandler(handler);
        l.warning("added file handler: "+handler);
    } catch(Exception e) {
        l.warning("file handler caught: "+e);
    }

I seem

to be writing a file and seem to have added a file handler, but I don’t see the file when I use adb :

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

Get: OpenDir failed and permission denied

Is there any way to transfer files to PC?

Thanks

Edit: Jared’s suggestion works: adb shell run-as com.tayek.tablet.gui.android.cb7 ls files – files will be displayed!

In addition, adb exec-out run-as package.name cat files/file > file fetches files to the PC.

Looks needed: adb -s %device% shell run-as %package% ls -l/data/data/%package%/files

Solution

Files in /data cannot be read by shell users. 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 files to your

PC, you need to copy the log files to your SD card and then use adb pull to retrieve the files. On Android 5.0+, you can simply do the following:

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

This should work for 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 also: Android adb, retrieve database using run-as

Related Problems and Solutions