Post on 12-Jan-2016
Java Android-8
Imran Shafi
Lecture Contents Debugging Android Projects Java/XML Errors Debugger Logcat Utility Android Debug Bridge (adb) Dalvik Debug Monitor Service(DDMS) Traceview
New Project Create a new project Provide project configuration Everything goes fine (nothing wrong with
configuration)
New Project Window
Oops Something Went Wrong (What?)
Error Identification
Xml Error
Fixing Given Error
Take Care of Imports
Coding Problems
Correcting Source Code
Output…. Whats Wrong Now?
Check Manifest
Check Again
Output…. Problem Still There….
Try This …
Output
What did we learn? XML Problems Source Code Problems Manifest Entries API Problems Protocols ….
ExceptionWhile navigating through
any application one can come across some exception
Exception screen with force close option
LogcatEclipse IDE Window (Menu) Show View Other Android LogcatInformation Shown: Time, pid, tag, Message
Logcat CommandsV (Verbose)
Shows everything
D (Debug)Shows debug info, warnings, errors
I (Information)Shows information, warnings, errors
W (Warning)Shows warnings and errors
E (Error)Shows errors
How to Log? To write your own entries from your
application into logcat, Android provides methods corresponding to the different entry priorities. The methods are all of the form:
Log.x(String tag, String message, [Throwable exception])
Where x can be either v,d,I,w,e
Example:
try {
mc.animateTo(mMyLocationOverlay.getMyLocation());
} catch (Exception e) {
Log.i("MicroJobs", "Unable to animate map", e);
}
mvMap.invalidate();
Android Debug Bridge (ADB) Android Debug Bridge (adb) is a versatile command
line tool that lets you communicate with an emulator instance or connected Android-powered device. It is a client-server program that includes three components:1) A client, which runs on your development machine. You
can invoke a client from a shell by issuing an adb command. Other Android tools such as the ADT plugin and DDMS also create adb clients.
2) A server, which runs as a background process on your development machine. The server manages communication between the client and the adb daemon running on an emulator or device.
3) A daemon, which runs as a background process on each emulator or device instance.
ADB Client/Server Modules Find adb tool in <sdk>/platform-tools/ An adb client when starts first checks whether there is an
adb server process already running. If not, it starts the server process. Server binds itlself to local TCP port 5037 and listens for commands sent from adb clients
Server then sets up connections to all running emulator/device instances. It locates emulator/device instances by scanning odd-numbered ports in the range 5555 to 5585 (the range used by emulators/devices). Where the server finds an adb daemon, it sets up a connection to that port. Note: Each emulator/device instance acquires a pair of sequential ports — an even-numbered port for console connections and an odd-numbered port for adb connections. For example:Emulator 1, console: 5554; Emulator 1, adb: 5555Emulator 2, console: 5556; Emulator 2, adb: 5557 …
ADB Commandsadb [-d|-e|-s <serialNumber>] <command>
List Devicesadb devicesDisplays a list of devices and emulators that the adb
server knows about. This is a good way to find the TCP/IP port for an emulator or device if you don't already know it. The port number is also displayed in the title of each emulator at the top of its window.
Device Promptadb shellThis connects you with a shell running on the
target and gives you a # prompt. The shell is a simplified Unix-like shell, so you can use the usual shell commands (ls, cat, rm, ps, etc.) to explore the target and make changes as appropriate. Ctrl-D or exit will get you out of the shell and back to your environment on the host.
Install New Package adb install [-l] [-r] packageThis can be used to install or reinstall an
applicationi. The -l option forward-locks the installation
(preventing the application from being copied later to another device)
ii. The -r option reinstalls the application without overwriting the existing application data.
iii. The package must be a valid, signed .apk file for the application to be installed.
Uninstall a packageadb uninstall [-k] package This uninstalls the application with the given
package name. The package parameter needs to be the full name of the package, without the ".apk" extension. So to uninstall TestProject, for example, you'd type:
adb uninstall com.test.mytestproject If you want to keep the application's associated data, you include the -k option
Push to Device adb pushlocal remote This command copies a file from the local name
on the host to the remote name on the target.
Pull from Device adb pullremote localThis is the counterpart to the previous
command, and copies a file from the target to the host.
DDMS: Dalvik Debug Monitor Service
DDMS: Dalvik Debug Monitor Service
DDMS: Dalvik Debug Monitor Service
Traceview Maybe the problem you're trying to debug isn't about
functionality Maybe your application does exactly what it's
supposed to do, but takes too long to do it Wouldn't it be nice to have a way of seeing how the
methods within your classes are interacting, and even to keep track of the relative time spent executing in each method?
Traceview is a utility that allow you just that kind of visibility.
It consists of two parts, one that you enable before running your program and one that you work with after the run in order to diagnose your findings:
Runtime Data Collection One can enable and disable logging for his
application While enabled, routines are linked into
application that create a binary trace file on the target
The trace file records every method instantiation and the time spent in each method
Trace Analysis If you copy the binary trace file from the
target to your host, you can run a trace analysis program that displays all the information from the file in graphical form
You can easily observe which methods are consuming most of the runtime, and drill down into those methods to find out which methods they in turn call and which of them consume the most time.
Collecting Trace Data The routines to perform trace data collection
are provided in the Android Software Development Kit
All you have to do is:1. Import the Debug package (android.os.Debug)
into your application.2. Call startMethodTracing when you want to start
collecting trace information.3. Call stopMethodTracing when you're done.
Collecting Trace Data The tracing routines always write their trace
information to a file on the target's SD card. If you're running on a real device, you need to plug in an SD card. If you're debugging on the emulator, you need to create a virtual SD card and tell the emulator to use it:
Create a virtual SD card with mksdcard.
Create SD Card From the host command prompt, use the
mksdcard utility to create a file that the emulator can use as a virtual SD card:
$ mksdcard -l ANDROID 1024M filename You can create the file anywhere you like, but the root directory for your project is a good place. The utility will allocate a file as big as the size you've given in the mksdcard command (1 GB in the example shown).
Use SD Card Tell the emulator to use the virtual SD card. In Eclipse, choose Window Preferences
Android Launch. You'll see a box there for emulator options. Add the following option:
-sdcard filename Use the complete path to the file, so the emulator can always find it, no matter where it's running from.