• Save
Android Nâng cao-Bài 9-Debug in Android Application Development
Upcoming SlideShare
Loading in...5

Android Nâng cao-Bài 9-Debug in Android Application Development






Total Views
Views on SlideShare
Embed Views



1 Embed 237

http://folami.nghelong.com 237



Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Android Nâng cao-Bài 9-Debug in Android Application Development Android Nâng cao-Bài 9-Debug in Android Application Development Presentation Transcript

  • 1 ANDROID NÂNG CAOANDROID NÂNG CAO Bài 9: Debugging inDebugging in Android Application DevelopmentAndroid Application Development
  • Debug Environment Dev Tools Android application Hierarchy Viewer & layoutopt Traceview & Method profiling ANR & StrictMode Agenda jhat & Heap Profiling
  • Debug Environment
  • Android Debug Bridge Model:  Client  Server Components A client A server A daemon Important commands: -d/-e/-s devices. install/uninstall. pull/push forward shell logcat kill-server Client 1 Client 2 Server Daemon 1 Daemon 2 Daemon 31 5037 5037 5555/5554 5585/5584 5557/5556
  • UI/Application Exercise Monkey The Monkey: generates pseudo-random streams of user events Application: stress-test applications Detect application crashes or receives any sort of unhandled exception. ANR error. Basic usage: $ adb shell monkey [options] <event-count>  Sample: $ adb shell monkey -p your.package.name -v 500  Reference: http://developer.android.com/guide/developing/tools/monkey.html
  • Dalvik Debug Monitor Server  2 ways to start DDMS:  Command line.  Show DDMS perspective in Eclipse.  Using:  Monitoring:  Viewing heap usage for a process.  Tracking memory allocation of objects.  Examining thread information.  Starting method profiling.  Using LogCat.  File operation  Work with emulator or device’s file system.  Emulating phone operations and location  Changing network state, speed, latency.  Spoofing calls or SMS text messages.  Setting the location of phone.  Process Management  Execute garbage collection  Interrupt process  Reference: http://developer.android.com/guide/developing/debugging/ddms.html
  • adb logcat command Use to view & follow the content of system log’s buffer. Filter log output: Using filter expression: tag:priority … tag:priority Controll log output format: Using –v switch: brief, process, tag, thread, raw, time, long. Viewing Alternative Log Buffers : Using –b switch: radio, event, main. Viewing stdout and stderr: setprop log.redirect-stdio true or edit /data/local.prop Reference: http://developer.android.com/guide/developing/tools/adb.html
  • Device DDMS & Debuggers DDMS adb Connect VM monitoring service VM JDWP Debugger (jdb) VM start/stop adbd VM’s PID Monitor 8700 8600
  • Hierarchy Viewer & layoutopt Purpose: Debugging & profiling user interface Tools: Hierarchy Viewer. layoutopt Reference: http://developer.android.com/guide/developing/debugging/debugging-ui.html
  • Hierarchy Viewer Purpose: Debugging &optimize user interface Running HV: Connect your device or launch an emulator. Install the application you want to work with. Run the application, and ensure that its UI is visible. Launch hierarchyviewer The first window you see displays a list of devices and emulators. Select the name of your Activity from the list. You can now look at its view hierarchy using the View Hierarchy window, or look at a magnified image of the UI using the Pixel Perfect window
  • Hierarchy Viewer •TreeView:
  • Hierarchy Viewer •Pixel Perfect window:
  • layoutopt Purpose: analyze XML layout file to find inefficiences. Running layoutopt: layoutopt <xmlfile> Information display when effect is detected: The filename  The line number.  The description.
  • TraceView & Method Profiling  TraceView: a graphical viewer for execution logs that you create by using the Debug class to log tracing information in your code.  Application:  Method profiling to find the bottle neck in your code execution.  Usage:  Create trace file:  Using Debug class: // start tracing to "/sdcard/xml.trace" Debug.startMethodTracing(“xml"); //Call to the method you want to trace. // stop tracing Debug.stopMethodTracing();  Using method profiling feature of DDMS to generate trace file.  Copy file to host machine.  Viewing trace file in Traceview.  Limitation:  Thread name is not emitted if thread exits during profiling.  Reuses thread ID of VM.  Reference: http://developer.android.com/guide/developing/debugging/debugging-tracing.html
  • TraceView & Method Profiling  Timeline Panel:  Profile Panel:
  • dmtracedump  Generate graphical call-stack diagram from trace log file. Call reference number Inclusive elapsed time Exclusive elapsed time Number of calls dmtracedump [-ho] [-s sortable] [-d trace-base-name] [-g outfile] <trace-base-name>
  • jhat & Heap Profiling  Heap Profiling helps to find the leak memory, speed up the program:  How many instances of a class.  How much memory usage.  Usage:  Set the break point:  Exception break point.  Conditional break point.  Dump HPROF file from DDMS.  Using jhat (java heap analysis tool) to parse hprof file.  Open the favorite web browser and visit: http://localhost:7000
  • jhat & Heap Profiling  Exception breakpoint:  Run > Add Java Exception Breakpoint …  Conditional breakpoint:  Right click on breakpoint > Properties > Check Conditional checkbox > Enter the condition. Read more about MAT:
  • jhat & Heap Profiling  Using DDMS dump hprof file:  Click on “Show heap histogram”:
  • ANR & StrictMode  ANR.  Causes  Activity  BroadcastReceiver  StrictMode: public void onCreate() { if (DEVELOPER_MODE) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() // or .detectAll() for all detectable problems .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() .detectLeakedClosableObjects() .penaltyLog() .penaltyDeath() .build()); } super.onCreate(); }  Reference: http://developer.android.com/guide/practices/design/responsiveness.html
  • Dev Tools Application  Get it from emulator & install into real device.  Application:  Enable some settings in your device for testing & debugging.  Usage:  Launch it > Development Settings  Debug app  Wait for debugger  Show screen updates  Immediately destroy activities  Show CPU usage  Show background  Reference: http://developer.android.com/guide/developing/debugging/debugging-devtools.html
  • Debug Tips  Dump the stack trace: adb shell > ps > kill -3  Display useful info on the emulator screen: View Dev Tools App  Get application and system state information from the emulator: View dumpsys and dumpstate  Get wireless connectivity information: DDMS > Device menu > Dump radio state  Log trace data: Call startMethodTracing()  Log radio data: adb shell>logcat –b radio  Capture screenshots: DDMS> Select device >Screen Capture  Use debugging helper classes: Log, Debug, StrictMode  Garbage collection: Any object the debugger is aware of is not garbage collected until after the debugger disconnects
  • Nguyen Huu Phuoc, MEng. ●  Blog:http://folami.nghelong.com ●  Website http://phuocnh.nghelong.com