Wednesday, November 28, 2012

Android beginner tutorial: Part 4 - Running and debugging applications

Today we'll learn how to run and debug our application.

There are two ways to run our Android application - using an emulator, and using a real Android device that is connected to the computer.

First I'll explain how to install and run the application on a real Android device. Connect your device to the computer using a USB cable. You might need to install proper device drivers to do this correctly. Take your phone/tablet, launch Settings > Applications > Deevlopment, then make sure USB debugging item is checked.

Open your project in Eclipse, then in the menu select Run > Run. A window will pop up asking you to select a device or an emulator. Select your device, and click OK. Your application will be installed and launched on the device! By default it's a simple screen with "Hello World" written on it.

You'll also find your application in the menu of your device, with the icon and title you've set. You can launch it from there as well.

The second way to run an application is using an emulator. Go to Window > Android Virtual Device Manager in Eclipse. Hit New, set the name and settings for your virtual device. You're free to play around with the settings, if something is entered incorrectly - you will be informed of the error. When you're done, click OK.

You can now see your device in the list of the Android Virutal Devices tab in the dialog window. You can start it now by selecting it and hitting "Start". You'll have to wait for the system to boot on the virtual device before you can use it. Then you can look around the phone/tablet almost like it's real.

Then go to Run > Run, and select the virtual device you have to launch your application there. If you didn't start your emulator before, you can do it here by ticking "Launch a new Android Virtual Device" radio button and then selecting the AVD.

And that's all you need to do to launch your application!

However, when developing programs, you might need more advanced debugging tools than just an error console.

There are 2 ways to debug Android applications in Eclipse - one is using the java debugger, the other one is using the DDMS (Dalvik Debug Monitor Server).

To use the java debugger, open Eclipse and go to Window > Open Perspective > Debug. You'll find a new button in the top right sector of the Eclipse IDE window labeled "Debug", located near Java button.

Launch your application if it isn't already launched on either your real device or an emulator, and you'll see some activity taking place in the left section of the debug screen. You can select items there and manipulate things like Variables on the right side of the screen. If it's just a simple application like our Hello World, there won't be much to do (or there won't be anything at all to debug).

The Debug feature includes Variables, Breakpoints and LogCat, which are tools that you can use to see values of variables, breakpoints, as well as system log messages in real time.

The Dalvik Debug Monitor Service is a tool that is included with Android SDK and is not present in Eclipse by default.

It can be launched the same way - Window > Open Perspective > DDMS. You'll quickly see that it's more advanced than the standard Debug tool and includes more features. It gives you control of many Android-related objects and processes. You can even explore the files of the device. It's rather complex for me to explain all of its features, I suggest you head over to the official documentation page for DDMS if you want to explore all of its features.

And that's it for this tutorial!

Thanks for reading!

No comments:

Post a Comment