Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to print to the console in an Android app?

Can I run an Android app through the emulator and make it print strings to my computer's console? By console I mean the standard place you would expect to see a System.out.println() in a normal java application. So if you ran the java application from the command prompt then you will see the println()s in the command prompt or if you ran the program in eclipse you will see it in the Console tab at the bottom.

like image 865
Anton Avatar asked Jun 23 '10 14:06

Anton


People also ask

Does Android have a console?

You can interact with the Chrome browser on your Android phone via the left panel in this window or you can interact with it on your device so long as you do not disconnect it from your computer. At the bottom right, you will see the console for it.

Where is the console in Android Studio?

in the upper menu of Android Studio. In the bottom status bar, click 5: Debug button, next to the 4: Run button. Now you should select the Logcat console. Check this article for more information.


2 Answers

Use Log.d("YourTag", "YourOutput");

see http://developer.android.com/reference/android/util/Log.html

like image 195
Mathias Conradt Avatar answered Sep 21 '22 18:09

Mathias Conradt


By default, the Android system sends stdout and stderr (System.out and System.err) output to /dev/null. In processes that run the Dalvik VM, you can have the system write a copy of the output to the log file. In this case, the system writes the messages to the log using the log tags stdout and stderr, both with priority I.

To route the output in this way, you stop a running emulator/device instance and then use the shell command setprop to enable the redirection of output. Here's how you do it:

$ adb shell stop $ adb shell setprop log.redirect-stdio true $ adb shell start 

The system retains this setting until you terminate the emulator/device instance. To use the setting as a default on the emulator/device instance, you can add an entry to /data/local.prop on the device.

You can find more information on this in the Android Debug Bridge document.

You could also create your own class to print to the console http://tech.chitgoks.com/2008/03/17/android-showing-systemout-messages-to-console/

I think this question has been answered on StackOverflow already How to output LogCat to Console?

like image 32
Ally Avatar answered Sep 18 '22 18:09

Ally