1 00:00:00,000 --> 00:00:05,000 So in a couple of movies now I've showed you how to use DDMS to actually view 2 00:00:05,000 --> 00:00:09,000 log information that's coming from your device, or from the emulator, and this 3 00:00:09,000 --> 00:00:14,000 can help you to debug errors that are occurring and also just to look at what's 4 00:00:14,000 --> 00:00:15,000 going on on your device. 5 00:00:15,000 --> 00:00:20,000 Now we are going to get into some more traditional-type debugging and how you go 6 00:00:20,000 --> 00:00:22,000 about debugging your Android applications. 7 00:00:22,000 --> 00:00:25,000 So I've created just a blank project here. 8 00:00:25,000 --> 00:00:26,000 I've called it Debugging. 9 00:00:26,000 --> 00:00:28,000 And we are just going to go into the layout. 10 00:00:28,000 --> 00:00:33,000 I am just going to add a few things in here. So we have our TextView. 11 00:00:33,000 --> 00:00:35,000 I am going to bring in a button. 12 00:00:35,000 --> 00:00:41,000 And then I'll just bring in an EditText field, just so we have some stuff in there. 13 00:00:41,000 --> 00:00:43,000 I'll save that. 14 00:00:43,000 --> 00:00:48,000 Now if we want to be able to debug our application, we need to add something to 15 00:00:48,000 --> 00:00:50,000 our Android manifest file that allows it to do that. 16 00:00:50,000 --> 00:00:55,000 So I am going to open up the manifest and then go to the Application tag. 17 00:00:55,000 --> 00:00:59,000 We can see at the top-right we have this Debuggable property. 18 00:00:59,000 --> 00:01:04,000 We need to set that to true if we want to be able to connect our application to 19 00:01:04,000 --> 00:01:06,000 the debugger here in Eclipse. 20 00:01:06,000 --> 00:01:09,000 Okay, so we have that done. 21 00:01:09,000 --> 00:01:14,000 Now let's go into our Java code, and we are just going to respond to the click 22 00:01:14,000 --> 00:01:18,000 event on that button. 23 00:01:18,000 --> 00:01:22,000 I will create a new instance of Button, 24 00:01:22,000 --> 00:01:26,000 findViewById. There is my button. 25 00:01:26,000 --> 00:01:29,000 Of course, I'll cast it to a button. 26 00:01:29,000 --> 00:01:38,000 Now we're going to set the OnClickListener, and just going to do an anonymous 27 00:01:38,000 --> 00:01:49,000 event handler here and of course let me actually import OnClickListener. 28 00:01:49,000 --> 00:01:53,000 Again, I showed you DDMS, and more specifically, LogCat. 29 00:01:53,000 --> 00:01:57,000 Here we can see LogCat, and again this is logging everything that's actually 30 00:01:57,000 --> 00:01:59,000 happening on our Android device. 31 00:01:59,000 --> 00:02:04,000 So every time, if I hit the Home screen, if I do anything on this device, it's 32 00:02:04,000 --> 00:02:07,000 actually going to be logging certain information. But I can put my own 33 00:02:07,000 --> 00:02:11,000 information in there from my application. 34 00:02:11,000 --> 00:02:16,000 And let's go back to our Java code and to do that, we are going to use the Log 35 00:02:16,000 --> 00:02:20,000 class, and this is in the android.util package. 36 00:02:20,000 --> 00:02:25,000 Now there is bunch of different methods in here that I can use to log stuff to LogCat. 37 00:02:25,000 --> 00:02:30,000 I can choose the d method, which is going to send a debug message, where it passes 38 00:02:30,000 --> 00:02:32,000 in a tag and a message. 39 00:02:32,000 --> 00:02:35,000 I can even actually send a Throwable object if I want to 40 00:02:35,000 --> 00:02:38,000 just log my exception. 41 00:02:38,000 --> 00:02:42,000 We have the e method, which actually logs an exception; 42 00:02:42,000 --> 00:02:44,000 i logs an info message; 43 00:02:44,000 --> 00:02:51,000 v, a verbose message; w actually is a warning; and the funny one down here is 44 00:02:51,000 --> 00:02:56,000 wtf, which is actually What a Terrible Failure, and this is something that's not 45 00:02:56,000 --> 00:02:57,000 supposed to happen on the system. 46 00:02:57,000 --> 00:02:58,000 It's pretty dramatic. 47 00:02:58,000 --> 00:03:01,000 We are just going to use d. 48 00:03:01,000 --> 00:03:06,000 Now for the tag, this can be anything, just so that I can recognize it in LogCat. 49 00:03:06,000 --> 00:03:14,000 So I am just going to do uppercase LEE, and then I'll just say, "thebutton was clicked." 50 00:03:14,000 --> 00:03:18,000 So this is going to log it, but what I want to do is actually I want to set 51 00:03:18,000 --> 00:03:25,000 a break point here so that it will pause my application and allow me to debug my application. 52 00:03:25,000 --> 00:03:28,000 So I am just going to go into the left-hand column. I'm sure you've done this if 53 00:03:28,000 --> 00:03:30,000 you debugged any code before. 54 00:03:30,000 --> 00:03:34,000 I'm just going to double-click it to add a break point, and then I'm going to hit 55 00:03:34,000 --> 00:03:37,000 this little bug up here to debug. 56 00:03:37,000 --> 00:03:43,000 And we'll want to run it as an Android application, and now let's go over to the emulator. 57 00:03:43,000 --> 00:03:46,000 So when this launches up, it's going to show me my user interface. 58 00:03:46,000 --> 00:03:49,000 Now you can see we get a message. It was waiting for the debugger. 59 00:03:49,000 --> 00:03:55,000 Basically, it was waiting for the debugger in Eclipse to connect itself to my application. 60 00:03:55,000 --> 00:03:56,000 So now they are connected. 61 00:03:56,000 --> 00:04:01,000 So now when I click this button, you can see it's automatically brought me to the 62 00:04:01,000 --> 00:04:04,000 Debug perspective here in Eclipse. 63 00:04:04,000 --> 00:04:08,000 Now depending on what you've done before with Eclipse it may pop up a 64 00:04:08,000 --> 00:04:09,000 message saying, "Hey! 65 00:04:09,000 --> 00:04:12,000 Do you want to go to the Debug perspective?" Just click Yes. 66 00:04:12,000 --> 00:04:16,000 But since I am here, now I have all the traditional debug tools that I am used to. 67 00:04:16,000 --> 00:04:20,000 So I can actually go through the different variables in my application. 68 00:04:20,000 --> 00:04:23,000 I can set values for those variables. 69 00:04:23,000 --> 00:04:27,000 I can look at my break points. I can see the code where it's actually stopped at 70 00:04:27,000 --> 00:04:30,000 that break point. And I have LogCat down here. 71 00:04:30,000 --> 00:04:34,000 And one of the things when I maximize this, you'll notice that it hasn't 72 00:04:34,000 --> 00:04:40,000 actually logged out that message, and that's because we're still paused on that break point. 73 00:04:40,000 --> 00:04:45,000 So if I actually click Resume here, so we go back to LogCat, we should now see 74 00:04:45,000 --> 00:04:48,000 the message, and we could see it down here, thebutton was clicked. 75 00:04:48,000 --> 00:04:53,000 This tag is basically just identifying which process, or which application, is 76 00:04:53,000 --> 00:04:55,000 sending this message. 77 00:04:55,000 --> 00:04:59,000 Now one of the things you'll notice about LogCat is it logs a tremendous amount 78 00:04:59,000 --> 00:05:02,000 of information from everything that's happening on the device. 79 00:05:02,000 --> 00:05:06,000 Well, what if I only want to see the information coming from my app? 80 00:05:06,000 --> 00:05:07,000 Well, I can set up a filter here. 81 00:05:07,000 --> 00:05:09,000 Click on this Plus sign. 82 00:05:09,000 --> 00:05:11,000 I'll just call the filter name Lee. 83 00:05:11,000 --> 00:05:16,000 By Log Tag, so if I just want to filter out the messages with the tag of Lee, I 84 00:05:16,000 --> 00:05:21,000 can click OK, and now you can see anything that I've logged with Lee is 85 00:05:21,000 --> 00:05:24,000 actually shown here. 86 00:05:24,000 --> 00:05:30,000 So that's how I log information to LogCat, and now you can see we are still on 87 00:05:30,000 --> 00:05:31,000 this breakpoint and again, 88 00:05:31,000 --> 00:05:35,000 I can do all that the traditional debug-type activities here. 89 00:05:35,000 --> 00:05:41,000 So this is great for traditional debugging of my code, but what about the user interface? 90 00:05:41,000 --> 00:05:45,000 So there's actually a great tool included with the Android SDK that allows you 91 00:05:45,000 --> 00:05:48,000 to debug your layouts and your UI. 92 00:05:48,000 --> 00:05:53,000 So I am just going to disconnect from the debugger here, and I am going to go to 93 00:05:53,000 --> 00:06:00,000 my android-sdk folder, which is in C/Program Files/Android/android-sdk, and then 94 00:06:00,000 --> 00:06:02,000 go into the tools folder. 95 00:06:02,000 --> 00:06:07,000 So the tool is actually called hierarchyviewer, and this is a batch file 96 00:06:07,000 --> 00:06:12,000 on Windows, so I can just double-click it; on Mac you've to launch it from the Command line. 97 00:06:12,000 --> 00:06:16,000 And what this tool does is it shows me all of the applications or processes that 98 00:06:16,000 --> 00:06:20,000 are actually running on my device, and in this case it's the emulator. 99 00:06:20,000 --> 00:06:24,000 And here is the application that I want to debug, and I am going to click 100 00:06:24,000 --> 00:06:26,000 Load View Hierarchy. 101 00:06:26,000 --> 00:06:30,000 And what this is doing is it's going through and inspecting the user 102 00:06:30,000 --> 00:06:33,000 interface of my application. 103 00:06:33,000 --> 00:06:35,000 And now it's displaying it in this tree view. 104 00:06:35,000 --> 00:06:38,000 Now here I can actually drag things around. 105 00:06:38,000 --> 00:06:41,000 I can use my mouse wheel to zoom in and out. 106 00:06:41,000 --> 00:06:48,000 This is basically showing me the structure of my application from a UI perspective. 107 00:06:48,000 --> 00:06:51,000 Now you probably wonder, what are these three colored circles? 108 00:06:51,000 --> 00:06:55,000 Well, this represents the performance, or how fast this thing has actually 109 00:06:55,000 --> 00:06:58,000 rendered. And now there are three steps to that: 110 00:06:58,000 --> 00:07:02,000 there is the measure step, where it measures how much space it's going to need; 111 00:07:02,000 --> 00:07:06,000 then the layout step, where it actually lays out things; and then finally the 112 00:07:06,000 --> 00:07:09,000 draw step, where it actually paints it to the screen. 113 00:07:09,000 --> 00:07:14,000 And obviously, if you see all green, that's good. And this is just relative to 114 00:07:14,000 --> 00:07:16,000 other views in your application. 115 00:07:16,000 --> 00:07:21,000 But if you had yellow or of course red is going to be the worst, 116 00:07:21,000 --> 00:07:26,000 that will then allow you to kind of pinpoint, these are the areas that I need to kind of watch. 117 00:07:26,000 --> 00:07:29,000 And in general, and when you're creating Android applications, you want to keep 118 00:07:29,000 --> 00:07:33,000 your View Hierarchy as flat as possible. 119 00:07:33,000 --> 00:07:36,000 You don't want tons and tons of nested layouts, but I also come through, 120 00:07:36,000 --> 00:07:41,000 I can click this, and I can see in this wireframe view where this actual Button 121 00:07:41,000 --> 00:07:42,000 control is laid out. 122 00:07:42,000 --> 00:07:48,000 I can also go through all of these properties to find out detailed information 123 00:07:48,000 --> 00:07:53,000 about where it's laid out, the size, all these different properties. 124 00:07:53,000 --> 00:07:57,000 I can also double-click this to see kind of a live preview, and we could 125 00:07:57,000 --> 00:08:02,000 see what it looks like on a white background, black, and I can also get some extra information. 126 00:08:02,000 --> 00:08:07,000 So I definitely recommend you come in here to look at your layout hierarchy, 127 00:08:07,000 --> 00:08:12,000 because this can be a really important place to improve the performance of your application. 128 00:08:12,000 --> 00:08:18,000 So there are other tools as well. So this is, I am on the Dev Guide on the SDK 129 00:08:18,000 --> 00:08:20,000 site, in the Debugging section. 130 00:08:20,000 --> 00:08:25,000 And if you want to do really detailed performance tracing of your application, 131 00:08:25,000 --> 00:08:27,000 there is a tool called TraceView. 132 00:08:27,000 --> 00:08:30,000 Now this is more of an advanced tool, so we are not going into it in this 133 00:08:30,000 --> 00:08:34,000 course, but it gives you this Timeline view so you can see over time where the 134 00:08:34,000 --> 00:08:40,000 bottlenecks in your code are, and you do this by adding tracing information in your code. 135 00:08:40,000 --> 00:08:45,000 But this can be a really good way, again if you're experiencing real problems 136 00:08:45,000 --> 00:08:49,000 with your code or if you're creating an application where performance is of the 137 00:08:49,000 --> 00:08:54,000 upmost priority, TraceView is definitely a tool that will help you to do that. 138 00:08:54,000 --> 00:08:58,000 So that's the basics of being able to debug your application here in a 139 00:08:58,000 --> 00:09:01,000 traditional way inside of Eclipse, but then using some of the other tools in the