Monday, December 22, 2014

Debug Android App in Android Studio

In MS Windows, Android Studio provides debugging options in its IDE.

For some who prefer to use the command line, there is the command adb to call debugging. Example

adb logcat

But if you get an error that is similar to;

'adb' is not recognized as an internal or external command, operable program or batch file.

Then, the adb path must be made known. Here are notes for MS Windows 8, with Android Studio version 1.0.1

Step 1: Copy the path for the Android Platform-Tools.

This is by default 
C:\Users\[YOUR USERNAME]\AppData\Local\Android\sdk\platform-tools

Replace [YOUR USERNAME] with the username in use by your system. This means that the computer lab should have done this, otherwise "Administration" rights is needed.

Step 2: Edit the MS Windows, System Environment's Path variable

Open the Windows Environment Variables in System Properties. Add (or Edit) to the end of Path variable in "System variables" with the value in step 1 above. Close all the boxes and restart the MS Windows all "cmd" terminal.

Step 3: Test

Unplug the Android device OR turnoff the emulator. In the Android Studio's command prompt OR in MS Windows cmd prompt type 2 commands to test;

> adb version 
Android Debug Bridge version 1.0.32
> adb logcat
- waiting for device -

Quick Guide to Using ADB 

Following are basic commands to use the debugger from a terminal. Firstly, plugin a device (or start up an Android emulator). The ">" below is a prompt to enter commands from the command line.

List available emulator or devices
> adb devices
List of devices attached
4df7a2af25be30c1        device

In the above example, "4df7a2af25be30c1" is the attached mobile Android device.

Access the emulator or device shell
> adb shell
or a specific device
> adb -s 4df7a2af25be30c1 shell

From here, you can issue commands within the device. Press Ctrl - D to exit.
One commonly used command is logcat to display logging info of the application. Code learn provides some basic tips.

Display memory usage by application
> adb -s 4df7a2af25be30c1 shell dumpsys meminfo

or by a specific application

> adb -s 4df7a2af25be30c1 shell dumpsys meminfo  com.example.tboxmy.helloworld

No comments:

Blog Archive