Presentation is loading. Please wait.

Presentation is loading. Please wait.

Debugging Android Applications

Similar presentations


Presentation on theme: "Debugging Android Applications"— Presentation transcript:

1 Debugging Android Applications

2 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 ©SoftMoore Consulting

3 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. ©SoftMoore Consulting

4 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. ©SoftMoore Consulting

5 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 ©SoftMoore Consulting

6 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 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. ©SoftMoore Consulting

7 Example: Android Device Monitor
©SoftMoore Consulting

8 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 ©SoftMoore Consulting

9 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. ©SoftMoore Consulting

10 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. ©SoftMoore Consulting

11 Example: Creating Toast Messages
Toast toast = Toast.makeText(this, R.string.message, Toast.LENGTH_SHORT); // center toast message in display toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); Note: If the parent view does not fill the entire display, use the following to center the toast in within the parent view: toast.setGravity(Gravity.CENTER, toast.getXOffset()/2, toast.getYOffset()/2); ©SoftMoore Consulting

12 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) ©SoftMoore Consulting

13 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 ©SoftMoore Consulting

14 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(); ©SoftMoore Consulting

15 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 ©SoftMoore Consulting

16 Relevant Links Debugging Debugging with Android Studio Using DDMS
Debugging with Android Studio Using DDMS Reading and Writing Logs Toasts ©SoftMoore Consulting


Download ppt "Debugging Android Applications"

Similar presentations


Ads by Google