41
Java Android-8 Imran Shafi

Java Android-8 Imran Shafi. Lecture Contents Debugging Android Projects Java/XML Errors Debugger Logcat Utility Android Debug Bridge (adb)

Embed Size (px)

Citation preview

Page 1: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Java Android-8

Imran Shafi

Page 2: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Lecture Contents Debugging Android Projects Java/XML Errors Debugger Logcat Utility Android Debug Bridge (adb) Dalvik Debug Monitor Service(DDMS) Traceview

Page 3: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

New Project Create a new project Provide project configuration Everything goes fine (nothing wrong with

configuration)

Page 4: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

New Project Window

Page 5: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Oops Something Went Wrong (What?)

Page 6: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Error Identification

Page 7: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Xml Error

Page 8: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Fixing Given Error

Page 9: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Take Care of Imports

Page 10: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Coding Problems

Page 11: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Correcting Source Code

Page 12: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Output…. Whats Wrong Now?

Page 13: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Check Manifest

Page 14: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Check Again

Page 15: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Output…. Problem Still There….

Page 16: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Try This …

Page 17: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Output

Page 18: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

What did we learn? XML Problems Source Code Problems Manifest Entries API Problems Protocols ….

Page 19: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

ExceptionWhile navigating through

any application one can come across some exception

Exception screen with force close option

Page 20: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

LogcatEclipse IDE Window (Menu) Show View Other Android LogcatInformation Shown: Time, pid, tag, Message

Page 21: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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

Page 22: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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();

Page 23: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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.

Page 24: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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 …

Page 25: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

ADB Commandsadb [-d|-e|-s <serialNumber>] <command>

Page 26: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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.

Page 27: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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.

Page 28: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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.

Page 29: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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

Page 30: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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.

Page 31: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

Pull from Device adb pullremote localThis is the counterpart to the previous

command, and copies a file from the target to the host.

Page 32: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

DDMS: Dalvik Debug Monitor Service

Page 33: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

DDMS: Dalvik Debug Monitor Service

Page 34: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

DDMS: Dalvik Debug Monitor Service

Page 35: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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:

Page 36: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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

Page 37: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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.

Page 38: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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.

Page 39: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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.

Page 40: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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).

Page 41: Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb)

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.