1 00:00:05,440 --> 00:00:06,939 In the previous video we added some 2 00:00:06,939 --> 00:00:09,090 debug logging to our app. Now in this 3 00:00:09,090 --> 00:00:10,760 video, we're gonna have a look at what it 4 00:00:10,760 --> 00:00:12,790 does and how it can be useful. Now at the 5 00:00:12,790 --> 00:00:15,150 bottom of the Android Studio screen, are a 6 00:00:15,150 --> 00:00:16,379 couple of panes that can be really 7 00:00:16,379 --> 00:00:18,610 useful when you get around to running 8 00:00:18,610 --> 00:00:21,690 your apps. Now the first one, down here, is 9 00:00:21,690 --> 00:00:24,980 the run window. So run the, what I'm going 10 00:00:24,980 --> 00:00:28,210 to do is run the app now, and just confirm 11 00:00:28,210 --> 00:00:30,470 that we can see that running. You can see 12 00:00:30,470 --> 00:00:32,700 that running now. Now I'm going to click 13 00:00:32,700 --> 00:00:35,030 on the run tab over here, to actually see 14 00:00:35,030 --> 00:00:38,030 some information about it, and it starts 15 00:00:38,030 --> 00:00:40,039 off showing the adb commands that 16 00:00:40,039 --> 00:00:42,659 Android Studio uses to install and run 17 00:00:42,659 --> 00:00:44,800 the device. Now we're going to be looking 18 00:00:44,800 --> 00:00:47,839 at adb, which is the Android debug bridge, 19 00:00:47,839 --> 00:00:49,269 a lot later in the course. 20 00:00:49,269 --> 00:00:50,749 But if you get problems running your 21 00:00:50,749 --> 00:00:53,289 apps, then we may ask you to have a look 22 00:00:53,289 --> 00:00:55,359 in here and paste any error messages 23 00:00:55,359 --> 00:00:57,850 into the discussion in Q&A area. 24 00:00:57,850 --> 00:00:59,300 Now hopefully, Android Studio will 25 00:00:59,300 --> 00:01:01,789 connect your emulator or device. If 26 00:01:01,789 --> 00:01:03,659 you've got a slow system with less than, 27 00:01:03,659 --> 00:01:06,049 say, 8 gigabytes of RAM, then it's a good 28 00:01:06,049 --> 00:01:08,670 idea to start your emulator manually. And 29 00:01:08,670 --> 00:01:10,500 you can see that in videos that I've 30 00:01:10,500 --> 00:01:12,580 been doing, in some cases I've been 31 00:01:12,580 --> 00:01:14,160 running the emulator, or having the emulator 32 00:01:14,160 --> 00:01:16,550 already running. And by the way, you can 33 00:01:16,550 --> 00:01:18,500 do that from the AVD manager that we saw 34 00:01:18,500 --> 00:01:21,610 in section 3. Now starting the emulator 35 00:01:21,610 --> 00:01:22,780 before you start coding wll give it 36 00:01:22,780 --> 00:01:24,800 time to start up, and saves you having to 37 00:01:24,800 --> 00:01:27,360 wait when you run your app. Ok, so that's 38 00:01:27,360 --> 00:01:30,780 the Run tab. The second tab is used for 39 00:01:30,780 --> 00:01:33,641 once your app actually runs, and I'm 40 00:01:33,641 --> 00:01:37,200 ignoring the TODO at this stage. And it 41 00:01:37,200 --> 00:01:38,700 used to be called the Android monitor 42 00:01:38,700 --> 00:01:41,180 pane, but in Android Studio 3 it's now been 43 00:01:41,180 --> 00:01:43,660 renamed to logcat, and that's this is one here, 44 00:01:43,660 --> 00:01:45,210 and possibly they renamed it because 45 00:01:45,210 --> 00:01:47,480 that's what most people called it anyway. 46 00:01:47,480 --> 00:01:49,860 Now the logcat, as I click that, that 47 00:01:49,860 --> 00:01:52,180 shows all sorts of things that are 48 00:01:52,180 --> 00:01:54,310 happening on any connected device or 49 00:01:54,310 --> 00:01:56,420 emulator, and with a real device you'd 50 00:01:56,420 --> 00:01:58,730 see loads of entries appearing in here. 51 00:01:58,730 --> 00:02:00,480 And you'd probably also see some warnings 52 00:02:00,480 --> 00:02:02,320 and probably some errors as well, and 53 00:02:02,320 --> 00:02:03,810 most of these will have nothing to do 54 00:02:03,810 --> 00:02:05,960 with your app. So the trick here is to 55 00:02:05,960 --> 00:02:07,930 work out what's what, because there can 56 00:02:07,930 --> 00:02:09,729 be a lot of output in here, but 57 00:02:09,729 --> 00:02:12,440 fortunately we can filter that. Now over 58 00:02:12,440 --> 00:02:16,330 to the far right here, is a drop down and 59 00:02:16,330 --> 00:02:17,640 at the moment, you can see that it's been 60 00:02:17,640 --> 00:02:18,640 set to Show 61 00:02:18,640 --> 00:02:20,750 only selected application. Now you might 62 00:02:20,750 --> 00:02:22,260 think that that's enough and we don't 63 00:02:22,260 --> 00:02:24,530 need to provide a filter as well, but it 64 00:02:24,530 --> 00:02:26,640 also shows all sorts of system events. 65 00:02:26,640 --> 00:02:28,670 Now if we want a full picture of what's 66 00:02:28,670 --> 00:02:30,780 being logged, we can change this drop 67 00:02:30,780 --> 00:02:34,220 down to No filters, as I've done there now. 68 00:02:34,220 --> 00:02:35,690 Now we get a ton of output as I scroll 69 00:02:35,690 --> 00:02:37,410 up. You can see there's a lot of 70 00:02:37,410 --> 00:02:41,890 information there, and that's with the 71 00:02:41,890 --> 00:02:42,890 emulator just sitting there, not really 72 00:02:42,890 --> 00:02:44,781 doing much. And all of that might be 73 00:02:44,781 --> 00:02:46,980 useful to Google engineers but I think, 74 00:02:46,980 --> 00:02:48,510 at this point, it's fairly confusing to 75 00:02:48,510 --> 00:02:50,430 us. So what I'm going to do is put that 76 00:02:50,430 --> 00:02:51,770 back to show only the selected 77 00:02:51,770 --> 00:02:53,520 application again, Show any selected 78 00:02:53,520 --> 00:02:56,250 application. Now if you're debugging more 79 00:02:56,250 --> 00:02:57,250 than one app at once, 80 00:02:57,250 --> 00:02:59,290 maybe you've created a GUI app and a 81 00:02:59,290 --> 00:03:01,260 service, for example, then you select 82 00:03:01,260 --> 00:03:02,670 which one to display in the second 83 00:03:02,670 --> 00:03:06,420 drop-down, over here, the second from 84 00:03:06,420 --> 00:03:07,420 the left, 85 00:03:07,420 --> 00:03:08,950 and you could choose which particular 86 00:03:08,950 --> 00:03:10,090 app that you're wanting to get the 87 00:03:10,090 --> 00:03:13,330 output for. You can see that there's 88 00:03:13,330 --> 00:03:15,980 quite a few Android apps that are 89 00:03:15,980 --> 00:03:17,760 actually running, right now on this 90 00:03:17,760 --> 00:03:19,430 emulator, but you can see that's ours 91 00:03:19,430 --> 00:03:20,430 there, obviously, by the name. Alright, so 92 00:03:20,430 --> 00:03:23,980 I'm going to leave that on buttoncounter 93 00:03:23,980 --> 00:03:26,880 app still, which is our app, and we can 94 00:03:26,880 --> 00:03:28,210 look at what happened when we launched the 95 00:03:28,210 --> 00:03:30,270 app. Now if we scroll back to the top of 96 00:03:30,270 --> 00:03:33,620 the entries, and if you scroll down and 97 00:03:33,620 --> 00:03:35,569 have a look, we should be able to see the 98 00:03:35,569 --> 00:03:39,739 onCreate method message that we actually 99 00:03:39,739 --> 00:03:42,170 put into our code, and there's the on 100 00:03:42,170 --> 00:03:45,800 Create called message. So that's the text 101 00:03:45,800 --> 00:03:47,280 we used in the logd call, or at least 102 00:03:47,280 --> 00:03:49,400 the last part is, the onCcreate called. 103 00:03:49,400 --> 00:03:51,480 The rest has been added automatically by 104 00:03:51,480 --> 00:03:52,900 Android to the left-hand side. Alright, so 105 00:03:52,900 --> 00:03:55,730 there's still a lot of other stuff, though, 106 00:03:55,730 --> 00:03:56,940 you can see here that we're not really 107 00:03:56,940 --> 00:03:58,410 interested in at this stage. So we can 108 00:03:58,410 --> 00:04:00,750 tidy up this log a little bit more. So 109 00:04:00,750 --> 00:04:02,740 there's a search box towards the 110 00:04:02,740 --> 00:04:05,349 right-hand side over here. So what we can 111 00:04:05,349 --> 00:04:08,300 do, is we can further filter what's being 112 00:04:08,300 --> 00:04:10,349 displayed, and that's one of the reasons 113 00:04:10,349 --> 00:04:12,160 for providing the TAG that we did in the 114 00:04:12,160 --> 00:04:14,720 previous video, in our login call. Our TAG 115 00:04:14,720 --> 00:04:16,160 was MainActivity, so I'm going to type 116 00:04:16,160 --> 00:04:18,870 MainActivity into the box, mainactivity, 117 00:04:18,870 --> 00:04:23,070 and you can see that we've got on 118 00:04:23,070 --> 00:04:26,900 Create called now, which was the message. Now 119 00:04:26,900 --> 00:04:28,780 I suggest always typing in lowercase 120 00:04:28,780 --> 00:04:30,880 here. By all means experiment, but 121 00:04:30,880 --> 00:04:31,880 generally, you'll get 122 00:04:31,880 --> 00:04:34,020 better results with lower case. Basically, 123 00:04:34,020 --> 00:04:35,781 lower case will always work, but if you 124 00:04:35,781 --> 00:04:37,500 try to match the case of the TAG and 125 00:04:37,500 --> 00:04:40,250 make a mistake, then it doesn't work. So 126 00:04:40,250 --> 00:04:42,210 I'm going to quickly demonstrate that by 127 00:04:42,210 --> 00:04:44,220 deleting the contents of the box, then 128 00:04:44,220 --> 00:04:47,061 typing Main, this time with a capital M. 129 00:04:47,061 --> 00:04:49,530 So that's obviously working fine, but if 130 00:04:49,530 --> 00:04:51,370 I then start to add activity, but using a 131 00:04:51,370 --> 00:04:54,220 lowercase a, a c, you can see that the 132 00:04:54,220 --> 00:04:56,790 log output has disappeared. In other 133 00:04:56,790 --> 00:04:58,550 words, nothing matches so all the log 134 00:04:58,550 --> 00:05:00,199 entries disappear. And once again, 135 00:05:00,199 --> 00:05:01,800 experiment after you watch this and the 136 00:05:01,800 --> 00:05:03,550 next video. Alright, so I'm going to put 137 00:05:03,550 --> 00:05:05,520 that mainactivity back in lower case 138 00:05:05,520 --> 00:05:09,290 again, and we only get the log entry 139 00:05:09,290 --> 00:05:10,729 entries from our MainActivity class. 140 00:05:10,729 --> 00:05:12,080 Now at the moment there's only one 141 00:05:12,080 --> 00:05:14,980 entry, onCreate called, and we will be 142 00:05:14,980 --> 00:05:17,169 adding some more, but at the moment I 143 00:05:17,169 --> 00:05:18,169 just want to get you familiar with 144 00:05:18,169 --> 00:05:20,330 filtering the logcat. Now if you recall, 145 00:05:20,330 --> 00:05:21,990 we also added login to the buttons 146 00:05:21,990 --> 00:05:23,810 onClickListener. So I'm going to switch 147 00:05:23,810 --> 00:05:27,500 over to the emulator now. Type in some 148 00:05:27,500 --> 00:05:32,960 text and click on the button, and you can see 149 00:05:32,960 --> 00:05:36,510 what appeared in the logcat pane. 150 00:05:36,510 --> 00:05:38,120 Basically, every time the button's tapped 151 00:05:38,120 --> 00:05:39,370 we're going to get that second message, 152 00:05:39,370 --> 00:05:41,100 and we can just go back and just click it a 153 00:05:41,100 --> 00:05:42,100 few more times. 154 00:05:42,100 --> 00:05:45,390 You can see it's being outputted into 155 00:05:45,390 --> 00:05:47,870 the logcat pane every time that button's 156 00:05:47,870 --> 00:05:51,010 actually tapped. So that's pretty cool 157 00:05:51,010 --> 00:05:53,290 and it's a very useful way to see when 158 00:05:53,290 --> 00:05:56,040 you miss it, when your methods get called. 159 00:05:56,040 --> 00:05:57,240 Now I've been telling you that the 160 00:05:57,240 --> 00:05:59,760 activity will be destroyed and recreated 161 00:05:59,760 --> 00:06:01,950 when the device is rotated. Now if that's the 162 00:06:01,950 --> 00:06:03,710 case, we should see onCreate being called 163 00:06:03,710 --> 00:06:06,270 again when we switch to landscape. Now 164 00:06:06,270 --> 00:06:07,770 before I do that, there's something else 165 00:06:07,770 --> 00:06:09,830 I want to show you. And it's not obvious 166 00:06:09,830 --> 00:06:11,770 and it's also not documented, at least as 167 00:06:11,770 --> 00:06:14,080 far as we've been able to find. Now we 168 00:06:14,080 --> 00:06:15,960 can quickly end up with a lot of log 169 00:06:15,960 --> 00:06:17,650 entries, even with the filters that we've 170 00:06:17,650 --> 00:06:20,350 set up, and that can make working out 171 00:06:20,350 --> 00:06:22,570 what's just happened quite difficult. Now 172 00:06:22,570 --> 00:06:24,600 fortunately, we can break up these log 173 00:06:24,600 --> 00:06:26,380 entries a bit. What we're about to do is 174 00:06:26,380 --> 00:06:29,060 rotate the device into landscape, so we 175 00:06:29,060 --> 00:06:31,310 can add a comment to that effect in the 176 00:06:31,310 --> 00:06:33,770 logcat. Now if we move, or 177 00:06:33,770 --> 00:06:35,490 click the mouse after the last entry in 178 00:06:35,490 --> 00:06:38,830 the logcat, we actually get a line break, 179 00:06:38,830 --> 00:06:40,150 as you can see there, the cursor moved 180 00:06:40,150 --> 00:06:42,419 down. And if we type in something 181 00:06:42,419 --> 00:06:48,630 here, so rotating into landscape, and 182 00:06:48,630 --> 00:06:51,530 press ENTER again. And now we're going to 183 00:06:51,530 --> 00:06:52,990 go back and rotate the device into 184 00:06:52,990 --> 00:06:55,300 landscape, and if I do that, 185 00:06:55,300 --> 00:07:00,919 you can see what's happened, that 186 00:07:00,919 --> 00:07:04,020 obviously, the device has been rotated 187 00:07:04,020 --> 00:07:07,020 into landscape mode. And as expected, 188 00:07:07,020 --> 00:07:08,460 we can see the onCreate being called 189 00:07:08,460 --> 00:07:10,169 again, and now that we've actually typed 190 00:07:10,169 --> 00:07:12,300 in that message, it's very easy to see 191 00:07:12,300 --> 00:07:13,669 which methods were called after we did 192 00:07:13,669 --> 00:07:15,949 that. So if we then come back here 193 00:07:15,949 --> 00:07:17,980 and then add another message, back to 194 00:07:17,980 --> 00:07:24,220 portrait, then go back to the device, our 195 00:07:24,220 --> 00:07:27,110 emulator, then go back to portrait, we can 196 00:07:27,110 --> 00:07:30,699 see that our logcat is telling us that 197 00:07:30,699 --> 00:07:33,680 onCreate's called again, and also now the 198 00:07:33,680 --> 00:07:36,550 layout of the log is probably a lot more 199 00:07:36,550 --> 00:07:39,260 intuitive. Now this is going to be far 200 00:07:39,260 --> 00:07:41,720 more informative when we log the rest of 201 00:07:41,720 --> 00:07:43,780 the lifecycle methods, but already you 202 00:07:43,780 --> 00:07:45,320 can see that it's a great way to see 203 00:07:45,320 --> 00:07:47,260 what's going on with our apps, and which 204 00:07:47,260 --> 00:07:48,840 of our methods the Android framework is 205 00:07:48,840 --> 00:07:51,410 calling. Now I said that this ability to 206 00:07:51,410 --> 00:07:52,900 type comments into the logcat is 207 00:07:52,900 --> 00:07:54,850 undocumented. It's been working for the 208 00:07:54,850 --> 00:07:57,220 last few releases of Android Studio but 209 00:07:57,220 --> 00:07:58,979 that doesn't mean it's going to 210 00:07:58,979 --> 00:08:01,539 continue to work. And the reason for that 211 00:08:01,539 --> 00:08:03,670 is Google changes things all the time. So 212 00:08:03,670 --> 00:08:06,180 make use of it while as long 213 00:08:06,180 --> 00:08:08,380 as it lasts, or as long as it lasts, and 214 00:08:08,380 --> 00:08:09,850 if you find that you can no longer type 215 00:08:09,850 --> 00:08:11,680 into the logcat, you'll just have to accept 216 00:08:11,680 --> 00:08:13,360 that the facility's been removed, for 217 00:08:13,360 --> 00:08:15,889 whatever reason. Now the last time I 218 00:08:15,889 --> 00:08:17,910 mentioned an undocumented feature in 219 00:08:17,910 --> 00:08:19,880 a video, Google removed it two weeks 220 00:08:19,880 --> 00:08:21,660 later. So I'm just, so that's why I'm 221 00:08:21,660 --> 00:08:23,020 telling you now that it might stop 222 00:08:23,020 --> 00:08:25,820 working. Alright, so that's the logcat 223 00:08:25,820 --> 00:08:27,039 and how to use it. 224 00:08:27,039 --> 00:08:29,569 Now I haven't mentioned this first 225 00:08:29,569 --> 00:08:32,940 drop-down box, and if you've got several 226 00:08:32,940 --> 00:08:35,130 emulators or devices connected, it lets 227 00:08:35,130 --> 00:08:36,610 you select which one you want to view 228 00:08:36,610 --> 00:08:38,860 the logs for. Keep an eye on that because 229 00:08:38,860 --> 00:08:40,350 if you close down the emulator and 230 00:08:40,350 --> 00:08:42,120 restart it, you might end up with one 231 00:08:42,120 --> 00:08:44,640 marked as dead. Now obviously, it won't 232 00:08:44,640 --> 00:08:46,670 log anything if it's not running, so use 233 00:08:46,670 --> 00:08:48,870 that drop box, if that's the case, to 234 00:08:48,870 --> 00:08:51,300 choose the one that is running. Alright, 235 00:08:51,300 --> 00:08:53,490 I'm gonna stop the video here. In the 236 00:08:53,490 --> 00:08:54,810 next video we'll add logging to the 237 00:08:54,810 --> 00:08:57,160 other life cycle methods, and see exactly 238 00:08:57,160 --> 00:08:58,740 what happens to our app while it's 239 00:08:58,740 --> 00:09:00,460 running. So I'll see you in the next 240 00:09:00,460 --> 00:09:00,800 video.