debugging android applications. tools for debugging an android application the android studio...

16
Debugging Android Applications

Upload: lynette-merritt

Post on 21-Dec-2015

217 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

Debugging Android Applications

Page 2: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

Tools for Debugging an Android Application

• The Android Studio Debugger

• Android Device Monitor– encapsulates the Dalvik Debug Monitoring Server (DDMS) and

other debug tools

• Android Debug Bridge (adb)

• Toast messages

• Class Log and the LogCat utility

Slide 2

Page 3: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

The Android Studio Debugger

• Android Studio enables you to debug apps running on the emulator or an Android device.

• With Android Studio, you can– Select a device to debug your app on.– View the system log.– Set breakpoints in your code.– Examine variables and evaluate expressions at run time.– Run the debugging tools from the Android SDK.– Capture screenshots and videos of your app.

Slide 3

Page 4: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

Using the Android Studio Debugger

• Build an APK signed with a debug key and install it on a physical Android device or on the Android emulator.

• To debug an app in Android Studio:– Open the project in Android Studio.– Click Debug in the toolbar.– On the Choose Device window, select a hardware device from

the list or choose a virtual device.– Click OK. The app starts on the selected device.

Slide 4

Page 5: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

The Android Device Monitor

• A stand-alone tool that provides a graphical user interface for several Android application debugging and analysis tools.

• Does not require installation of an integrated development environment.

• Encapsulates the following tools:– DDMS– Tracer for OpenGL ES– Hierarchy Viewer– Systrace– Traceview– Pixel Perfect magnification viewer

Slide 5

Page 6: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

Starting the Android Device Monitor

• From Android Studio– click on the Android Device Monitor toolbar icon or use the menu

item Tools → Android → Android Device Monitor

• From the command line, type monitor or double click monitor.bat in the tools subdirectory of the SDK

Slide 6

Note: If you encounter problems running the Android Device Monitor or any of the other debugging tools, try the following:− update Android Studio (Help → Check for Update)− add the tools and the platform-tools subdirectories of the SDK to your PATH environment variable.

Page 7: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

Example: Android Device Monitor

Slide 7

Page 8: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

The Android Debug Bridge(adb)

• The Android Debug Bridge (adb) is a command line tool located in the platform-tools directory.

• Sample adb commands:– adb shell

• create a Linux command-line shell• use exit to exit the shell

– adb shell mkdir /sdcard/app_bkup/• single command to create a shell, make a directory, and exit

– adb push VSwithKeyboard.apk /sdcard/app_bkup/• copy a file to the device

Slide 8

Page 9: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

Toast Messages

• Class Toast (in package android.widget) can be used to display a brief message to the user.

• When the view is shown to the user, it appears as a floating view over the application. It will never receive focus.

• Toast examples– volume control– brief message saying that your settings have been saved

• Toast messages can be used both for debugging and as part of an application.

Slide 9

Page 10: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

Creating Toast Messages

Toast messages are usually created by calling one of the static methods in Toast. static Toast makeText

(Context context, int resId, int duration)

Make a standard toast that just contains a text view with the text from a resource.

static Toast makeText (Context context, CharSequence text, int duration)

Make a standard toast that just contains a text view.

Notes:– Parameter context is usually “this”.– Duration is usually one of Toast.LENGTH_SHORT or Toast.LENGTH_LONG.

Slide 10

Page 11: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

Example: Creating Toast Messages

Toast toast = Toast.makeText(this, R.string.message, Toast.LENGTH_SHORT);

// center toast message in displaytoast.setGravity(Gravity.CENTER, 0, 0);toast.show();

Slide 11

Note: If the parent view does not fill the entire display, usethe following to center the toast in within the parent view:toast.setGravity(Gravity.CENTER, toast.getXOffset()/2, toast.getYOffset()/2);

Page 12: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

The Log Class

• The Log class (in package android.util) can be used to “log” messages at runtime.

• Static methods are used to log messages of various severity levels:– Log.e(String tag, String msg): Log an error– Log.w(String tag, String msg): Log a warning– Log.i(String tag, String msg): Log an informational

message– Log.d(String tag, String msg): Log a debugging message– Log.v(String tag, String msg): Log a verbose message– Log.wtf(String tag, String msg): Log a “What a Terrible

Failure” message (to report an exception that should never happen)

Slide 12

Page 13: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

The Log Class(continued)

• The “tag” parameter for log methods is usually something like the name of the activity posting the log.

• There is an alternate version of each log method that has a third parameter of type Throwable; e.g.,– Log.e(String tag, String msg, Throwable tr): Log an error

message and an exception

Slide 13

Page 14: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

Example: Using the Log Class

private static final String LOG_TAG = "FileStorage";private static final String FILE_NAME = "datafile.txt";

...try { FileInputStream fis = openFileInput(FILE_NAME); ... // do something with the file input stream }catch (FileNotFoundException ex) { String errorMsg = FILE_NAME + " not found"; Log.e(LOG_TAG, errorMsg, ex); Toast toast = Toast.makeText(FileStorage.this, errorMsg, Toast.LENGTH_SHORT); toast.show(); }

Slide 14

Page 15: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

The LogCat Utility

• Developers can view Log messages in Android Studio by clicking on the “6: Android” icon on the bottom of the IDE.

• The view can be filtered by log level severity or by the tag specified in the method call.

• Log messages can also be viewed– in the debugger– in the Android Device Monitor– by using the Android Debug Bridge

(run the command adb logcat

Slide 15

Page 16: Debugging Android Applications. Tools for Debugging an Android Application The Android Studio Debugger Android Device Monitor –encapsulates the Dalvik

©SoftMoore Consulting

Relevant Links

• Debugginghttp://developer.android.com/tools/debugging/index.html

• Debugging with Android Studiohttp://developer.android.com/tools/debugging/debugging-studio.html

• Using DDMShttp://developer.android.com/tools/debugging/ddms.html

• Reading and Writing Logshttp://developer.android.com/tools/debugging/debugging-log.html

• Toastshttp://developer.android.com/guide/topics/ui/notifiers/toasts.html

Slide 16